Skip to content

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

@alakdam07

Description

@alakdam07

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions