1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getJoinDefinitions = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | const mapGetOrSet_1 = require("./utilities/mapGetOrSet");
|
6 | const FieldSetScalar = new graphql_1.GraphQLScalarType({
|
7 | name: 'join__FieldSet',
|
8 | });
|
9 | const JoinGraphDirective = new graphql_1.GraphQLDirective({
|
10 | name: "join__graph",
|
11 | locations: [graphql_1.DirectiveLocation.ENUM_VALUE],
|
12 | args: {
|
13 | name: {
|
14 | type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString),
|
15 | },
|
16 | url: {
|
17 | type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString),
|
18 | },
|
19 | }
|
20 | });
|
21 | function getJoinGraphEnum(serviceList) {
|
22 | const sortedServiceList = serviceList
|
23 | .slice()
|
24 | .sort((a, b) => a.name.localeCompare(b.name));
|
25 | function sanitizeGraphQLName(name) {
|
26 | const alphaNumericUnderscoreOnly = name.replace(/[\W]/g, '_');
|
27 | const noNumericFirstChar = alphaNumericUnderscoreOnly.match(/^\d/)
|
28 | ? '_' + alphaNumericUnderscoreOnly
|
29 | : alphaNumericUnderscoreOnly;
|
30 | const noUnderscoreNumericEnding = noNumericFirstChar.match(/_\d+$/)
|
31 | ? noNumericFirstChar + '_'
|
32 | : noNumericFirstChar;
|
33 | const toUpper = noUnderscoreNumericEnding.toLocaleUpperCase();
|
34 | return toUpper;
|
35 | }
|
36 | const sanitizedNameToServiceDefinitions = new Map();
|
37 | for (const service of sortedServiceList) {
|
38 | const { name } = service;
|
39 | const sanitized = sanitizeGraphQLName(name);
|
40 | (0, mapGetOrSet_1.mapGetOrSet)(sanitizedNameToServiceDefinitions, sanitized, []).push(service);
|
41 | }
|
42 | const enumValueNameToServiceDefinition = Object.create(null);
|
43 | for (const [sanitizedName, services] of sanitizedNameToServiceDefinitions) {
|
44 | if (services.length === 1) {
|
45 | enumValueNameToServiceDefinition[sanitizedName] = services[0];
|
46 | }
|
47 | else {
|
48 | for (const [index, service] of services.entries()) {
|
49 | enumValueNameToServiceDefinition[`${sanitizedName}_${index + 1}`] = service;
|
50 | }
|
51 | }
|
52 | }
|
53 | const entries = Object.entries(enumValueNameToServiceDefinition);
|
54 | return {
|
55 | graphNameToEnumValueName: Object.fromEntries(entries.map(([enumValueName, service]) => [service.name, enumValueName])),
|
56 | JoinGraphEnum: new graphql_1.GraphQLEnumType({
|
57 | name: 'join__Graph',
|
58 | values: Object.fromEntries(entries.map(([enumValueName, service]) => [
|
59 | enumValueName,
|
60 | { value: service },
|
61 | ])),
|
62 | }),
|
63 | };
|
64 | }
|
65 | function getJoinFieldDirective(JoinGraphEnum) {
|
66 | return new graphql_1.GraphQLDirective({
|
67 | name: 'join__field',
|
68 | locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
69 | args: {
|
70 | graph: {
|
71 | type: JoinGraphEnum,
|
72 | },
|
73 | requires: {
|
74 | type: FieldSetScalar,
|
75 | },
|
76 | provides: {
|
77 | type: FieldSetScalar,
|
78 | },
|
79 | },
|
80 | });
|
81 | }
|
82 | function getJoinOwnerDirective(JoinGraphEnum) {
|
83 | return new graphql_1.GraphQLDirective({
|
84 | name: 'join__owner',
|
85 | locations: [graphql_1.DirectiveLocation.OBJECT, graphql_1.DirectiveLocation.INTERFACE],
|
86 | args: {
|
87 | graph: {
|
88 | type: new graphql_1.GraphQLNonNull(JoinGraphEnum),
|
89 | },
|
90 | },
|
91 | });
|
92 | }
|
93 | function getJoinDefinitions(serviceList) {
|
94 | const { graphNameToEnumValueName, JoinGraphEnum } = getJoinGraphEnum(serviceList);
|
95 | const JoinFieldDirective = getJoinFieldDirective(JoinGraphEnum);
|
96 | const JoinOwnerDirective = getJoinOwnerDirective(JoinGraphEnum);
|
97 | const JoinTypeDirective = new graphql_1.GraphQLDirective({
|
98 | name: 'join__type',
|
99 | locations: [graphql_1.DirectiveLocation.OBJECT, graphql_1.DirectiveLocation.INTERFACE],
|
100 | isRepeatable: true,
|
101 | args: {
|
102 | graph: {
|
103 | type: new graphql_1.GraphQLNonNull(JoinGraphEnum),
|
104 | },
|
105 | key: {
|
106 | type: FieldSetScalar,
|
107 | },
|
108 | },
|
109 | });
|
110 | return {
|
111 | graphNameToEnumValueName,
|
112 | FieldSetScalar,
|
113 | JoinTypeDirective,
|
114 | JoinFieldDirective,
|
115 | JoinOwnerDirective,
|
116 | JoinGraphEnum,
|
117 | JoinGraphDirective,
|
118 | };
|
119 | }
|
120 | exports.getJoinDefinitions = getJoinDefinitions;
|
121 |
|
\ | No newline at end of file |