1 | import { GraphQLSchema } from 'graphql';
|
2 | import { GraphQLDeferDirective, GraphQLStreamDirective, } from '@graphql-tools/utils';
|
3 | import { DeferStreamDirectiveLabelRule } from './validations/defer-stream-directive-label.js';
|
4 | import { DeferStreamDirectiveOnRootFieldRule } from './validations/defer-stream-directive-on-root-field.js';
|
5 | import { OverlappingFieldsCanBeMergedRule } from './validations/overlapping-fields-can-be-merged.js';
|
6 | import { StreamDirectiveOnListFieldRule } from './validations/stream-directive-on-list-field.js';
|
7 | export function useDeferStream() {
|
8 | return {
|
9 | onSchemaChange: ({ schema, replaceSchema, }) => {
|
10 | const directives = [];
|
11 | const deferInSchema = schema.getDirective('defer');
|
12 | if (deferInSchema == null) {
|
13 | directives.push(GraphQLDeferDirective);
|
14 | }
|
15 | const streamInSchema = schema.getDirective('stream');
|
16 | if (streamInSchema == null) {
|
17 | directives.push(GraphQLStreamDirective);
|
18 | }
|
19 | if (directives.length) {
|
20 | replaceSchema(new GraphQLSchema({
|
21 | ...schema.toConfig(),
|
22 | directives: [...schema.getDirectives(), ...directives],
|
23 | }));
|
24 | }
|
25 | },
|
26 | onValidate: ({ params, addValidationRule, }) => {
|
27 |
|
28 | params.rules = params.rules || [];
|
29 | params.rules = params.rules.filter((rule) => rule.name !== 'OverlappingFieldsCanBeMergedRule');
|
30 | addValidationRule(OverlappingFieldsCanBeMergedRule);
|
31 | addValidationRule(DeferStreamDirectiveLabelRule);
|
32 | addValidationRule(DeferStreamDirectiveOnRootFieldRule);
|
33 | addValidationRule(StreamDirectiveOnListFieldRule);
|
34 | },
|
35 | };
|
36 | }
|