GraphQL : the object name is defined in resolvers, but not in schema

后端 未结 4 1300
说谎
说谎 2021-02-20 00:01

I want to define a mutation using graphql.

My mutation is getting an object as argument. So I defined the new Object in the schema and in the resolver using GraphQLObjec

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-20 00:33

    Extra data (Related to the error - not to the Q code).

    hello-world

    We define our resolvers in a map, where the map's keys correspond to our schema's types. https://www.apollographql.com/docs/tutorial/resolvers/

    The most basic "hello world" example of this "wrong map" error.

    I was wrong on purpose (under resolver definitions - use hello2 instead of hello).

    graphql-yoga server example:

    /*index.js*/
    const { GraphQLServer } = require('graphql-yoga')
    
    const typeDefs = `
      type Query {
        hello(name: String): String!
      }
    `
    
    const resolvers = {
      Query: {
        hello2: (_, { name }) => `Hello ${name || 'World'}`,
      },
    }
    
    const server = new GraphQLServer({ typeDefs, resolvers })
    server.start(() => console.log('Server is running on localhost:4000'))
    

    Throw error:

    [Error: Query.hello2 defined in resolvers, but not in schema]

    Change the resolver to hello (match to hello schema type) to fix this error:

    Related:

    • schema docs: https://graphql.org/learn/schema/
    • Great tuturial: https://www.apollographql.com/docs/tutorial/introduction/

提交回复
热议问题