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 GraphQLObjectType.
However I m getting this error :
Error: Agreement.name defined in resolvers, but not in schema
Any idea ?
Here is my Schema definition
const typeDefs = `
type Agreement {
id: Int
}
type Mutation {
agreementsPost(agreement: Agreement) : String
}
`;
And Here is my resolver :
const appResolvers = {
Agreement: new GraphQLObjectType({
name: 'Agreement',
fields: {
id: { type: GraphQLInt },
}
}),
Mutation: {
agreementsPost(root, args) {
return axios.post("....").then(res => res.data);
},
}
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!
Extra data (Related to the error - not to the Q code).
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With