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

后端 未结 4 1273
说谎
说谎 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:32

    I came across this error when migrating from v1 of apollo-server-express to v2, the error had to do with Uploads not being defined in schema. Now when declaring you graphql schema you can use the option set uploads: false

    // GraphQL: Schema
    const SERVER = new ApolloServer({
      typeDefs: typeDefs,
      resolvers: resolvers,
      introspection: true,
      uploads: false,
      playground: {
        endpoint: `http://localhost:3000/graphql`,
        settings: {
          'editor.theme': 'light'
        }
      }
    });
    

    Appears to solve the issue in this case if your error is Uploads specific

    0 讨论(0)
  • 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/
    0 讨论(0)
  • 2021-02-20 00:40

    In my case it happens because there is inconsistent in schema for non-null. in my mutation I don't have the non-null mutation while in the object schema it has non-null mutation.

    0 讨论(0)
  • 2021-02-20 00:47

    Couple of things to fix here. First, to use an object as an argument, you have to define it as an input (or GraphQLInputObjectType) in your schema -- you cannot use a regular type (or GraphQLObjectType) as an argument.

    So your type definitions need to look something like this:

    type Mutation {
      agreementsPost(agreement: Agreement): String
    }
    
    input Agreement {
      id: Int
    }
    

    If you already have an Agreement type, you'll need to name your input something else. It's a good convention to just append Input to whatever your type name is:

    type Mutation {
      agreementsPost(agreement: AgreementInput): String
    }
    
    type Agreement {
      id: Int
    }
    
    input AgreementInput {
      id: Int
    }
    

    This should be sufficient to allow you to pass in an AgreementInput object as an argument to your mutation. You don't need to add Agreement or AgreementInput to your resolvers (in fact, inputs are not "resolved" by GraphQL, so adding a resolver for an input is not possible).

    That said, your resolvers object should not need to incorporate any of the type constructors provided by the graphql package -- Apollo constructs a GraphQLSchema object from your resolvers and type definitions for you when you call makeExecutableSchema.

    If your type definitions include the types Foo and Bar, your resolvers object might look something like this:

    const resolvers = {
      Foo: {
        someFooProperty: (foo, args, context, info) => {}
      },
      Bar: {
        someBarProperty: (bar, args, context, info) => {}
        someOtherBarProperty: (bar, args, context, info) => {}
      },
      Query: {
        someQuery: (root, args, context, info) => {}
      },
      Mutation: {
        someMutation: (root, args, context, info) => {}
      },
    }
    

    Notice how each property in the resolvers object matches one of the types defined in your schema (including Query and Mutation). The value of each of those properties is itself an object, with each property mapping to one of the fields defined for that particular type. Each field's value is your resolve function.

    The reason for the error you're seeing is that you've effectively told makeExecutableSchema to add resolvers to two fields on the Agreement type -- name and fields -- neither of which are actually in your schema according to your type definitions.

    You can read more about how to generate a schema using Apollo here. You may see examples out there of generating a schema "programatically" using just GraphQL.js by defining a GraphQLSchema object and passing that to your middleware instead. There's pros and cons to both approaches, but using makeExecutableSchema is generally easier and less error-prone. Either way, it's good to know how to generate a schema programatically, but you should not mix the two!

    0 讨论(0)
提交回复
热议问题