@jcm/nexus-plugin-yup-validation

Allows to use Yup schemas to validate GraphQL arguments

Usage no npm install needed!

<script type="module">
  import jcmNexusPluginYupValidation from 'https://cdn.skypack.dev/@jcm/nexus-plugin-yup-validation';
</script>

README

@jcm/nexus-plugin-yup-validation

Patreon Logo
Discord Logo

This plugin allows to set a yup property on some field (Mutations are the only ones supported at the moment), and it will validate the arguments passed to the field using the given yup schema. It's inspired by something I wrote a long time ago: GraphQL Mutation Arguments Validation with Yup using graphql-middleware

Sample usage:

const AddUserMutation = mutationField((t) => {
  t.field('addUser', {
    type: AddUserPayload,
    args: {
      email: stringArg(),
    },
    yup: {
      schema: yup.object({
        email: yup.string().email().required(),
      }),
    },
    // ...
  })
})

The default errorPayloadBuilder assumes that the mutation output type has a field named error which is an object with the following structure:

{
  message: string,
  details: ErrorDetail[]
}

// ErrorDetails:
{
  path: string[],
  messages: string[]
}