Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

graphql mutation gives syntax error: Expected Name

Tags:

graphql

I am trying to implement mutations with a variable. But I get the following error:

"Syntax Error GraphQL request (3:22) Expected Name, found $

2:     mutation {
3:       createProperty($property) {
                        ^
4:         id
"

My schema definitely doesn't say anything about a name, that's why I think this error is so strange.. I also don't think the documentations about graphql / apollo are very good.

Calling the mutation from client:

const property = {
    title: 'First house',
    cost: 849,
    bedrooms: 3,
    bathrooms: 2,
    car_spaces: 1,
    house_size: 60,
  };

  const createPropertyQuery =
  graphql(gql`
    mutation {
      createProperty($property) {
        id
      }
    }
  `, {
      options: {
        variables: {
          property,
        },
      },
    });


  const { data } = await apolloClient.query({
    query: createPropertyQuery,
  });

Schema:

type Property {
    title: String!
    cost: Float
    user: User
    bedrooms: Int!
    bathrooms: Int!
    car_spaces: Int!
    house_size: Int!
}
input propertyInput {
    title: String!
    cost: Float
    bedrooms: Int!
    bathrooms: Int!
    car_spaces: Int!
    house_size: Int!
}

type RootMutation {
    createProperty (
        property: propertyInput
    ): Property
}
like image 397
Soundwave Avatar asked Jul 30 '17 21:07

Soundwave


1 Answers

You should mention name of the parameter at first!

mutation CreatePropertyMutatuin($property: propertyInput){
  createProperty(property: $property) {
    id
  }
}
like image 168
Mohsen ZareZardeyni Avatar answered Sep 25 '22 17:09

Mohsen ZareZardeyni