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

GraphQLFieldConfig prevents arguments from being typed #2829

Closed
migueloller opened this issue Oct 27, 2020 · 1 comment · Fixed by #3328
Closed

GraphQLFieldConfig prevents arguments from being typed #2829

migueloller opened this issue Oct 27, 2020 · 1 comment · Fixed by #3328

Comments

@migueloller
Copy link

Using a non-optional type for field arguments is not possible with current TypeScript typings. Here's a small reproduction:

import {
  GraphQLNonNull,
  GraphQLObjectType,
  GraphQLSchema,
  GraphQLString,
} from "graphql";

const queryType = new GraphQLObjectType({
  name: "Query",
  fields: {
    hello: {
      type: new GraphQLNonNull(GraphQLString),
      args: { message: { type: new GraphQLNonNull(GraphQLString) } },
      resolve(rootValue, args: { message: string }) {
        return `Hello, ${args.message}!`;
      },
    },
  },
});

export const schema = new GraphQLSchema({ query: queryType });
src/schema.ts:14:7 - error TS2322: Type '(rootValue: any, args: { message: string; }) => string' is not assignable to type 'GraphQLFieldResolver<any, any, { [argName: string]: any; }>'.
  Types of parameters 'args' and 'args' are incompatible.
    Property 'message' is missing in type '{ [argName: string]: any; }' but required in type '{ message: string; }'.

14       resolve(rootValue, args: { message: string }) {
         ~~~~~~~

  src/schema.ts:14:34
    14       resolve(rootValue, args: { message: string }) {
                                        ~~~~~~~
    'message' is declared here.
  node_modules/graphql/type/definition.d.ts:519:3
    519   resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>;
          ~~~~~~~
    The expected type comes from property 'resolve' which is declared here on type 'GraphQLFieldConfig<any, any, { [argName: string]: any; }>'


Found 1 error.

If the type of args is changed to { message?: string } the problem goes away. But this isn't the correct type since that input field is non-null.

@mabn
Copy link

mabn commented Oct 21, 2021

This was broken in #2488

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 a pull request may close this issue.

2 participants