2
votes

The result returned from an Apollo mutation is typically a type or a subset of fields from a type, and this is usually great. So my mutation:

const addUserMutation = gql`
  mutation createUser($email: String!, $permissions: [CreatePermissionInput]) {
    createUser(input: {email: $email, permissions: $permissions}) {
      id
      created
      lastUpdated
      uid
      email
      permissions {
        who
        ...
      }
    }
  }
`

Which is calling:

extend type Mutation {
  createUser(input: CreateUserInput!): User
}

Will return the user with the fields listed.

Problem is, I want to know if the user that we just tried to create already existed or not. So how can I edit the response to include this flag? Can you have a mutation return, say, an object like:

{
  exists: true,
  user: { ... }
}

So I can do this:

this.props.submit({
  variables: {
    email,
    permissions,
  },
}).then(({ result }) => {
  console.log(result)
  // > { exists: true, user: [USER OBJECT] }
})

I get that this will break the auto cache update but sometimes you need the response from an update to tell you more.

1

1 Answers

2
votes

Create an additional type for the return result of mutation

type UserPayLoad {
exists:Boolean
user:User
}

extend type Mutation {
  createUser(input: CreateUserInput!): UserPayLoad
}

Just try this. This may help you