Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass previous object to resolvers #2206

Open
Tracked by #3150
cjelger opened this issue Sep 30, 2019 · 6 comments
Open
Tracked by #3150

Pass previous object to resolvers #2206

cjelger opened this issue Sep 30, 2019 · 6 comments

Comments

@cjelger
Copy link

cjelger commented Sep 30, 2019

Feature requests

Based on the documentation at https://graphql.org/learn/execution/#root-fields-resolvers, I expected that resolvers would always receive the parent/previous object as the first argument. However when I tested the library, the arguments received by the resolvers are actually (args, context, info).

This doesn't match the documentation, and is a serious limitation when one needs the parent object (for example, some id) in order to resolve children fields.

Is there a workaround or is this a known limitation?

@IvanGoncharov
Copy link
Member

@cjelger Sorry for the confusion we definitely need to update our docs.
The short answer: you get the root object as this because resolver is called on this object.

This doesn't match the documentation

We actually have two types of resolvers:

  1. specified in GraphQLObjectType fields and they get (parent, args, context, info)
  2. resolvers specified on parent itself, in this case, their prototype is (args, context, info) and you can access parent using this.

@cjelger
Copy link
Author

cjelger commented Sep 30, 2019

Thanks @IvanGoncharov, using this indeed works! I guess there might be a similar "mismatch" in the documentation when it comes to defining resolvers: the documentation seems to indicate that it's possible to define resolvers for types like Human at https://graphql.org/learn/execution/#scalar-coercion but this also doesn't work when defining the resolvers via the graphql method. Would this work when defining resolvers with GraphQLObjectType?

@IvanGoncharov
Copy link
Member

the documentation seems to indicate that it's possible to define resolvers for types like Human at graphql.org/learn/execution/#scalar-coercion but this also doesn't work when defining the resolvers via the graphql method

@cjelger It should work, see here for a complete example: https://github.com/graphql/graphql-js/blob/eef0878d851f7b8a4392b031550a1080497c018c/src/__tests__/starWarsSchema.js

@cjelger
Copy link
Author

cjelger commented Oct 11, 2019

@IvanGoncharov Thank you for your reply. I will try this again based on the example/test ... it's pretty much what I tried with minor differences.

@benallfree
Copy link

Hello, this one bit me too. I am using graphql-code-generator to generate Typescript resolvers. The resolver function signature matches the Graphql specification:

export type ResolverFn<TResult, TParent, TContext, TArgs> = (
  parent: TParent,
  args: TArgs,
  context: TContext,
  info: GraphQLResolveInfo
) => Promise<TResult> | TResult;

But when I write a simple resolver for graphql-js,

hello: (...args) => {
    console.log({ args })
    return {
      greeting: "Hello"
    }
  },

I see that the parent is not passed

args: [
   { ...variable info... },
   { ...context info... },
   { ... other info ... }
]

What is the recommended fix or workaround for this?

@benallfree
Copy link

Update: using makeExecutableSchema works perfectly.

@saihaj saihaj mentioned this issue May 30, 2021
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants