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

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

提交回复
热议问题