You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(graphql): graphQL custom field complexity and validationRules (#9955)
### What?
Adds the ability to set custom validation rules on the root `graphQL`
config property and the ability to define custom complexity on
relationship, join and upload type fields.
### Why?
**Validation Rules**
These give you the option to add your own validation rules. For example,
you may want to prevent introspection queries in production. You can now
do that with the following:
```ts
import { GraphQL } from '@payloadcms/graphql/types'
import { buildConfig } from 'payload'
export default buildConfig({
// ...
graphQL: {
validationRules: (args) => [
NoProductionIntrospection
]
},
// ...
})
const NoProductionIntrospection: GraphQL.ValidationRule = (context) => ({
Field(node) {
if (process.env.NODE_ENV === 'production') {
if (node.name.value === '__schema' || node.name.value === '__type') {
context.reportError(
new GraphQL.GraphQLError(
'GraphQL introspection is not allowed, but the query contained __schema or __type',
{ nodes: [node] }
)
);
}
}
}
})
```
**Custom field complexity**
You can now increase the complexity of a field, this will help users
from running queries that are too expensive. A higher number will make
the `maxComplexity` trigger sooner.
```ts
const fieldWithComplexity = {
name: 'authors',
type: 'relationship',
relationship: 'authors',
graphQL: {
complexity: 100, // highlight-line
}
}
```
Copy file name to clipboardExpand all lines: docs/fields/relationship.mdx
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,6 +61,7 @@ export const MyRelationshipField: Field = {
61
61
|**`custom`**| Extension point for adding custom data (e.g. for plugins) |
62
62
|**`typescriptSchema`**| Override field type generation with providing a JSON schema |
63
63
|**`virtual`**| Provide `true` to disable field in the database. See [Virtual Fields](https://payloadcms.com/blog/learn-how-virtual-fields-can-help-solve-common-cms-challenges)|
64
+
|**`graphQL`**| Custom graphQL configuration for the field. [More details](/docs/graphql/overview#field-complexity)|
64
65
65
66
_\* An asterisk denotes that a property is required._
Copy file name to clipboardExpand all lines: docs/fields/upload.mdx
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -68,6 +68,7 @@ export const MyUploadField: Field = {
68
68
|**`custom`**| Extension point for adding custom data (e.g. for plugins) |
69
69
|**`typescriptSchema`**| Override field type generation with providing a JSON schema |
70
70
|**`virtual`**| Provide `true` to disable field in the database. See [Virtual Fields](https://payloadcms.com/blog/learn-how-virtual-fields-can-help-solve-common-cms-challenges)|
71
+
|**`graphQL`**| Custom graphQL configuration for the field. [More details](/docs/graphql/overview#field-complexity)|
71
72
72
73
_\* An asterisk denotes that a property is required._
Copy file name to clipboardExpand all lines: docs/graphql/overview.mdx
+50Lines changed: 50 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,7 @@ At the top of your Payload Config you can define all the options to manage Graph
23
23
|`maxComplexity`| A number used to set the maximum allowed complexity allowed by requests [More](/docs/graphql/overview#query-complexity-limits)|
24
24
|`disablePlaygroundInProduction`| A boolean that if false will enable the GraphQL playground, defaults to true. [More](/docs/graphql/overview#graphql-playground)|
25
25
|`disable`| A boolean that if true will disable the GraphQL entirely, defaults to false. |
26
+
|`validationRules`| A function that takes the ExecutionArgs and returns an array of ValidationRules. |
26
27
27
28
## Collections
28
29
@@ -124,6 +125,55 @@ You can even log in using the `login[collection-singular-label-here]` mutation t
124
125
see a ton of detail about how GraphQL operates within Payload.
125
126
</Banner>
126
127
128
+
## Custom Validation Rules
129
+
130
+
You can add custom validation rules to your GraphQL API by defining a `validationRules` function in your Payload Config. This function should return an array of [Validation Rules](https://graphql.org/graphql-js/validation/#validation-rules) that will be applied to all incoming queries and mutations.
if (node.name.value==='__schema'||node.name.value==='__type') {
150
+
context.reportError(
151
+
newGraphQL.GraphQLError(
152
+
'GraphQL introspection is not allowed, but the query contained __schema or __type',
153
+
{ nodes: [node] }
154
+
)
155
+
);
156
+
}
157
+
}
158
+
}
159
+
})
160
+
```
161
+
127
162
## Query complexity limits
128
163
129
164
Payload comes with a built-in query complexity limiter to prevent bad people from trying to slow down your server by running massive queries. To learn more, [click here](/docs/production/preventing-abuse#limiting-graphql-complexity).
165
+
166
+
## Field complexity
167
+
168
+
You can define custom complexity for `relationship`, `upload` and `join` type fields. This is useful if you want to assign a higher complexity to a field that is more expensive to resolve. This can help prevent users from running queries that are too complex.
0 commit comments