@omnigraph/json-schema 1.0.0-alpha-20220804093904-8e2e41f7f → 1.0.0-alpha-20230420181317-a95037648
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/cjs/addExecutionLogicToComposer.js +208 -0
- package/cjs/addRootFieldResolver.js +395 -0
- package/cjs/bundle.js +65 -0
- package/cjs/directives.js +580 -0
- package/cjs/getComposerFromJSONSchema.js +1297 -0
- package/cjs/getDereferencedJSONSchemaFromOperations.js +33 -0
- package/cjs/getGraphQLSchemaFromDereferencedJSONSchema.js +39 -0
- package/cjs/getJSONSchemaStringFormatScalarMap.js +61 -0
- package/cjs/getReferencedJSONSchemaFromOperations.js +260 -0
- package/cjs/getTypeResolverFromOutputTCs.js +69 -0
- package/cjs/getUnionTypeComposers.js +85 -0
- package/cjs/getValidTypeName.js +20 -0
- package/cjs/index.js +14 -0
- package/cjs/loadGraphQLSchemaFromJSONSchemas.js +59 -0
- package/cjs/package.json +1 -0
- package/cjs/resolveDataByUnionInputType.js +40 -0
- package/cjs/scalars.js +30 -0
- package/cjs/types.js +0 -0
- package/cjs/utils.js +65 -0
- package/esm/addExecutionLogicToComposer.js +204 -0
- package/esm/addRootFieldResolver.js +390 -0
- package/esm/bundle.js +60 -0
- package/esm/directives.js +565 -0
- package/esm/getComposerFromJSONSchema.js +1293 -0
- package/esm/getDereferencedJSONSchemaFromOperations.js +29 -0
- package/esm/getGraphQLSchemaFromDereferencedJSONSchema.js +35 -0
- package/esm/getJSONSchemaStringFormatScalarMap.js +56 -0
- package/esm/getReferencedJSONSchemaFromOperations.js +255 -0
- package/esm/getTypeResolverFromOutputTCs.js +65 -0
- package/esm/getUnionTypeComposers.js +80 -0
- package/esm/getValidTypeName.js +16 -0
- package/esm/index.js +9 -0
- package/esm/loadGraphQLSchemaFromJSONSchemas.js +54 -0
- package/esm/resolveDataByUnionInputType.js +36 -0
- package/esm/scalars.js +27 -0
- package/esm/types.js +0 -0
- package/esm/utils.js +57 -0
- package/package.json +34 -27
- package/typings/addExecutionLogicToComposer.d.cts +14 -0
- package/typings/addExecutionLogicToComposer.d.ts +14 -0
- package/typings/addRootFieldResolver.d.cts +26 -0
- package/typings/addRootFieldResolver.d.ts +26 -0
- package/typings/bundle.d.cts +34 -0
- package/typings/bundle.d.ts +34 -0
- package/typings/directives.d.cts +54 -0
- package/typings/directives.d.ts +54 -0
- package/typings/getComposerFromJSONSchema.d.cts +13 -0
- package/{getComposerFromJSONSchema.d.ts → typings/getComposerFromJSONSchema.d.ts} +4 -1
- package/typings/getDereferencedJSONSchemaFromOperations.d.cts +14 -0
- package/{getDereferencedJSONSchemaFromOperations.d.ts → typings/getDereferencedJSONSchemaFromOperations.d.ts} +5 -3
- package/typings/getGraphQLSchemaFromDereferencedJSONSchema.d.cts +5 -0
- package/typings/getGraphQLSchemaFromDereferencedJSONSchema.d.ts +5 -0
- package/typings/getJSONSchemaStringFormatScalarMap.d.cts +2 -0
- package/typings/getJSONSchemaStringFormatScalarMap.d.ts +2 -0
- package/typings/getReferencedJSONSchemaFromOperations.d.cts +16 -0
- package/{getReferencedJSONSchemaFromOperations.d.ts → typings/getReferencedJSONSchemaFromOperations.d.ts} +6 -4
- package/typings/getTypeResolverFromOutputTCs.d.cts +7 -0
- package/typings/getTypeResolverFromOutputTCs.d.ts +7 -0
- package/typings/getUnionTypeComposers.d.cts +24 -0
- package/typings/getUnionTypeComposers.d.ts +24 -0
- package/{getStringScalarWithMinMaxLength.d.ts → typings/getValidTypeName.d.cts} +4 -3
- package/typings/index.d.cts +9 -0
- package/typings/index.d.ts +9 -0
- package/typings/loadGraphQLSchemaFromJSONSchemas.d.cts +3 -0
- package/typings/loadGraphQLSchemaFromJSONSchemas.d.ts +3 -0
- package/typings/resolveDataByUnionInputType.d.cts +2 -0
- package/typings/resolveDataByUnionInputType.d.ts +2 -0
- package/typings/scalars.d.cts +4 -0
- package/typings/scalars.d.ts +4 -0
- package/typings/types.d.cts +76 -0
- package/typings/types.d.ts +76 -0
- package/typings/utils.d.cts +15 -0
- package/{utils.d.ts → typings/utils.d.ts} +1 -1
- package/addExecutionLogicToComposer.d.ts +0 -15
- package/bundle.d.ts +0 -39
- package/getGenericJSONScalar.d.ts +0 -8
- package/getGraphQLSchemaFromDereferencedJSONSchema.d.ts +0 -3
- package/getJSONSchemaStringFormatScalarMap.d.ts +0 -3
- package/getTypeResolverFromOutputTCs.d.ts +0 -4
- package/getUnionTypeComposers.d.ts +0 -17
- package/getValidateFnForSchemaPath.d.ts +0 -3
- package/index.d.ts +0 -9
- package/index.js +0 -1908
- package/index.mjs +0 -1895
- package/loadGraphQLSchemaFromJSONSchemas.d.ts +0 -2
- package/resolveDataByUnionInputType.d.ts +0 -3
- package/types.d.ts +0 -62
- package/{getValidTypeName.d.ts → typings/getValidTypeName.d.ts} +1 -1
package/cjs/bundle.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getGraphQLSchemaFromBundle = exports.createBundleFromDereferencedSchema = void 0;
|
|
4
|
+
const json_machete_1 = require("json-machete");
|
|
5
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
6
|
+
const fetch_1 = require("@whatwg-node/fetch");
|
|
7
|
+
const getGraphQLSchemaFromDereferencedJSONSchema_js_1 = require("./getGraphQLSchemaFromDereferencedJSONSchema.js");
|
|
8
|
+
async function createBundleFromDereferencedSchema(name, { dereferencedSchema, endpoint, operations, operationHeaders, logger = new utils_1.DefaultLogger(name), }) {
|
|
9
|
+
logger.debug(`Creating references from dereferenced schema`);
|
|
10
|
+
const referencedSchema = await (0, json_machete_1.referenceJSONSchema)(dereferencedSchema);
|
|
11
|
+
logger.debug(`Bundle generation finished`);
|
|
12
|
+
return {
|
|
13
|
+
name,
|
|
14
|
+
endpoint,
|
|
15
|
+
operations,
|
|
16
|
+
operationHeaders: typeof operationHeaders === 'object' ? operationHeaders : {},
|
|
17
|
+
referencedSchema,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
exports.createBundleFromDereferencedSchema = createBundleFromDereferencedSchema;
|
|
21
|
+
/**
|
|
22
|
+
* Generates a local GraphQLSchema instance from
|
|
23
|
+
* previously generated JSON Schema bundle
|
|
24
|
+
*/
|
|
25
|
+
async function getGraphQLSchemaFromBundle({ name, endpoint: bundledBaseUrl, operations, operationHeaders: bundledOperationHeaders = {}, referencedSchema, }, { cwd = process.cwd(), logger = new utils_1.DefaultLogger(name), fetch = fetch_1.fetch, endpoint: overwrittenBaseUrl, operationHeaders: additionalOperationHeaders = {}, queryParams, queryStringOptions, } = {}) {
|
|
26
|
+
logger.info(`Dereferencing the bundle`);
|
|
27
|
+
const fullyDeferencedSchema = await (0, json_machete_1.dereferenceObject)(referencedSchema, {
|
|
28
|
+
cwd,
|
|
29
|
+
fetchFn: fetch,
|
|
30
|
+
logger,
|
|
31
|
+
});
|
|
32
|
+
const endpoint = overwrittenBaseUrl || bundledBaseUrl;
|
|
33
|
+
let operationHeaders = {};
|
|
34
|
+
if (typeof additionalOperationHeaders === 'function') {
|
|
35
|
+
operationHeaders = async (resolverData, operationConfig) => {
|
|
36
|
+
const result = await additionalOperationHeaders(resolverData, {
|
|
37
|
+
endpoint,
|
|
38
|
+
field: operationConfig.field,
|
|
39
|
+
method: 'method' in operationConfig ? operationConfig.method : 'POST',
|
|
40
|
+
path: 'path' in operationConfig ? operationConfig.path : operationConfig.pubsubTopic,
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
...bundledOperationHeaders,
|
|
44
|
+
...result,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
operationHeaders = {
|
|
50
|
+
...bundledOperationHeaders,
|
|
51
|
+
...additionalOperationHeaders,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
logger.info(`Creating the GraphQL Schema from dereferenced schema`);
|
|
55
|
+
return (0, getGraphQLSchemaFromDereferencedJSONSchema_js_1.getGraphQLSchemaFromDereferencedJSONSchema)(name, {
|
|
56
|
+
fullyDeferencedSchema,
|
|
57
|
+
logger,
|
|
58
|
+
endpoint,
|
|
59
|
+
operations,
|
|
60
|
+
operationHeaders,
|
|
61
|
+
queryParams,
|
|
62
|
+
queryStringOptions,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
exports.getGraphQLSchemaFromBundle = getGraphQLSchemaFromBundle;
|
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExampleDirective = exports.OneOfDirective = exports.EnumDirective = exports.StatusCodeTypeNameDirective = exports.processDirectives = exports.processDictionaryDirective = exports.DictionaryDirective = exports.processLinkFieldAnnotations = exports.LinkResolverDirective = exports.LinkDirective = exports.processResponseMetadataAnnotations = exports.ResponseMetadataDirective = exports.GlobalOptionsDirective = exports.HTTPOperationDirective = exports.processScalarType = exports.processTypeScriptAnnotations = exports.TypeScriptDirective = exports.processPubSubOperationAnnotations = exports.PubSubOperationDirective = exports.processRegExpAnnotations = exports.RegExpDirective = exports.processResolveRootFieldAnnotations = exports.ResolveRootFieldDirective = exports.processResolveRootAnnotations = exports.ResolveRootDirective = exports.processDiscriminatorAnnotations = exports.DiscriminatorDirective = exports.processLengthAnnotations = exports.LengthDirective = void 0;
|
|
4
|
+
const dset_1 = require("dset");
|
|
5
|
+
const graphql_1 = require("graphql");
|
|
6
|
+
const graphql_scalars_1 = require("graphql-scalars");
|
|
7
|
+
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
|
|
8
|
+
const utils_1 = require("@graphql-tools/utils");
|
|
9
|
+
const addRootFieldResolver_js_1 = require("./addRootFieldResolver.js");
|
|
10
|
+
const getTypeResolverFromOutputTCs_js_1 = require("./getTypeResolverFromOutputTCs.js");
|
|
11
|
+
const scalars_js_1 = require("./scalars.js");
|
|
12
|
+
exports.LengthDirective = new graphql_1.GraphQLDirective({
|
|
13
|
+
name: 'length',
|
|
14
|
+
locations: [graphql_1.DirectiveLocation.SCALAR],
|
|
15
|
+
args: {
|
|
16
|
+
min: {
|
|
17
|
+
type: graphql_1.GraphQLInt,
|
|
18
|
+
},
|
|
19
|
+
max: {
|
|
20
|
+
type: graphql_1.GraphQLInt,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
function processLengthAnnotations(scalar, { min: minLength, max: maxLength, }) {
|
|
25
|
+
function coerceString(value) {
|
|
26
|
+
if (value != null) {
|
|
27
|
+
const vStr = value.toString();
|
|
28
|
+
if (typeof minLength !== 'undefined' && vStr.length < minLength) {
|
|
29
|
+
throw new Error(`${scalar.name} cannot be less than ${minLength} but given ${vStr}`);
|
|
30
|
+
}
|
|
31
|
+
if (typeof maxLength !== 'undefined' && vStr.length > maxLength) {
|
|
32
|
+
throw new Error(`${scalar.name} cannot be more than ${maxLength} but given ${vStr}`);
|
|
33
|
+
}
|
|
34
|
+
return vStr;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
scalar.serialize = coerceString;
|
|
38
|
+
scalar.parseValue = coerceString;
|
|
39
|
+
scalar.parseLiteral = ast => {
|
|
40
|
+
if ('value' in ast) {
|
|
41
|
+
return coerceString(ast.value);
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
exports.processLengthAnnotations = processLengthAnnotations;
|
|
47
|
+
exports.DiscriminatorDirective = new graphql_1.GraphQLDirective({
|
|
48
|
+
name: 'discriminator',
|
|
49
|
+
locations: [graphql_1.DirectiveLocation.INTERFACE, graphql_1.DirectiveLocation.UNION],
|
|
50
|
+
args: {
|
|
51
|
+
field: {
|
|
52
|
+
type: graphql_1.GraphQLString,
|
|
53
|
+
},
|
|
54
|
+
mapping: {
|
|
55
|
+
type: scalars_js_1.ObjMapScalar,
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
function processDiscriminatorAnnotations({ interfaceType, discriminatorFieldName, }) {
|
|
60
|
+
interfaceType.resolveType = root => root[discriminatorFieldName];
|
|
61
|
+
}
|
|
62
|
+
exports.processDiscriminatorAnnotations = processDiscriminatorAnnotations;
|
|
63
|
+
exports.ResolveRootDirective = new graphql_1.GraphQLDirective({
|
|
64
|
+
name: 'resolveRoot',
|
|
65
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
|
66
|
+
});
|
|
67
|
+
function processResolveRootAnnotations(field) {
|
|
68
|
+
field.resolve = root => root;
|
|
69
|
+
}
|
|
70
|
+
exports.processResolveRootAnnotations = processResolveRootAnnotations;
|
|
71
|
+
exports.ResolveRootFieldDirective = new graphql_1.GraphQLDirective({
|
|
72
|
+
name: 'resolveRootField',
|
|
73
|
+
locations: [
|
|
74
|
+
graphql_1.DirectiveLocation.FIELD_DEFINITION,
|
|
75
|
+
graphql_1.DirectiveLocation.ARGUMENT_DEFINITION,
|
|
76
|
+
graphql_1.DirectiveLocation.INPUT_FIELD_DEFINITION,
|
|
77
|
+
],
|
|
78
|
+
args: {
|
|
79
|
+
field: {
|
|
80
|
+
type: graphql_1.GraphQLString,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
function isOriginallyListType(type) {
|
|
85
|
+
if ((0, graphql_1.isNonNullType)(type)) {
|
|
86
|
+
return isOriginallyListType(type.ofType);
|
|
87
|
+
}
|
|
88
|
+
return (0, graphql_1.isListType)(type);
|
|
89
|
+
}
|
|
90
|
+
function processResolveRootFieldAnnotations(field, propertyName) {
|
|
91
|
+
if (!field.resolve || field.resolve.name === 'defaultFieldResolver') {
|
|
92
|
+
field.resolve = (root, args, context, info) => {
|
|
93
|
+
const actualFieldObj = root[propertyName];
|
|
94
|
+
if (actualFieldObj != null) {
|
|
95
|
+
const isArray = Array.isArray(actualFieldObj);
|
|
96
|
+
const isListType = isOriginallyListType(info.returnType);
|
|
97
|
+
if (isListType && !isArray) {
|
|
98
|
+
return [actualFieldObj];
|
|
99
|
+
}
|
|
100
|
+
else if (!isListType && isArray) {
|
|
101
|
+
return actualFieldObj[0];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return actualFieldObj;
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
exports.processResolveRootFieldAnnotations = processResolveRootFieldAnnotations;
|
|
109
|
+
exports.RegExpDirective = new graphql_1.GraphQLDirective({
|
|
110
|
+
name: 'regexp',
|
|
111
|
+
locations: [graphql_1.DirectiveLocation.SCALAR],
|
|
112
|
+
args: {
|
|
113
|
+
pattern: {
|
|
114
|
+
type: graphql_1.GraphQLString,
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
});
|
|
118
|
+
function processRegExpAnnotations(scalar, pattern) {
|
|
119
|
+
function coerceString(value) {
|
|
120
|
+
if (value != null) {
|
|
121
|
+
const vStr = value.toString();
|
|
122
|
+
const regexp = new RegExp(pattern);
|
|
123
|
+
if (!regexp.test(vStr)) {
|
|
124
|
+
throw new Error(`${scalar.name} must match ${pattern} but given ${vStr}`);
|
|
125
|
+
}
|
|
126
|
+
return vStr;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
scalar.serialize = coerceString;
|
|
130
|
+
scalar.parseValue = coerceString;
|
|
131
|
+
scalar.parseLiteral = ast => {
|
|
132
|
+
if ('value' in ast) {
|
|
133
|
+
return coerceString(ast.value);
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
exports.processRegExpAnnotations = processRegExpAnnotations;
|
|
139
|
+
exports.PubSubOperationDirective = new graphql_1.GraphQLDirective({
|
|
140
|
+
name: 'pubsubOperation',
|
|
141
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
|
142
|
+
args: {
|
|
143
|
+
pubsubTopic: {
|
|
144
|
+
type: graphql_1.GraphQLString,
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
function processPubSubOperationAnnotations({ field, globalPubsub, pubsubTopic, logger, }) {
|
|
149
|
+
field.subscribe = (root, args, context, info) => {
|
|
150
|
+
const operationLogger = logger.child(`${info.parentType.name}.${field.name}`);
|
|
151
|
+
const pubsub = (context === null || context === void 0 ? void 0 : context.pubsub) || globalPubsub;
|
|
152
|
+
if (!pubsub) {
|
|
153
|
+
return (0, utils_1.createGraphQLError)(`You should have PubSub defined in either the config or the context!`);
|
|
154
|
+
}
|
|
155
|
+
const interpolationData = { root, args, context, info, env: process.env };
|
|
156
|
+
let interpolatedPubSubTopic = string_interpolation_1.stringInterpolator.parse(pubsubTopic, interpolationData);
|
|
157
|
+
if (interpolatedPubSubTopic.startsWith('webhook:')) {
|
|
158
|
+
const [, expectedMethod, expectedUrl] = interpolatedPubSubTopic.split(':');
|
|
159
|
+
const expectedPath = new URL(expectedUrl, 'http://localhost').pathname;
|
|
160
|
+
interpolatedPubSubTopic = `webhook:${expectedMethod}:${expectedPath}`;
|
|
161
|
+
}
|
|
162
|
+
operationLogger.debug(`=> Subscribing to pubSubTopic: ${interpolatedPubSubTopic}`);
|
|
163
|
+
return pubsub.asyncIterator(interpolatedPubSubTopic);
|
|
164
|
+
};
|
|
165
|
+
field.resolve = (root, args, context, info) => {
|
|
166
|
+
const operationLogger = logger.child(`${info.parentType.name}.${field.name}`);
|
|
167
|
+
operationLogger.debug('Received ', root, ' from ', pubsubTopic);
|
|
168
|
+
return root;
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
exports.processPubSubOperationAnnotations = processPubSubOperationAnnotations;
|
|
172
|
+
exports.TypeScriptDirective = new graphql_1.GraphQLDirective({
|
|
173
|
+
name: 'typescript',
|
|
174
|
+
locations: [graphql_1.DirectiveLocation.SCALAR, graphql_1.DirectiveLocation.ENUM],
|
|
175
|
+
args: {
|
|
176
|
+
type: {
|
|
177
|
+
type: graphql_1.GraphQLString,
|
|
178
|
+
},
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
function processTypeScriptAnnotations(type, typeDefinition) {
|
|
182
|
+
type.extensions = type.extensions || {};
|
|
183
|
+
type.extensions.codegenScalarType = typeDefinition;
|
|
184
|
+
}
|
|
185
|
+
exports.processTypeScriptAnnotations = processTypeScriptAnnotations;
|
|
186
|
+
function addExecutionLogicToScalar(nonExecutableScalar, actualScalar) {
|
|
187
|
+
Object.defineProperties(nonExecutableScalar, {
|
|
188
|
+
serialize: {
|
|
189
|
+
value: actualScalar.serialize,
|
|
190
|
+
},
|
|
191
|
+
parseValue: {
|
|
192
|
+
value: actualScalar.parseValue,
|
|
193
|
+
},
|
|
194
|
+
parseLiteral: {
|
|
195
|
+
value: actualScalar.parseLiteral,
|
|
196
|
+
},
|
|
197
|
+
extensions: {
|
|
198
|
+
value: {
|
|
199
|
+
...actualScalar.extensions,
|
|
200
|
+
...nonExecutableScalar.extensions,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
function processScalarType(schema, type) {
|
|
206
|
+
if (type.name in graphql_scalars_1.resolvers) {
|
|
207
|
+
const actualScalar = graphql_scalars_1.resolvers[type.name];
|
|
208
|
+
addExecutionLogicToScalar(type, actualScalar);
|
|
209
|
+
}
|
|
210
|
+
if (type.name === 'ObjMap') {
|
|
211
|
+
addExecutionLogicToScalar(type, scalars_js_1.ObjMapScalar);
|
|
212
|
+
}
|
|
213
|
+
const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
|
|
214
|
+
for (const directiveAnnotation of directiveAnnotations) {
|
|
215
|
+
switch (directiveAnnotation.name) {
|
|
216
|
+
case 'length':
|
|
217
|
+
processLengthAnnotations(type, directiveAnnotation.args);
|
|
218
|
+
break;
|
|
219
|
+
case 'regexp':
|
|
220
|
+
processRegExpAnnotations(type, directiveAnnotation.args.pattern);
|
|
221
|
+
break;
|
|
222
|
+
case 'typescript':
|
|
223
|
+
processTypeScriptAnnotations(type, directiveAnnotation.args.type);
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
exports.processScalarType = processScalarType;
|
|
229
|
+
exports.HTTPOperationDirective = new graphql_1.GraphQLDirective({
|
|
230
|
+
name: 'httpOperation',
|
|
231
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
|
232
|
+
args: {
|
|
233
|
+
path: {
|
|
234
|
+
type: graphql_1.GraphQLString,
|
|
235
|
+
},
|
|
236
|
+
operationSpecificHeaders: {
|
|
237
|
+
type: scalars_js_1.ObjMapScalar,
|
|
238
|
+
},
|
|
239
|
+
httpMethod: {
|
|
240
|
+
type: new graphql_1.GraphQLEnumType({
|
|
241
|
+
name: 'HTTPMethod',
|
|
242
|
+
values: {
|
|
243
|
+
GET: { value: 'GET' },
|
|
244
|
+
HEAD: { value: 'HEAD' },
|
|
245
|
+
POST: { value: 'POST' },
|
|
246
|
+
PUT: { value: 'PUT' },
|
|
247
|
+
DELETE: { value: 'DELETE' },
|
|
248
|
+
CONNECT: { value: 'CONNECT' },
|
|
249
|
+
OPTIONS: { value: 'OPTIONS' },
|
|
250
|
+
TRACE: { value: 'TRACE' },
|
|
251
|
+
PATCH: { value: 'PATCH' },
|
|
252
|
+
},
|
|
253
|
+
}),
|
|
254
|
+
},
|
|
255
|
+
isBinary: {
|
|
256
|
+
type: graphql_1.GraphQLBoolean,
|
|
257
|
+
},
|
|
258
|
+
requestBaseBody: {
|
|
259
|
+
type: scalars_js_1.ObjMapScalar,
|
|
260
|
+
},
|
|
261
|
+
queryParamArgMap: {
|
|
262
|
+
type: scalars_js_1.ObjMapScalar,
|
|
263
|
+
},
|
|
264
|
+
queryStringOptionsByParam: {
|
|
265
|
+
type: scalars_js_1.ObjMapScalar,
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
exports.GlobalOptionsDirective = new graphql_1.GraphQLDirective({
|
|
270
|
+
name: 'globalOptions',
|
|
271
|
+
locations: [graphql_1.DirectiveLocation.OBJECT],
|
|
272
|
+
args: {
|
|
273
|
+
sourceName: {
|
|
274
|
+
type: graphql_1.GraphQLString,
|
|
275
|
+
},
|
|
276
|
+
endpoint: {
|
|
277
|
+
type: graphql_1.GraphQLString,
|
|
278
|
+
},
|
|
279
|
+
operationHeaders: {
|
|
280
|
+
type: scalars_js_1.ObjMapScalar,
|
|
281
|
+
},
|
|
282
|
+
queryStringOptions: {
|
|
283
|
+
type: scalars_js_1.ObjMapScalar,
|
|
284
|
+
},
|
|
285
|
+
queryParams: {
|
|
286
|
+
type: scalars_js_1.ObjMapScalar,
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
});
|
|
290
|
+
exports.ResponseMetadataDirective = new graphql_1.GraphQLDirective({
|
|
291
|
+
name: 'responseMetadata',
|
|
292
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
|
293
|
+
});
|
|
294
|
+
function processResponseMetadataAnnotations(field) {
|
|
295
|
+
field.resolve = function responseMetadataResolver(root) {
|
|
296
|
+
return {
|
|
297
|
+
url: root.$url,
|
|
298
|
+
headers: root.$response.header,
|
|
299
|
+
method: root.$method,
|
|
300
|
+
status: root.$statusCode,
|
|
301
|
+
statusText: root.$statusText,
|
|
302
|
+
body: root.$response.body,
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
exports.processResponseMetadataAnnotations = processResponseMetadataAnnotations;
|
|
307
|
+
exports.LinkDirective = new graphql_1.GraphQLDirective({
|
|
308
|
+
name: 'link',
|
|
309
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
|
310
|
+
args: {
|
|
311
|
+
defaultRootType: {
|
|
312
|
+
type: graphql_1.GraphQLString,
|
|
313
|
+
},
|
|
314
|
+
defaultField: {
|
|
315
|
+
type: graphql_1.GraphQLString,
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
exports.LinkResolverDirective = new graphql_1.GraphQLDirective({
|
|
320
|
+
name: 'linkResolver',
|
|
321
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
|
322
|
+
args: {
|
|
323
|
+
linkResolverMap: {
|
|
324
|
+
type: scalars_js_1.ObjMapScalar,
|
|
325
|
+
},
|
|
326
|
+
},
|
|
327
|
+
});
|
|
328
|
+
function linkResolver({ linkObjArgs, targetTypeName, targetFieldName }, { root, args, context, info, env }) {
|
|
329
|
+
for (const argKey in linkObjArgs) {
|
|
330
|
+
const argInterpolation = linkObjArgs[argKey];
|
|
331
|
+
const actualValue = typeof argInterpolation === 'string'
|
|
332
|
+
? string_interpolation_1.stringInterpolator.parse(argInterpolation, {
|
|
333
|
+
root,
|
|
334
|
+
args,
|
|
335
|
+
context,
|
|
336
|
+
info,
|
|
337
|
+
env,
|
|
338
|
+
})
|
|
339
|
+
: argInterpolation;
|
|
340
|
+
(0, dset_1.dset)(args, argKey, actualValue);
|
|
341
|
+
}
|
|
342
|
+
const type = info.schema.getType(targetTypeName);
|
|
343
|
+
const field = type.getFields()[targetFieldName];
|
|
344
|
+
return field.resolve(root, args, context, info);
|
|
345
|
+
}
|
|
346
|
+
function getLinkResolverMap(schema, field) {
|
|
347
|
+
const parentFieldLinkResolverDirectives = (0, utils_1.getDirective)(schema, field, 'linkResolver');
|
|
348
|
+
if (parentFieldLinkResolverDirectives === null || parentFieldLinkResolverDirectives === void 0 ? void 0 : parentFieldLinkResolverDirectives.length) {
|
|
349
|
+
const linkResolverMap = parentFieldLinkResolverDirectives[0].linkResolverMap;
|
|
350
|
+
if (linkResolverMap) {
|
|
351
|
+
return linkResolverMap;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
function findLinkResolverMap({ schema, operationType, defaultRootTypeName, defaultFieldName, }) {
|
|
356
|
+
const parentType = schema.getRootType(operationType);
|
|
357
|
+
const parentField = parentType.getFields()[operationType];
|
|
358
|
+
if (parentField) {
|
|
359
|
+
const linkResolverMap = getLinkResolverMap(schema, parentField);
|
|
360
|
+
if (linkResolverMap) {
|
|
361
|
+
return linkResolverMap;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const defaultRootType = schema.getType(defaultRootTypeName);
|
|
365
|
+
if (defaultRootType) {
|
|
366
|
+
const defaultField = defaultRootType.getFields()[defaultFieldName];
|
|
367
|
+
if (defaultField) {
|
|
368
|
+
const linkResolverMap = getLinkResolverMap(schema, defaultField);
|
|
369
|
+
if (linkResolverMap) {
|
|
370
|
+
return linkResolverMap;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function processLinkFieldAnnotations(field, defaultRootTypeName, defaultFieldName) {
|
|
376
|
+
field.resolve = (root, args, context, info) => {
|
|
377
|
+
const linkResolverMap = findLinkResolverMap({
|
|
378
|
+
schema: info.schema,
|
|
379
|
+
defaultRootTypeName,
|
|
380
|
+
defaultFieldName,
|
|
381
|
+
parentFieldName: root.$field,
|
|
382
|
+
operationType: info.operation.operation,
|
|
383
|
+
});
|
|
384
|
+
const linkResolverOpts = linkResolverMap[field.name];
|
|
385
|
+
return linkResolver(linkResolverOpts, { root, args, context, info, env: process.env });
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
exports.processLinkFieldAnnotations = processLinkFieldAnnotations;
|
|
389
|
+
exports.DictionaryDirective = new graphql_1.GraphQLDirective({
|
|
390
|
+
name: 'dictionary',
|
|
391
|
+
locations: [graphql_1.DirectiveLocation.FIELD_DEFINITION],
|
|
392
|
+
});
|
|
393
|
+
function processDictionaryDirective(fieldMap, field) {
|
|
394
|
+
field.resolve = root => {
|
|
395
|
+
const result = [];
|
|
396
|
+
for (const key in root) {
|
|
397
|
+
if (key in fieldMap) {
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
result.push({
|
|
401
|
+
key,
|
|
402
|
+
value: root[key],
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
return result;
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
exports.processDictionaryDirective = processDictionaryDirective;
|
|
409
|
+
function processDirectives({ schema, globalFetch, logger, pubsub, ...extraGlobalOptions }) {
|
|
410
|
+
const nonExecutableObjMapScalar = schema.getType('ObjMap');
|
|
411
|
+
if (nonExecutableObjMapScalar && (0, graphql_1.isScalarType)(nonExecutableObjMapScalar)) {
|
|
412
|
+
addExecutionLogicToScalar(nonExecutableObjMapScalar, scalars_js_1.ObjMapScalar);
|
|
413
|
+
}
|
|
414
|
+
let [globalOptions = {}] = ((0, utils_1.getDirective)(schema, schema.getQueryType(), 'globalOptions') ||
|
|
415
|
+
[]);
|
|
416
|
+
globalOptions = {
|
|
417
|
+
...globalOptions,
|
|
418
|
+
...extraGlobalOptions,
|
|
419
|
+
};
|
|
420
|
+
const typeMap = schema.getTypeMap();
|
|
421
|
+
for (const typeName in typeMap) {
|
|
422
|
+
const type = typeMap[typeName];
|
|
423
|
+
const exampleAnnotations = (0, utils_1.getDirective)(schema, type, 'example');
|
|
424
|
+
if (exampleAnnotations === null || exampleAnnotations === void 0 ? void 0 : exampleAnnotations.length) {
|
|
425
|
+
const examples = [];
|
|
426
|
+
for (const exampleAnnotation of exampleAnnotations) {
|
|
427
|
+
if (exampleAnnotation === null || exampleAnnotation === void 0 ? void 0 : exampleAnnotation.value) {
|
|
428
|
+
examples.push(exampleAnnotation.value);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
type.extensions = type.extensions || {};
|
|
432
|
+
type.extensions.examples = examples;
|
|
433
|
+
}
|
|
434
|
+
if ((0, graphql_1.isScalarType)(type)) {
|
|
435
|
+
processScalarType(schema, type);
|
|
436
|
+
}
|
|
437
|
+
if ((0, graphql_1.isInterfaceType)(type)) {
|
|
438
|
+
const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
|
|
439
|
+
for (const directiveAnnotation of directiveAnnotations) {
|
|
440
|
+
switch (directiveAnnotation.name) {
|
|
441
|
+
case 'discriminator':
|
|
442
|
+
processDiscriminatorAnnotations({
|
|
443
|
+
interfaceType: type,
|
|
444
|
+
discriminatorFieldName: directiveAnnotation.args.field,
|
|
445
|
+
});
|
|
446
|
+
break;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
if ((0, graphql_1.isUnionType)(type)) {
|
|
451
|
+
const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
|
|
452
|
+
let statusCodeTypeNameIndexMap;
|
|
453
|
+
let discriminatorField;
|
|
454
|
+
let discriminatorMapping;
|
|
455
|
+
for (const directiveAnnotation of directiveAnnotations) {
|
|
456
|
+
switch (directiveAnnotation.name) {
|
|
457
|
+
case 'statusCodeTypeName':
|
|
458
|
+
statusCodeTypeNameIndexMap = statusCodeTypeNameIndexMap || {};
|
|
459
|
+
statusCodeTypeNameIndexMap[directiveAnnotation.args.statusCode] =
|
|
460
|
+
directiveAnnotation.args.typeName;
|
|
461
|
+
break;
|
|
462
|
+
case 'discriminator':
|
|
463
|
+
discriminatorField = directiveAnnotation.args.field;
|
|
464
|
+
discriminatorMapping = directiveAnnotation.args.mapping;
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
type.resolveType = (0, getTypeResolverFromOutputTCs_js_1.getTypeResolverFromOutputTCs)({
|
|
469
|
+
possibleTypes: type.getTypes(),
|
|
470
|
+
discriminatorField,
|
|
471
|
+
discriminatorMapping,
|
|
472
|
+
statusCodeTypeNameMap: statusCodeTypeNameIndexMap,
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
if ((0, graphql_1.isEnumType)(type)) {
|
|
476
|
+
const directiveAnnotations = (0, utils_1.getDirectives)(schema, type);
|
|
477
|
+
for (const directiveAnnotation of directiveAnnotations) {
|
|
478
|
+
switch (directiveAnnotation.name) {
|
|
479
|
+
case 'typescript':
|
|
480
|
+
processTypeScriptAnnotations(type, directiveAnnotation.args.type);
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
const enumValues = type.getValues();
|
|
485
|
+
for (const enumValue of enumValues) {
|
|
486
|
+
const directiveAnnotations = (0, utils_1.getDirectives)(schema, enumValue);
|
|
487
|
+
for (const directiveAnnotation of directiveAnnotations) {
|
|
488
|
+
switch (directiveAnnotation.name) {
|
|
489
|
+
case 'enum': {
|
|
490
|
+
const realValue = JSON.parse(directiveAnnotation.args.value);
|
|
491
|
+
enumValue.value = realValue;
|
|
492
|
+
type._valueLookup.set(realValue, enumValue);
|
|
493
|
+
break;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
if ('getFields' in type) {
|
|
500
|
+
const fields = type.getFields();
|
|
501
|
+
for (const fieldName in fields) {
|
|
502
|
+
const field = fields[fieldName];
|
|
503
|
+
const directiveAnnotations = (0, utils_1.getDirectives)(schema, field);
|
|
504
|
+
for (const directiveAnnotation of directiveAnnotations) {
|
|
505
|
+
switch (directiveAnnotation.name) {
|
|
506
|
+
case 'resolveRoot':
|
|
507
|
+
processResolveRootAnnotations(field);
|
|
508
|
+
break;
|
|
509
|
+
case 'resolveRootField':
|
|
510
|
+
processResolveRootFieldAnnotations(field, directiveAnnotation.args.field);
|
|
511
|
+
break;
|
|
512
|
+
case 'pubsubOperation':
|
|
513
|
+
processPubSubOperationAnnotations({
|
|
514
|
+
field: field,
|
|
515
|
+
pubsubTopic: directiveAnnotation.args.pubsubTopic,
|
|
516
|
+
globalPubsub: pubsub,
|
|
517
|
+
logger,
|
|
518
|
+
});
|
|
519
|
+
break;
|
|
520
|
+
case 'httpOperation':
|
|
521
|
+
(0, addRootFieldResolver_js_1.addHTTPRootFieldResolver)(schema, field, logger, globalFetch, directiveAnnotation.args, globalOptions);
|
|
522
|
+
break;
|
|
523
|
+
case 'responseMetadata':
|
|
524
|
+
processResponseMetadataAnnotations(field);
|
|
525
|
+
break;
|
|
526
|
+
case 'link':
|
|
527
|
+
processLinkFieldAnnotations(field, directiveAnnotation.args.defaultRootType, directiveAnnotation.args.defaultField);
|
|
528
|
+
break;
|
|
529
|
+
case 'dictionary':
|
|
530
|
+
processDictionaryDirective(fields, field);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return schema;
|
|
537
|
+
}
|
|
538
|
+
exports.processDirectives = processDirectives;
|
|
539
|
+
exports.StatusCodeTypeNameDirective = new graphql_1.GraphQLDirective({
|
|
540
|
+
name: 'statusCodeTypeName',
|
|
541
|
+
locations: [graphql_1.DirectiveLocation.UNION],
|
|
542
|
+
isRepeatable: true,
|
|
543
|
+
args: {
|
|
544
|
+
typeName: {
|
|
545
|
+
type: graphql_1.GraphQLString,
|
|
546
|
+
},
|
|
547
|
+
statusCode: {
|
|
548
|
+
type: graphql_1.GraphQLID,
|
|
549
|
+
},
|
|
550
|
+
},
|
|
551
|
+
});
|
|
552
|
+
exports.EnumDirective = new graphql_1.GraphQLDirective({
|
|
553
|
+
name: 'enum',
|
|
554
|
+
locations: [graphql_1.DirectiveLocation.ENUM_VALUE],
|
|
555
|
+
args: {
|
|
556
|
+
value: {
|
|
557
|
+
type: graphql_1.GraphQLString,
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
});
|
|
561
|
+
exports.OneOfDirective = new graphql_1.GraphQLDirective({
|
|
562
|
+
name: 'oneOf',
|
|
563
|
+
locations: [graphql_1.DirectiveLocation.OBJECT, graphql_1.DirectiveLocation.INTERFACE],
|
|
564
|
+
});
|
|
565
|
+
exports.ExampleDirective = new graphql_1.GraphQLDirective({
|
|
566
|
+
name: 'example',
|
|
567
|
+
locations: [
|
|
568
|
+
graphql_1.DirectiveLocation.FIELD_DEFINITION,
|
|
569
|
+
graphql_1.DirectiveLocation.OBJECT,
|
|
570
|
+
graphql_1.DirectiveLocation.INPUT_OBJECT,
|
|
571
|
+
graphql_1.DirectiveLocation.ENUM,
|
|
572
|
+
graphql_1.DirectiveLocation.SCALAR,
|
|
573
|
+
],
|
|
574
|
+
args: {
|
|
575
|
+
value: {
|
|
576
|
+
type: scalars_js_1.ObjMapScalar,
|
|
577
|
+
},
|
|
578
|
+
},
|
|
579
|
+
isRepeatable: true,
|
|
580
|
+
});
|