UNPKG

3.31 kBJavaScriptView Raw
1import { GraphQLDirective, GraphQLList, GraphQLNonNull, GraphQLString } from 'graphql';
2import { defaultStitchingDirectiveOptions } from './defaultStitchingDirectiveOptions.js';
3import { stitchingDirectivesValidator } from './stitchingDirectivesValidator.js';
4import { stitchingDirectivesTransformer } from './stitchingDirectivesTransformer.js';
5export function stitchingDirectives(options = {}) {
6 const finalOptions = {
7 ...defaultStitchingDirectiveOptions,
8 ...options,
9 };
10 const { keyDirectiveName, computedDirectiveName, mergeDirectiveName, canonicalDirectiveName } = finalOptions;
11 const keyDirectiveTypeDefs = /* GraphQL */ `directive @${keyDirectiveName}(selectionSet: String!) on OBJECT`;
12 const computedDirectiveTypeDefs = /* GraphQL */ `directive @${computedDirectiveName}(selectionSet: String!) on FIELD_DEFINITION`;
13 const mergeDirectiveTypeDefs = /* GraphQL */ `directive @${mergeDirectiveName}(argsExpr: String, keyArg: String, keyField: String, key: [String!], additionalArgs: String) on FIELD_DEFINITION`;
14 const canonicalDirectiveTypeDefs = /* GraphQL */ `directive @${canonicalDirectiveName} on OBJECT | INTERFACE | INPUT_OBJECT | UNION | ENUM | SCALAR | FIELD_DEFINITION | INPUT_FIELD_DEFINITION`;
15 const keyDirective = new GraphQLDirective({
16 name: keyDirectiveName,
17 locations: ['OBJECT'],
18 args: {
19 selectionSet: { type: new GraphQLNonNull(GraphQLString) },
20 },
21 });
22 const computedDirective = new GraphQLDirective({
23 name: computedDirectiveName,
24 locations: ['FIELD_DEFINITION'],
25 args: {
26 selectionSet: { type: new GraphQLNonNull(GraphQLString) },
27 },
28 });
29 const mergeDirective = new GraphQLDirective({
30 name: mergeDirectiveName,
31 locations: ['FIELD_DEFINITION'],
32 args: {
33 argsExpr: { type: GraphQLString },
34 keyArg: { type: GraphQLString },
35 keyField: { type: GraphQLString },
36 key: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)) },
37 additionalArgs: { type: GraphQLString },
38 },
39 });
40 const canonicalDirective = new GraphQLDirective({
41 name: canonicalDirectiveName,
42 locations: [
43 'OBJECT',
44 'INTERFACE',
45 'INPUT_OBJECT',
46 'UNION',
47 'ENUM',
48 'SCALAR',
49 'FIELD_DEFINITION',
50 'INPUT_FIELD_DEFINITION',
51 ],
52 });
53 const allStitchingDirectivesTypeDefs = [
54 keyDirectiveTypeDefs,
55 computedDirectiveTypeDefs,
56 mergeDirectiveTypeDefs,
57 canonicalDirectiveTypeDefs,
58 ].join('\n');
59 return {
60 keyDirectiveTypeDefs,
61 computedDirectiveTypeDefs,
62 mergeDirectiveTypeDefs,
63 canonicalDirectiveTypeDefs,
64 stitchingDirectivesTypeDefs: allStitchingDirectivesTypeDefs,
65 allStitchingDirectivesTypeDefs,
66 keyDirective,
67 computedDirective,
68 mergeDirective,
69 canonicalDirective,
70 allStitchingDirectives: [keyDirective, computedDirective, mergeDirective, canonicalDirective],
71 stitchingDirectivesValidator: stitchingDirectivesValidator(finalOptions),
72 stitchingDirectivesTransformer: stitchingDirectivesTransformer(finalOptions),
73 };
74}