@@ -22,10 +22,10 @@ There are multiple ways to make use of the constraint directive in your project.
22
22
23
23
### Schema wrapper
24
24
25
- Implementation based on schema wrappers - basic scalars are wrapped as custom scalars with validations.
25
+ Implementation based on schema wrappers - basic scalars are wrapped as custom scalars with validations.
26
26
27
27
#### Benefits
28
- * based on ` graphql ` library, works everywhere
28
+ * based on ` graphql ` library, works everywhere
29
29
* posibility to also validate GraphQL response data
30
30
31
31
#### Caveats
@@ -81,6 +81,9 @@ function `validateQuery(schema, query, variables, operationName)` can be used to
81
81
Use as an [ Envelop plugin] ( https://www.envelop.dev ) in supported frameworks, e.g. [ GraphQL Yoga] ( https://www.graphql-yoga.com/ ) .
82
82
Functionality is plugged in ` execute ` phase
83
83
84
+ This plugin requires the following dependencies installed in your project:
85
+ * ` @envelop/core ` - ` ^2.0.0 `
86
+
84
87
``` js
85
88
const { createEnvelopQueryValidationPlugin , constraintDirectiveTypeDefs } = require (' graphql-constraint-directive' )
86
89
const express = require (' express' )
@@ -122,6 +125,9 @@ app.listen(4000);
122
125
123
126
As an [ Apollo 3 Server] ( https://www.apollographql.com/docs/apollo-server/v3 ) plugin
124
127
128
+ This plugin requires the following dependencies installed in your project:
129
+ * dependencies required for your selected Apollo Server 3 variant
130
+
125
131
``` js
126
132
const { createApolloQueryValidationPlugin , constraintDirectiveTypeDefs } = require (' graphql-constraint-directive' )
127
133
const express = require (' express' )
@@ -153,7 +159,7 @@ const plugins = [
153
159
]
154
160
155
161
const app = express ()
156
- const server = new ApolloServer ({
162
+ const server = new ApolloServer ({
157
163
schema,
158
164
plugins
159
165
})
@@ -167,13 +173,15 @@ server.applyMiddleware({ app })
167
173
168
174
As an [ Apollo 4 Server] ( https://www.apollographql.com/docs/apollo-server/v4 ) plugin
169
175
176
+ This plugin requires the following dependencies installed in your project:
177
+ * ` @apollo/server ` - ` ^4.0.0 `
178
+ * ` graphql-tag ` - ` ^2.0.0 `
179
+
170
180
``` js
171
181
const { createApollo4QueryValidationPlugin , constraintDirectiveTypeDefs } = require (' graphql-constraint-directive/apollo4' )
172
- const express = require (' express' )
173
182
const { ApolloServer } = require (' @apollo/server' )
183
+ const { startStandaloneServer } = require (' @apollo/server/standalone' );
174
184
const { makeExecutableSchema } = require (' @graphql-tools/schema' )
175
- const cors = require (' cors' )
176
- const { json } = require (' body-parser' )
177
185
178
186
const typeDefs = `
179
187
type Query {
@@ -194,31 +202,25 @@ let schema = makeExecutableSchema({
194
202
})
195
203
196
204
const plugins = [
197
- createApollo4QueryValidationPlugin ({
198
- schema
199
- })
205
+ createApollo4QueryValidationPlugin ()
200
206
]
201
207
202
- const app = express ()
203
- const server = new ApolloServer ({
208
+ const server = new ApolloServer ({
204
209
schema,
205
210
plugins
206
211
})
207
212
208
- await server .start ()
209
-
210
- app .use (
211
- ' /' ,
212
- cors (),
213
- json (),
214
- expressMiddleware (server)
215
- )
213
+ await startStandaloneServer (server);
216
214
```
217
215
#### Apollo 4 Subgraph server
218
216
219
217
There is a small change required to make the Apollo Server quickstart work when trying to build an [ Apollo Subgraph Server] ( https://www.apollographql.com/docs/federation/building-supergraphs/subgraphs-apollo-server/ ) .
220
218
We must use the ` buildSubgraphSchema ` function to build a schema that can be passed to an Apollo Gateway/supergraph, instead of ` makeExecuteableSchema ` . This uses ` makeExecutableSchema ` under the hood.
221
219
220
+ This plugin requires the following dependencies installed in your project:
221
+ * ` @apollo/server ` - ` ^4.0.0 `
222
+ * ` graphql-tag ` - ` ^2.0.0 `
223
+
222
224
``` ts
223
225
import { ApolloServer } from ' @apollo/server' ;
224
226
import { startStandaloneServer } from ' @apollo/server/standalone' ;
@@ -247,9 +249,7 @@ const schema = buildSubgraphSchema({
247
249
});
248
250
249
251
const plugins = [
250
- createApollo4QueryValidationPlugin ({
251
- schema
252
- })
252
+ createApollo4QueryValidationPlugin ()
253
253
]
254
254
255
255
const server = new ApolloServer ({
@@ -262,9 +262,9 @@ await startStandaloneServer(server);
262
262
263
263
#### Express
264
264
265
- * This implementation is untested now, as [ ` express-graphql ` module] ( https://github.com/graphql/express-graphql ) is not maintained anymore.*
265
+ * This implementation is untested now, as [ ` express-graphql ` module] ( https://github.com/graphql/express-graphql ) is not maintained anymore.*
266
266
267
- As a [ Validation rule] ( https://graphql.org/graphql-js/validation/ ) when query ` variables ` are available
267
+ As a [ Validation rule] ( https://graphql.org/graphql-js/validation/ ) when query ` variables ` are available
268
268
269
269
``` js
270
270
const { createQueryValidationRule , constraintDirectiveTypeDefs } = require (' graphql-constraint-directive' )
@@ -411,14 +411,14 @@ app.use('/graphql', bodyParser.json(), graphqlExpress({ schema, formatError }))
411
411
Throws a [` UserInputError` ](https://www.apollographql.com/docs/apollo-server/data/errors/#bad_user_input) for each validation error.
412
412
413
413
#### Apollo Server 4
414
- Throws a prefilled ` GraphQLError` with ` extensions .code ` set to ` BAD_USER_INPUT ` and http status code ` 400 ` .
415
- In case of more validation errors, top level error is generic with ` Query is invalid, for details see extensions .validationErrors ` message,
414
+ Throws a prefilled ` GraphQLError` with ` extensions .code ` set to ` BAD_USER_INPUT ` and http status code ` 400 ` .
415
+ In case of more validation errors, top level error is generic with ` Query is invalid, for details see extensions .validationErrors ` message,
416
416
detailed errors are stored in ` extensions .validationErrors ` of this error.
417
417
418
418
#### Envelop
419
419
The Envelop plugin throws a prefilled ` GraphQLError` for each validation error.
420
420
421
421
### uniqueTypeName
422
422
` ` ` @constraint (uniqueTypeName: " Unique_Type_Name" )` ` `
423
- Override the unique type name generate by the library to the one passed as an argument.
423
+ Override the unique type name generate by the library to the one passed as an argument.
424
424
Has meaning only for ` Schema wrapper` implementation.
0 commit comments