Graphql Nested object: “must be Input Type but got”
See original GitHub issueI am trying to do a mutation on Graphql. I want to add another object in my query and mutation. My root query works fine but I had trouble with mutation. I am getting an error. This is the error: “The type of Mutation.addRestaurant(location:) must be Input Type but got: LocationType.”
This is my restaurant query
const restaurantname = new GraphQLObjectType({
name: "RestaurantType",
description: "This is Helsinki's Restuarent",
fields: () => ({
name: { type: GraphQLString },
image: { type: GraphQLString },
address: { type: GraphQLString },
info_url: { type: GraphQLString },
location: { // This is locationtype
type: location,
resolve(parents, args) {
return parents;
}
}
})
});
This is my locationtype object, where I added in restaurant type.
const location = new GraphQLObjectType({
name: "LocationType",
description: "Helsinki's locations with lat and long",
fields: () => ({
lat: { type: GraphQLFloat },
long: { type: GraphQLFloat }
})
});
This is my root query and it works fine:
const query = new GraphQLObjectType({
name: "Rootquery",
description: "This is Rootquery",
fields: () => {
return {
restaurant: {
type: restaurantname,
args: {
name: { type: GraphQLString }
},
resolve(parents, args) {
//return restaurant.models.restaurant.find({ where: args });
return parents;
}
}
};
}
});
This is the mutation which I don’t know how to add locationtype object:
const mutation = new GraphQLObjectType({
name: "Mutation",
fields: () => {
return {
addRestaurant: {
type: restaurantname,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
image: { type: new GraphQLNonNull(GraphQLString) },
address: { type: new GraphQLNonNull(GraphQLString) },
info_url: { type: new GraphQLNonNull(GraphQLString) },
location: { type: new GraphQLNonNull(location) } //
},
resolve(parents, args) {
let restaurantData = new restaurant({
name: args.name,
image: args.image,
address: args.address,
info_url: args.info_url,
location: args.location
});
restaurantData.save();
}
}
};
}
});
This is second time Github issue. So, It looks mess. I am sorry for that.
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
GraphQL Error field type must be Input Type but got
GraphQL Error field type must be Input Type but got: ... In GraphQL, an input cannot be used as a type and a...
Read more >User Daniel Rearden - Stack Exchange
GraphQL Error field type must be Input Type but got: · stackoverflow.com ... Apollo/GraphQL field type for object with dynamic keys · stackoverflow.com....
Read more >GraphQL with express error : Query.example field type must ...
... X must resolve to an Object type at runtime for field Query.user with value · GraphQL Args error: argument type must be...
Read more >Top 10 GraphQL Developer Questions on Stack OverFlow
09, How to query list of objects with array as argument in GraphQL? ... 03, GraphQL Error field type must be input Type...
Read more >Top 10 GraphQL Developer Questions on Stack ... - DocDocGo
10, How do you prevent nested attack on GraphQL/Apollo server? ... 03, GraphQL Error field type must be input Type but got:.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@alakdam07 Check your types const restaurant = require(“./models/restaurant”);
addRestaurant: { type: new GraphQLList(**restaurantname**), args: { name: { type: new GraphQLNonNull(GraphQLString) }, image: { type: new GraphQLNonNull(GraphQLString) }, address: { type: new GraphQLNonNull(GraphQLString) }, info_url: { type: new GraphQLNonNull(GraphQLString) }, location: { type: new GraphQLList(locationInput) } },I think restaurantname is not the type you are importing. Let me know if it helped!Cheers
Had the same problem. The problem is that in the mutation you define address as type, on this line:
address: { type: new GraphQLNonNull(GraphQLString) },It should be defined asinputinstead, making it:address: { input: new GraphQLNonNull(GraphQLString) },