graphql/graphql-relay-js

Pass arguments to edgeFields resolvers?

danturu opened this issue · 1 comments

Is it possible to pass arguments to edgeFields resolvers?

const { connectionType: organizationConnection } = connectionDefinitions({

  edgeFields: {
    role: {
      type: RoleEnumType,
      resolve: async (edge, args, ctx) => {
        // !!!
        // HOW CAN I GET THE USER ID HERE NOT THE VIEWER ID??? HOW CAN I PASS ARGS?
        // !!!
      }
    },
  },
});

const UserType = new GraphQLObjectType({
  ...
  
  organizations: {
    type: organizationConnection,
    description: 'The organizations of the user.',
    args: connectionArgs,

    resolve: (user, args, ctx) => {
      const organizations = ctx.organizations.listByUserId({
        userId: user.id, // NOT THE VIEWER ID
      });

      return connectionFromPromisedArray(organizations, args);
    }
  },

  ...
}

I asked in the discord's channel and someone recommend me to use context, but I think that the ctx field isn't a good place, because it's not the current user. In my case the viewer (the current user) could see the other users' organizations from its (the viewer) perspective.

Sorry for the delayed response.
You can wrap array items with object and put userId there:

      const organizations = 
       ctx.organizations.listByUserId({ userId: user.id })
        .map(node => ({ userId: user.id, node }));