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

Update field resolver interface to allow typed args #932

Merged
merged 3 commits into from
Sep 7, 2018

Conversation

luk3thomas
Copy link
Contributor

@luk3thomas luk3thomas commented Aug 18, 2018

Related to #704

This change allows me to specify a type for the args object of a resolver. Without this change I cannot specify types for the args object of a resolver. As an example

const resolvers = {
  Query: {
    hello: (_parent: object, args: { id: string }) => `Hello ${args.id}!`,
  },
};

☝️ Does not compile.

[ts]
Argument of type '{ typeDefs: DocumentNode; resolvers: { Query: { hello: (_parent: object, args: { id: string; }) => string; }; }; }' is not assignable to parameter of type 'Config'.
  Types of property 'resolvers' are incompatible.
    Type '{ Query: { hello: (_parent: object, args: { id: string; }) => string; }; }' is not assignable to type 'IResolvers<any, any>'.
      Property 'Query' is incompatible with index signature.
        Type '{ hello: (_parent: object, args: { id: string; }) => string; }' is not assignable to type '(() => any) | GraphQLScalarType | IEnumResolver | IResolverObject<any, any> | IResolverOptions<any, any>'.
          Type '{ hello: (_parent: object, args: { id: string; }) => string; }' has no properties in common with type 'IResolverOptions<any, any>'.
(property) typeDefs: DocumentNode

TODO:

  • If this PR is a new feature, reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass
  • Update CHANGELOG.md with your change. Include a description of your change, link to PR (always) and issue (if applicable). Add your CHANGELOG entry under vNEXT. Do not create a new version number for your change yourself.

@apollo-cla
Copy link

@luk3thomas: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/

Copy link
Contributor

@hwillson hwillson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @luk3thomas - LGTM!

@booooh
Copy link

booooh commented Oct 19, 2018

Maybe I'm doing something wrong, but I bumped my version of apollo-server-express to 2.2.0-alpha, which seems to take in this change, and I'm still seeing the same issue.

src/index.ts:21:3 - error TS2322: Type '{ Query: { hello: (parent: any, args: { name: string; }) => string; }; }' is not assignable to type 'IResolvers<any, any>'.
  Property 'Query' is incompatible with index signature.
    Type '{ hello: (parent: any, args: { name: string; }) => string; }' is not assignable to type 'GraphQLScalarType | IEnumResolver | (() => any) | IResolverObject<any, any> | IResolverOptions<any, any>'.
      Type '{ hello: (parent: any, args: { name: string; }) => string; }' has no properties in common with type 'IResolverOptions<any, any>'.

@booooh
Copy link

booooh commented Oct 20, 2018

Specifically, the following 2 examples demonstrate the failure vs a working example. Note that the args needs to be typed as a Mapping in order for this to work

const good: IFieldResolver<any, any> = (p: any, args: { [key: string]: any }) => `hello ${args.id}!`; //Works :)
const bad: IFieldResolver<any, any> = (p: any, args: { id: string }) => `hello ${args.id}!`; //Error :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants