Search code examples
typescriptgraphqlgraphql-js

why the custom sub query type isn't assignable to GraphQLObjectType?


I attempted to create a graphQl query schema with typescript and got this error message

Argument of type '{ name: string; movie: { type: GraphQLObjectType<any, any>; args: { id: { type: GraphQLScalarType; }; }; resolve(parent: any, args: any): void; }; }' is not assignable to parameter of type 'Readonly<GraphQLObjectTypeConfig<any, any>>'. Object literal may only specify known properties, and 'movie' does not exist in type 'Readonly<GraphQLObjectTypeConfig<any, any>>'

const Query = new GraphQLObjectType({
    name: "Query",
    movie: {
        type: MovieType,
        args: { id: { type: GraphQLString } },
        resolve(parent, args) {},
    },
});

Solution

  • movie is not a valid parameter to pass to GraphQLObjectType's constructor. If your intent is to add a field named movie to your Query type, you should do:

    const Query = new GraphQLObjectType({
      name: "Query",
      fields: {
        movie: {
          type: MovieType,
          args: { id: { type: GraphQLString } },
          resolve(parent, args) {},
        },
      },
    });