Apollo Server Federation not resolving?

590 Views Asked by At

First of all thanks for reading this, I have spent the last day on this one bug that I am simply unable to figure out. I had it working before, but for some reason it just stopped. Basically, all the services standalone work as expected. However, once I try to reference another one (let's say I am using the profile service and want to pull the email from the account service) it just returns null. No error message, no nothing. I have added the important code snippets, although if you would like the see the entire code base please let me know and I will will push it to github.

ACCOUNT TYPE DEFS:

import { gql } from 'apollo-server';

const typeDefs = gql`
  # SCALARS
  """
  An ISO 8601-encoded UTC date string...
  """
  scalar DateTime
  # OBJECTS
  """
  An account is an Auth0 user that provides authentication details.
  """
  type Account @key(fields: "id") {
    "The unique Auth0 ID associated with the account."
    id: ID!
    "The date and time the account was created."
    createdAt: DateTime!
    "The email associated with the account (must be unique)."
    email: String
    "Whether the account is blocked."
    isBlocked: Boolean
    "Whether the account has a moderator role."
    isModerator: Boolean
  }

ACCOUNT RESOLVERS:

 Account: {
    __resolveReference(reference, { dataSources }, info) {
      return dataSources.accountsAPI.getAccountById(reference.id);
    },
    id(account, args, context, info) {
      return account.user_id;
    },
    createdAt(account, args, context, info) {
      return account.created_at;
    },
    isModerator(account, args, context, info) {
      return (
        account.app_metadata &&
        account.app_metadata.roles &&
        account.app_metadata.roles.includes('moderator')
      );
    },
    isBlocked(account, args, context, info) {
      return account.blocked;
    },
  },

PROFILE TYPE DEFS:

      extend type Account @key(fields: "id") {
        id: ID! @external
        "Metadata about the user that owns the account."
        profile: Profile
      }
  type Profile @key(fields: "id") {
    "The unique MongoDB document ID of the user's profile."
    id: ID!
    "The Auth0 account tied to this profile."
    account: Account!
    "The URL of the user's avatar."
    avatar: String
    "A short bio or description about the user (max. 256 characters)."
    description: String
    "Other users that the user follows."
    following(
      first: Int
      after: String
      last: Int
      before: String
      orderBy: ProfileOrderByInput
    ): ProfileConnection
    "The full name of the user."
    fullName: String
    "The URL of the user's GitHub page."
    githubUrl: String
    "The user's pinned GitHub repositories and gists."
    pinnedItems: [PinnableItem]
    "The unique username of the user."
    username: String!
    "Whether the currently logged in user follows this profile."
    viewerIsFollowing: Boolean
  }

PROFILE RESOLVER:

Profile: {
    account(profile, args, context, info) {
      return { __typename: 'Account', id: profile.account_id };
    },
}
1

There are 1 best solutions below

0
On

While writing this I realized what the problem was, so I will post it here so that none of you will have to waste your time. I don't yet understand why, I will follow up soon, but for some reason, the permissions that I have (using grahpql-shield). Broke everything, after commenting it out when creating the federated service, it magically started working, for those who are curious, here were my permissions.

ACCOUNT PERMISSION

import { and, or, rule, shield } from 'graphql-shield';

import getPermissions from '../../lib/getPermissions';

const canReadAnyAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('read:any_account');
});

const canReadOwnAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('read:own_account');
});
const canEditOwnAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('edit:own_account');
});
const canBlockAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('block:any_account');
});
const canPromoteAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('promote:any_account');
});
const isReadingOwnAccount = rule()(
  (parent, { id }, { user }, info) => user.sub === id,
);

const isEditingOwnAccount = rule()(
  (parent, { where: { id } }, { user }, info) => user.sub === id,
);

const permissions = shield(
  {
    Query: {
      account: or(
        and(canReadAnyAccount, isEditingOwnAccount),
        canReadAnyAccount,
      ),
      accounts: canReadAnyAccount,
    },
    Mutation: {
      changeAccountBlockedStatus: canBlockAccount,
      changeAccountModeratorRole: canPromoteAccount,
      deleteAccount: and(canEditOwnAccount, isEditingOwnAccount),
      updateAccount: and(canEditOwnAccount, isEditingOwnAccount),
    },
  },
  { debug: process.env.NODE_ENV === 'development' },
);

export default permissions;

PROFILES PERMISSIONS:

import { and, or, rule, shield } from 'graphql-shield';

import getPermissions from '../../lib/getPermissions';

const canReadAnyAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('read:any_account');
});

const canReadOwnAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('read:own_account');
});
const canEditOwnAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('edit:own_account');
});
const canBlockAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('block:any_account');
});
const canPromoteAccount = rule()((parent, args, { user }, info) => {
  const userPermissions = getPermissions(user);

  return userPermissions && userPermissions.includes('promote:any_account');
});
const isReadingOwnAccount = rule()(
  (parent, { id }, { user }, info) => user.sub === id,
);

const isEditingOwnAccount = rule()(
  (parent, { where: { id } }, { user }, info) => user.sub === id,
);

const permissions = shield(
  {
    Query: {
      account: or(
        and(canReadAnyAccount, isEditingOwnAccount),
        canReadAnyAccount,
      ),
      accounts: canReadAnyAccount,
    },
    Mutation: {
      changeAccountBlockedStatus: canBlockAccount,
      changeAccountModeratorRole: canPromoteAccount,
      deleteAccount: and(canEditOwnAccount, isEditingOwnAccount),
      updateAccount: and(canEditOwnAccount, isEditingOwnAccount),
    },
  },
  { debug: process.env.NODE_ENV === 'development' },
);

export default permissions;

I also would like to also thank Mandi from 8 bit-press as she is the reason I am learning graphql with her awesome book. There might be a few errors here or there (not yet sure as I am prone to making mistakes, if not this might be a mistake and will be useful for anyone else reading the book) but I certainly do recommend for anyone looking to learn apollo to check it out https://8bit.press/book/advanced-graphql