Skip to content

Commit

Permalink
docs: update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
JCMais committed May 3, 2020
1 parent 3aaa53b commit fc28b0e
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ const schemaWithMiddleware = applyMiddleware(schema, yupMiddleware());

### Setting the Validation Schema of each Mutation

For each mutation that you want to validate the args, you must define a property `validationSchema` when defining it. Example:
For each mutation that you want to validate the args, you must define the validation schema on the definition of the mutation. The way to do that depends on the version of GraphQL you are using:

#### GraphQL <= 14

```ts
const resolvers = {
Expand All @@ -178,6 +180,26 @@ const resolvers = {
};
```

#### GraphQL >= 15

```ts
const resolvers = {
// ...
Mutation: {
AddUser: {
extensions: {
yupMiddleware: {
validationSchema: yupSchemaHere,
},
},
resolve: async (root, args, context, info) => {
// ...
},
},
},
};
```

You can also pass another property named `validationOptions` to pass
other [options](#options) that should only be used for this mutation.

Expand All @@ -195,7 +217,7 @@ export default mutationWithClientMutationId({
// ...
}),
}),
mutateAndGetPayload: async args => {
mutateAndGetPayload: async (args) => {
// ...
},
outputFields: {
Expand All @@ -209,14 +231,15 @@ This will:
```js
const mutation = mutationWithClientMutationId({
name: 'MyMutation',
mutateAndGetPayload: async args => {
mutateAndGetPayload: async (args) => {
// ...
},
outputFields: {
// ...
},
});

// GraphQL <= 14
export default {
...mutation,
validationSchema: yup.object().shape({
Expand All @@ -225,6 +248,21 @@ export default {
}),
}),
};

// GraphQL >= 15
export default {
...mutation,
extensions: {
...mutation.extensions,
yupMiddleware: {
validationSchema: yup.object().shape({
input: yup.object().shape({
// ...
}),
}),
},
},
};
```

[graphql]: https://github.com/graphql/graphql-js
Expand Down

0 comments on commit fc28b0e

Please sign in to comment.