question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Graphql Nested object: “must be Input Type but got”

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
carlosmoricommented, Nov 26, 2020

@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

0reactions
georgesamycommented, Jan 28, 2021

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 as input instead, making it: address: { input: new GraphQLNonNull(GraphQLString) },

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found