@jcm/nexus-plugin-relay-mutation

Adds relayMutation(field, config) method to the nexus/schema builder to create Relay compatible mutations

Usage no npm install needed!

<script type="module">
  import jcmNexusPluginRelayMutation from 'https://cdn.skypack.dev/@jcm/nexus-plugin-relay-mutation';
</script>

README

@jcm/nexus-plugin-relay-mutation

Patreon Logo
Discord Logo

This plugin adds the field method relayMutation(fieldName, fieldConfig) to the Nexus Schema Builder, which can be used to create Relay-compliant mutations.

It's based on the mutation helper from graphql-relay.

Sample usage:

const mutation = mutationField((t) => {
  t.relayMutation('addNumbers', {
    inputFields(t2) {
      t2.int('number1', {
        required: true,
      })
      t2.int('number2', {
        required: true,
      })
    },
    outputFields(t2) {
      t2.int('result')
    },
    mutateAndGetPayload(_root, input, _ctx, _info) {
      return {
        result: input.number1 + input.number2,
      }
    },
  })
})

With the above code, the following schema will be generated:

input addNumbersInput {
  number1: Int!
  number2: Int!
  clientMutationId: String
}

type addNumbersPayload {
  result: Int!
  clientMutationId: String
}

type Mutation {
  addNumbers(input: addNumbersInput!): addNumbersPayload!
  # ...
}

# ...