graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Create mutations with custom objects

Open rlgreen91 opened this issue 7 years ago • 2 comments

How exactly do I create a mutation with custom objects? For example, I have the following mutation:

Query = Vsa::Client.parse <<- 'GRAPHQL'
    mutation($driver: Types::DriverInput!, $name: String!) {
      create_driver_event(driver: $driver, name: $name) {}
    }
  GRAPHQL

Right now, I have a class to declare the input type for driver, and when I call the endpoint, I pass along a hash of the keys and values as the variable. However, on staging (Heroku) I run across the following error:

2018-08-16T20:46:30.523524+00:00 app[worker.1]: SyntaxError: /app/app/lib/notification.rb:6: syntax error, unexpected ':', expecting ')'
2018-08-16T20:46:30.523532+00:00 app[worker.1]:     mutation($driver: Types::DriverInput!, $name: ...

What am I missing here? How can I pass along the custom type? Should I just pass it as type hash?

rlgreen91 avatar Aug 16 '18 20:08 rlgreen91

If DriverInput is the name of the custom input object defined on the schema, you could change it to:

Query = Vsa::Client.parse <<- 'GRAPHQL'
    mutation($driver: DriverInput!, $name: String!) {
    create_driver_event(driver: $driver, name: $name) {}
}
GRAPHQL

Suppose DriverInput had a name:String and id:Int, then the variable you pass to Client.query should be:

{ 
 DriverInput: 
  { 
     name: 'example',
     id: 1
  }
}

LucasBCunha avatar Jan 03 '20 22:01 LucasBCunha

I had a hard time with this, but I got it working successfully.

Say you have the following mutation:

  CreateMutation = API::Client.parse <<-'GRAPHQL'
    mutation($input: WidgetCreateInput!) {
        createWidget(input: $input) {
          ...
      }
  GRAPHQL

You would pass the object like so, assuming the WidgetCreateInput has a name and description:

  result = API::Client.query Graph::CreateMutation,
                               variables: { input: {
                                 name: 'foo",
                                 description: 'bar'
                               } }

visoft avatar Dec 05 '20 00:12 visoft