Search code examples
javascriptgraphqlapollomutation

GraphQLError: Expected type \"StatusInput\" to be an object


I can create a project in Apollo Sandbox - below is the copy of Project copy from Apollo Sandbox

{"data": {"createProject": {"_id": "65952c08576a8d317dc4df47", "description": "front end", "name": "new react", status": [{"code": "a", "launch": "c", "content": "b", "planning": "d","qa": "f", "start": "g", "ux": "h" }]}}

however I added mutation on client side as per below

mutation createProject($name: String! $description: String! $status: [StatusInput]!) {    createProject(name: $name, description: $description, status: $status) { _id name description status {start planning ux content code qa launch }}}

can below is the useQuery code for the submit handler

const handleSubmit = async (event) => {
  event.preventDefault();

  try {
    const mutationRes = await createProject({
      variables: {
        name: formState.name,
        description: formState.description,
        status: formState.status,
      },
    });

    console.log(mutationRes);
  } catch (err) {
    console.log(err);
  }
};

I am able to see the response in network tab of name, description and status but I get 400 status code stating that GraphQLError: Expected type "StatusInput" to be an object."

Server Side typeDef: Input

input StatusInput { start: String! planning: String! ux: String! content: String! code: String! qa: String! launch: String! project: ID! }

and Mutation

createProject( name: String! description: String! status: [StatusInput]! ): Project

Solution

  • I changed schema

        status: {
        type: String,
        enum: ["start", "planning", "ux", "content", "code", "qa", "launch"],
      },
    

    type def

    enum Status {
    start
    planning
    ux
    content
    cde
    qa
    launch
    

    }

      type Project {
    _id: ID!
    name: String!
    description: String!
    status: Status!
    }
    

    with enum I was able to assign project status and solved the problem - for any more info please feel free to contact me