Apollo-client: Mutation that passes a array list of objects

Created on 31 Aug 2016  路  5Comments  路  Source: apollographql/apollo-client

This is more of a question, but can't find any example or solultion. How to correlated the mutation from the client with the schema or root schema and resolver on the server with an Array list of objects as an variable parameter?

If I have

mutation: gql`
          mutation updateFoo($input: updateFooMutationListInput!) {
            updateFoo(input: $input)
          }
        `,
        variables: {
          input: {
            id,
          },
        },

and in my rootSchema I have

const rootSchema = [`
  type Query {
    foos: [Foo],
  }
  type Mutation {
    updateFoo(updateFooMutationListInput: [Object]): Boolean
  }
  schema {
    query: Query,
    mutation: Mutation
  }
`];

updateFooMutationListInput will be an array of objects. How do I define or reference this input type in place of [Object]?

[Object] does not work and throws error that Object is not a valid type. I also try to to give it a defType FooList

const fooSchema = [`
  type FooList {
   id: ID
  }
`];

 updateFoo(updateFooMutationListInput: [FooList]): Boolean

but this doesn't work and throw error of expecting an Input type. So I change type to input

  input FooList {
   id: ID
  }

but got the same error. This is my mutation resolver

    async updateFoo(root, { updateFooMutationListInput }, context) {
      updateFooMutationListInput.map((foo) => {
        Foo.update({
          _id: foo.id
        },{
          $set: {
            foo_task_done: true
          }
        });
      });
      return true;
    }

Some refereces I checked out was http://docs.apollostack.com/graphql/queries.html and
http://docs.apollostack.com/graphql/schemas.html and
http://stackoverflow.com/questions/32304486/how-to-make-a-mutation-query-for-inserting-a-list-of-array-fields-in-graphql

Can someone point me in a good direction or provide a simple example of passing an array of objects to a mutation with Apollo?

Most helpful comment

Yes I had to break down any type I was putting in the input type also as aninput type. Very nice ...thanks for the guidance!

All 5 comments

You need to define an input type: http://docs.apollostack.com/graphql/schemas.html#input-objects

Right there is no way to reuse regular object types for input as well, so you often need to define two similar types about the same object.

Okay thanks. You mean when I tried

 input FooList {
   id: ID
  }


  type Mutation {
    updateFoo(updateFooMutationListInput: [FooList]): Boolean
  }

I'm not sure where to put this, I've tried it in the rootSchema and in the fooSchema and still get error Expected Input type

From the docs

input AvatarOptions {
  format: String
  filter: String
}
type Person {
  avatar(size: Int = 100, options: AvatarOptions): String
}

where format is a String and filter is a String scalar, do they have to be scalar or can they be types?

Yes I had to break down any type I was putting in the input type also as aninput type. Very nice ...thanks for the guidance!

For what it's worth, the link to the docs no longer works. I searched for input-objects, and the only result I got was for iOS. I've now spent several hours researching how to do what in REST is incredibly simple (pass an array of objects to the server - in this case, as a graphql mutation.)

It would be great to see the docs updated with an example of how to do this. Even the graphql docs themselves don't really explain how to do this directly.

Was this page helpful?
0 / 5 - 0 ratings