Skip to content

GraphQLFieldConfig prevents arguments from being typed #2829

Closed
@migueloller

Description

@migueloller

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions