apollo-client: Don't error if optional field not present

Sometimes I have a form with not all fields required, and it will be used as inputs to a mutation.

const query = gql`
  mutation myForm(
    $name: String!
    $tags: [String!]
  ) {
    createSomethingAwesome(
      name: $name
      tags: $tags
    ) {
       id
       someField
    }
`

But I actually need to pass an empty tags to variables. If I pass only variables: { name } it will error.

Should apollo-client take care of this?

Right now I’m doing variables: { tags: [], ...formFields } instead of variables: formFields since formFields won’t have tags key if it is empty.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 2
  • Comments: 16 (10 by maintainers)

Most upvoted comments

@webular it’s a good approach, just tested and it works.

But I usually prefer just use GraphQLInputObjectType when some input just make sense when grouped (like geocode={lat, lon}), but it clearly shows that if you really want this feature, you could get it somehow from the GraphQL specs.

I think I can close it now @stubailo .