UNPKG

1.85 kBJavaScriptView Raw
1import { GraphQLSchema } from 'graphql';
2import { GraphQLDeferDirective, GraphQLStreamDirective, } from '@graphql-tools/utils';
3import { DeferStreamDirectiveLabelRule } from './validations/defer-stream-directive-label.js';
4import { DeferStreamDirectiveOnRootFieldRule } from './validations/defer-stream-directive-on-root-field.js';
5import { OverlappingFieldsCanBeMergedRule } from './validations/overlapping-fields-can-be-merged.js';
6import { StreamDirectiveOnListFieldRule } from './validations/stream-directive-on-list-field.js';
7export 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 // Just to make TS happy because rules are always defined by useEngine.
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}