@omnigraph/json-schema 1.0.0-alpha-3fc47d119.0 → 1.0.0-alpha-20220804093904-8e2e41f7f
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/getComposerFromJSONSchema.d.ts +3 -3
- package/getGraphQLSchemaFromDereferencedJSONSchema.d.ts +1 -3
- package/getUnionTypeComposers.d.ts +3 -7
- package/index.js +479 -461
- package/index.mjs +479 -461
- package/package.json +6 -6
- package/types.d.ts +0 -1
package/index.mjs
CHANGED
|
@@ -297,8 +297,10 @@ function resolveDataByUnionInputType(data, type, schemaComposer) {
|
|
|
297
297
|
const resolvedData = resolveDataByUnionInputType(data[fieldName], field.type, schemaComposer);
|
|
298
298
|
return resolvedData;
|
|
299
299
|
}
|
|
300
|
+
const fieldData = data[fieldName];
|
|
301
|
+
data[fieldName] = undefined;
|
|
300
302
|
const realFieldName = ((_a = field.extensions) === null || _a === void 0 ? void 0 : _a.propertyName) || fieldName;
|
|
301
|
-
data[realFieldName] = resolveDataByUnionInputType(
|
|
303
|
+
data[realFieldName] = resolveDataByUnionInputType(fieldData, field.type, schemaComposer);
|
|
302
304
|
}
|
|
303
305
|
}
|
|
304
306
|
}
|
|
@@ -818,7 +820,7 @@ function getTypeResolverFromOutputTCs(ajv, outputTypeComposers, statusCodeOneOfI
|
|
|
818
820
|
const ONE_OF_DEFINITION = /* GraphQL */ `
|
|
819
821
|
directive @oneOf on INPUT_OBJECT | FIELD_DEFINITION
|
|
820
822
|
`;
|
|
821
|
-
function getUnionTypeComposers({ schemaComposer, ajv, typeComposersList,
|
|
823
|
+
function getUnionTypeComposers({ schemaComposer, ajv, typeComposersList, subSchemaAndTypeComposers, }) {
|
|
822
824
|
if (typeComposersList.length === 1) {
|
|
823
825
|
return typeComposersList[0];
|
|
824
826
|
}
|
|
@@ -842,97 +844,25 @@ function getUnionTypeComposers({ schemaComposer, ajv, typeComposersList, subSche
|
|
|
842
844
|
type: input,
|
|
843
845
|
};
|
|
844
846
|
});
|
|
845
|
-
|
|
846
|
-
name: getValidTypeName({
|
|
847
|
-
schemaComposer,
|
|
848
|
-
isInput: true,
|
|
849
|
-
subSchema,
|
|
850
|
-
}),
|
|
851
|
-
description: subSchema.description,
|
|
852
|
-
fields: unionInputFields,
|
|
853
|
-
});
|
|
847
|
+
subSchemaAndTypeComposers.input.addFields(unionInputFields);
|
|
854
848
|
if (!schemaComposer.hasDirective('oneOf')) {
|
|
855
849
|
schemaComposer.addTypeDefs(ONE_OF_DEFINITION);
|
|
856
850
|
}
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
]);
|
|
863
|
-
let output;
|
|
864
|
-
let sharedFields;
|
|
865
|
-
if (generateInterfaceFromSharedFields) {
|
|
866
|
-
for (const typeComposer of outputTypeComposers) {
|
|
867
|
-
const fieldMap = typeComposer.getFields();
|
|
868
|
-
if (!sharedFields) {
|
|
869
|
-
sharedFields = { ...fieldMap };
|
|
870
|
-
}
|
|
871
|
-
else {
|
|
872
|
-
for (const potentialSharedFieldName in sharedFields) {
|
|
873
|
-
if (!(potentialSharedFieldName in fieldMap &&
|
|
874
|
-
fieldMap[potentialSharedFieldName].type.getTypeName() ===
|
|
875
|
-
sharedFields[potentialSharedFieldName].type.getTypeName())) {
|
|
876
|
-
sharedFields[potentialSharedFieldName] = undefined;
|
|
877
|
-
delete sharedFields[potentialSharedFieldName];
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
}
|
|
851
|
+
const resolveType = getTypeResolverFromOutputTCs(ajv, outputTypeComposers, subSchemaAndTypeComposers.output.getExtension('statusCodeOneOfIndexMap'));
|
|
852
|
+
subSchemaAndTypeComposers.output.setResolveType(resolveType);
|
|
853
|
+
for (const outputTypeComposer of outputTypeComposers) {
|
|
854
|
+
if ('getFields' in outputTypeComposer) {
|
|
855
|
+
subSchemaAndTypeComposers.output.addType(outputTypeComposer);
|
|
881
856
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
name: getValidTypeName({
|
|
887
|
-
schemaComposer,
|
|
888
|
-
isInput: false,
|
|
889
|
-
subSchema,
|
|
890
|
-
}),
|
|
891
|
-
description: subSchema.description,
|
|
892
|
-
fields: sharedFields,
|
|
893
|
-
resolveType,
|
|
894
|
-
extensions: {
|
|
895
|
-
statusCodeOneOfIndexMap,
|
|
896
|
-
},
|
|
897
|
-
});
|
|
898
|
-
for (const typeComposer of outputTypeComposers) {
|
|
899
|
-
typeComposer.addInterface(output);
|
|
900
|
-
// GraphQL removes implementations
|
|
901
|
-
schemaComposer.addSchemaMustHaveType(typeComposer);
|
|
857
|
+
else {
|
|
858
|
+
for (const possibleType of outputTypeComposer.getTypes()) {
|
|
859
|
+
subSchemaAndTypeComposers.output.addType(possibleType);
|
|
860
|
+
}
|
|
902
861
|
}
|
|
903
862
|
}
|
|
904
|
-
else {
|
|
905
|
-
// If no shared fields found
|
|
906
|
-
output = schemaComposer.createUnionTC({
|
|
907
|
-
name: getValidTypeName({
|
|
908
|
-
schemaComposer,
|
|
909
|
-
isInput: false,
|
|
910
|
-
subSchema,
|
|
911
|
-
}),
|
|
912
|
-
description: subSchema.description,
|
|
913
|
-
types: () => {
|
|
914
|
-
const possibleTypes = [];
|
|
915
|
-
for (const outputTypeComposer of outputTypeComposers) {
|
|
916
|
-
if ('getFields' in outputTypeComposer) {
|
|
917
|
-
possibleTypes.push(outputTypeComposer);
|
|
918
|
-
}
|
|
919
|
-
else {
|
|
920
|
-
for (const possibleType of outputTypeComposer.getTypes()) {
|
|
921
|
-
possibleTypes.push(possibleType);
|
|
922
|
-
}
|
|
923
|
-
}
|
|
924
|
-
}
|
|
925
|
-
return possibleTypes;
|
|
926
|
-
},
|
|
927
|
-
resolveType,
|
|
928
|
-
extensions: {
|
|
929
|
-
statusCodeOneOfIndexMap,
|
|
930
|
-
},
|
|
931
|
-
});
|
|
932
|
-
}
|
|
933
863
|
return {
|
|
934
|
-
input,
|
|
935
|
-
output,
|
|
864
|
+
input: subSchemaAndTypeComposers.input,
|
|
865
|
+
output: subSchemaAndTypeComposers.output,
|
|
936
866
|
};
|
|
937
867
|
}
|
|
938
868
|
|
|
@@ -1020,18 +950,22 @@ const GraphQLFile = new GraphQLScalarType({
|
|
|
1020
950
|
codegenScalarType: 'File',
|
|
1021
951
|
},
|
|
1022
952
|
});
|
|
1023
|
-
function getComposerFromJSONSchema(schema, logger
|
|
953
|
+
function getComposerFromJSONSchema(schema, logger) {
|
|
1024
954
|
const schemaComposer = new SchemaComposer();
|
|
1025
955
|
const ajv = new Ajv({
|
|
1026
956
|
strict: false,
|
|
1027
957
|
});
|
|
1028
958
|
addFormats(ajv);
|
|
1029
959
|
const formatScalarMap = getJSONSchemaStringFormatScalarMap(ajv);
|
|
1030
|
-
const
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
960
|
+
const rootInputTypeNameComposerMap = {
|
|
961
|
+
QueryInput: () => schemaComposer.Query,
|
|
962
|
+
MutationInput: () => schemaComposer.Mutation,
|
|
963
|
+
SubscriptionInput: () => schemaComposer.Subscription,
|
|
964
|
+
};
|
|
965
|
+
return visitJSONSchema(schema, {
|
|
966
|
+
enter(subSchema, { path, visitedSubschemaResultMap }) {
|
|
967
|
+
var _a;
|
|
968
|
+
logger === null || logger === void 0 ? void 0 : logger.debug(`Entering ${path} for GraphQL Schema`);
|
|
1035
969
|
if (typeof subSchema === 'boolean') {
|
|
1036
970
|
const typeComposer = schemaComposer.getAnyTC(GraphQLJSON$1);
|
|
1037
971
|
return subSchema
|
|
@@ -1057,6 +991,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1057
991
|
return {
|
|
1058
992
|
input: typeComposer,
|
|
1059
993
|
output: typeComposer,
|
|
994
|
+
nullable: subSchema.nullable,
|
|
1060
995
|
};
|
|
1061
996
|
}
|
|
1062
997
|
if (subSchema.const) {
|
|
@@ -1077,6 +1012,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1077
1012
|
return {
|
|
1078
1013
|
input: typeComposer,
|
|
1079
1014
|
output: typeComposer,
|
|
1015
|
+
nullable: subSchema.nullable,
|
|
1080
1016
|
};
|
|
1081
1017
|
}
|
|
1082
1018
|
if (subSchema.enum && subSchema.type !== 'boolean') {
|
|
@@ -1111,234 +1047,34 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1111
1047
|
return {
|
|
1112
1048
|
input: typeComposer,
|
|
1113
1049
|
output: typeComposer,
|
|
1050
|
+
nullable: subSchema.nullable,
|
|
1114
1051
|
};
|
|
1115
1052
|
}
|
|
1116
|
-
if (
|
|
1117
|
-
|
|
1118
|
-
if (
|
|
1119
|
-
|
|
1120
|
-
|
|
1053
|
+
if (Array.isArray(subSchema.type)) {
|
|
1054
|
+
const validTypes = subSchema.type.filter((typeName) => typeName !== 'null');
|
|
1055
|
+
if (validTypes.length === 1) {
|
|
1056
|
+
subSchema.type = validTypes[0];
|
|
1057
|
+
// continue with the single type
|
|
1121
1058
|
}
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
const { input, output } = getUnionTypeComposers({
|
|
1125
|
-
schemaComposer,
|
|
1126
|
-
ajv,
|
|
1127
|
-
typeComposersList: subSchema.oneOf.map(({ input, output }) => ({
|
|
1128
|
-
input: input.ofType || input,
|
|
1129
|
-
output: output.ofType || output,
|
|
1130
|
-
})),
|
|
1131
|
-
subSchema,
|
|
1132
|
-
generateInterfaceFromSharedFields,
|
|
1133
|
-
statusCodeOneOfIndexMap,
|
|
1134
|
-
});
|
|
1059
|
+
else if (validTypes.length === 0) {
|
|
1060
|
+
const typeComposer = schemaComposer.getAnyTC(GraphQLVoid);
|
|
1135
1061
|
return {
|
|
1136
|
-
input:
|
|
1137
|
-
output:
|
|
1062
|
+
input: typeComposer,
|
|
1063
|
+
output: typeComposer,
|
|
1064
|
+
nullable: subSchema.nullable,
|
|
1138
1065
|
};
|
|
1139
1066
|
}
|
|
1140
|
-
return getUnionTypeComposers({
|
|
1141
|
-
schemaComposer,
|
|
1142
|
-
ajv,
|
|
1143
|
-
typeComposersList: subSchema.oneOf,
|
|
1144
|
-
subSchema,
|
|
1145
|
-
generateInterfaceFromSharedFields,
|
|
1146
|
-
statusCodeOneOfIndexMap,
|
|
1147
|
-
});
|
|
1148
|
-
}
|
|
1149
|
-
if (subSchema.allOf && !subSchema.properties) {
|
|
1150
|
-
const inputFieldMap = {};
|
|
1151
|
-
const fieldMap = {};
|
|
1152
|
-
let ableToUseGraphQLInputObjectType = true;
|
|
1153
|
-
for (const maybeTypeComposers of subSchema.allOf) {
|
|
1154
|
-
const { input: inputTypeComposer, output: outputTypeComposer } = maybeTypeComposers;
|
|
1155
|
-
if (inputTypeComposer instanceof ScalarTypeComposer) {
|
|
1156
|
-
ableToUseGraphQLInputObjectType = false;
|
|
1157
|
-
}
|
|
1158
|
-
else {
|
|
1159
|
-
const inputTypeElemFieldMap = inputTypeComposer.getFields();
|
|
1160
|
-
for (const fieldName in inputTypeElemFieldMap) {
|
|
1161
|
-
const field = inputTypeElemFieldMap[fieldName];
|
|
1162
|
-
inputFieldMap[fieldName] = field;
|
|
1163
|
-
}
|
|
1164
|
-
}
|
|
1165
|
-
if (outputTypeComposer instanceof ScalarTypeComposer) {
|
|
1166
|
-
fieldMap[outputTypeComposer.getTypeName()] = {
|
|
1167
|
-
type: outputTypeComposer,
|
|
1168
|
-
resolve: root => root,
|
|
1169
|
-
};
|
|
1170
|
-
}
|
|
1171
|
-
else if (outputTypeComposer instanceof UnionTypeComposer) {
|
|
1172
|
-
const outputTCElems = outputTypeComposer.getTypes();
|
|
1173
|
-
for (const outputTCElem of outputTCElems) {
|
|
1174
|
-
const outputTypeElemFieldMap = outputTCElem.getFields();
|
|
1175
|
-
for (const fieldName in outputTypeElemFieldMap) {
|
|
1176
|
-
const field = outputTypeElemFieldMap[fieldName];
|
|
1177
|
-
fieldMap[fieldName] = field;
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
}
|
|
1181
|
-
else {
|
|
1182
|
-
const typeElemFieldMap = outputTypeComposer.getFields();
|
|
1183
|
-
for (const fieldName in typeElemFieldMap) {
|
|
1184
|
-
const field = typeElemFieldMap[fieldName];
|
|
1185
|
-
fieldMap[fieldName] = field;
|
|
1186
|
-
}
|
|
1187
|
-
}
|
|
1188
|
-
}
|
|
1189
|
-
let inputTypeComposer;
|
|
1190
|
-
const outputTypeComposer = schemaComposer.createObjectTC({
|
|
1191
|
-
name: getValidTypeName({
|
|
1192
|
-
schemaComposer,
|
|
1193
|
-
isInput: false,
|
|
1194
|
-
subSchema,
|
|
1195
|
-
}),
|
|
1196
|
-
description: subSchema.description,
|
|
1197
|
-
fields: fieldMap,
|
|
1198
|
-
extensions: {
|
|
1199
|
-
validateWithJSONSchema,
|
|
1200
|
-
examples: subSchema.examples,
|
|
1201
|
-
default: subSchema.default,
|
|
1202
|
-
},
|
|
1203
|
-
});
|
|
1204
|
-
if (ableToUseGraphQLInputObjectType) {
|
|
1205
|
-
inputTypeComposer = schemaComposer.createInputTC({
|
|
1206
|
-
name: getValidTypeName({
|
|
1207
|
-
schemaComposer,
|
|
1208
|
-
isInput: true,
|
|
1209
|
-
subSchema,
|
|
1210
|
-
}),
|
|
1211
|
-
description: subSchema.description,
|
|
1212
|
-
fields: inputFieldMap,
|
|
1213
|
-
extensions: {
|
|
1214
|
-
examples: subSchema.examples,
|
|
1215
|
-
default: subSchema.default,
|
|
1216
|
-
},
|
|
1217
|
-
});
|
|
1218
|
-
}
|
|
1219
1067
|
else {
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
: getGenericJSONScalar({
|
|
1223
|
-
schemaComposer,
|
|
1224
|
-
isInput: true,
|
|
1225
|
-
subSchema,
|
|
1226
|
-
validateWithJSONSchema,
|
|
1227
|
-
});
|
|
1228
|
-
}
|
|
1229
|
-
return {
|
|
1230
|
-
input: inputTypeComposer,
|
|
1231
|
-
output: outputTypeComposer,
|
|
1232
|
-
};
|
|
1233
|
-
}
|
|
1234
|
-
if (subSchema.anyOf && !subSchema.properties) {
|
|
1235
|
-
if (subSchema.title === 'Any') {
|
|
1236
|
-
const genericJSONScalar = getGenericJSONScalar({
|
|
1237
|
-
schemaComposer,
|
|
1238
|
-
isInput: false,
|
|
1068
|
+
const typeComposer = getGenericJSONScalar({
|
|
1069
|
+
isInput: true,
|
|
1239
1070
|
subSchema,
|
|
1240
|
-
validateWithJSONSchema,
|
|
1241
|
-
});
|
|
1242
|
-
return {
|
|
1243
|
-
input: genericJSONScalar,
|
|
1244
|
-
output: genericJSONScalar,
|
|
1245
|
-
};
|
|
1246
|
-
}
|
|
1247
|
-
// It should not have `required` because it is `anyOf` not `allOf`
|
|
1248
|
-
const inputFieldMap = {};
|
|
1249
|
-
const fieldMap = {};
|
|
1250
|
-
let ableToUseGraphQLInputObjectType = true;
|
|
1251
|
-
for (const typeComposers of subSchema.anyOf) {
|
|
1252
|
-
const { input: inputTypeComposer, output: outputTypeComposer } = typeComposers;
|
|
1253
|
-
if (inputTypeComposer instanceof ScalarTypeComposer) {
|
|
1254
|
-
ableToUseGraphQLInputObjectType = false;
|
|
1255
|
-
}
|
|
1256
|
-
else {
|
|
1257
|
-
const inputTypeElemFieldMap = inputTypeComposer.getFields();
|
|
1258
|
-
for (const fieldName in inputTypeElemFieldMap) {
|
|
1259
|
-
const field = inputTypeElemFieldMap[fieldName];
|
|
1260
|
-
inputFieldMap[fieldName] = isNonNullType(field.type.getType())
|
|
1261
|
-
? {
|
|
1262
|
-
...field,
|
|
1263
|
-
type: () => field.type.ofType,
|
|
1264
|
-
}
|
|
1265
|
-
: field;
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
if (outputTypeComposer instanceof ScalarTypeComposer) {
|
|
1269
|
-
const typeName = outputTypeComposer.getTypeName();
|
|
1270
|
-
fieldMap[typeName] = {
|
|
1271
|
-
type: outputTypeComposer,
|
|
1272
|
-
resolve: root => root,
|
|
1273
|
-
};
|
|
1274
|
-
}
|
|
1275
|
-
else {
|
|
1276
|
-
const typeElemFieldMap = outputTypeComposer.getFields();
|
|
1277
|
-
for (const fieldName in typeElemFieldMap) {
|
|
1278
|
-
const field = typeElemFieldMap[fieldName];
|
|
1279
|
-
fieldMap[fieldName] = {
|
|
1280
|
-
type: () => getNamedType(field.type.getType()),
|
|
1281
|
-
...field,
|
|
1282
|
-
};
|
|
1283
|
-
}
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
let inputTypeComposer;
|
|
1287
|
-
const outputTypeComposer = schemaComposer.createObjectTC({
|
|
1288
|
-
name: getValidTypeName({
|
|
1289
1071
|
schemaComposer,
|
|
1290
|
-
isInput: false,
|
|
1291
|
-
subSchema,
|
|
1292
|
-
}),
|
|
1293
|
-
description: subSchema.description,
|
|
1294
|
-
fields: fieldMap,
|
|
1295
|
-
extensions: {
|
|
1296
1072
|
validateWithJSONSchema,
|
|
1297
|
-
examples: subSchema.examples,
|
|
1298
|
-
default: subSchema.default,
|
|
1299
|
-
},
|
|
1300
|
-
});
|
|
1301
|
-
if (ableToUseGraphQLInputObjectType) {
|
|
1302
|
-
inputTypeComposer = schemaComposer.createInputTC({
|
|
1303
|
-
name: getValidTypeName({
|
|
1304
|
-
schemaComposer,
|
|
1305
|
-
isInput: true,
|
|
1306
|
-
subSchema,
|
|
1307
|
-
}),
|
|
1308
|
-
description: subSchema.description,
|
|
1309
|
-
fields: inputFieldMap,
|
|
1310
|
-
extensions: {
|
|
1311
|
-
examples: subSchema.examples,
|
|
1312
|
-
default: subSchema.default,
|
|
1313
|
-
},
|
|
1314
1073
|
});
|
|
1315
|
-
}
|
|
1316
|
-
else {
|
|
1317
|
-
inputTypeComposer = isSomeInputTypeComposer(outputTypeComposer)
|
|
1318
|
-
? outputTypeComposer
|
|
1319
|
-
: getGenericJSONScalar({
|
|
1320
|
-
schemaComposer,
|
|
1321
|
-
isInput: true,
|
|
1322
|
-
subSchema,
|
|
1323
|
-
validateWithJSONSchema,
|
|
1324
|
-
});
|
|
1325
|
-
}
|
|
1326
|
-
return {
|
|
1327
|
-
input: inputTypeComposer,
|
|
1328
|
-
output: outputTypeComposer,
|
|
1329
|
-
};
|
|
1330
|
-
}
|
|
1331
|
-
if (Array.isArray(subSchema.type)) {
|
|
1332
|
-
const validTypes = subSchema.type.filter((typeName) => typeName !== 'null');
|
|
1333
|
-
if (validTypes.length === 1) {
|
|
1334
|
-
subSchema.type = validTypes[0];
|
|
1335
|
-
// continue with the single type
|
|
1336
|
-
}
|
|
1337
|
-
else {
|
|
1338
|
-
const typeComposer = schemaComposer.getAnyTC(GraphQLVoid);
|
|
1339
1074
|
return {
|
|
1340
1075
|
input: typeComposer,
|
|
1341
1076
|
output: typeComposer,
|
|
1077
|
+
nullable: subSchema.nullable,
|
|
1342
1078
|
};
|
|
1343
1079
|
}
|
|
1344
1080
|
}
|
|
@@ -1349,6 +1085,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1349
1085
|
input: typeComposer,
|
|
1350
1086
|
output: typeComposer,
|
|
1351
1087
|
description: subSchema.description,
|
|
1088
|
+
nullable: subSchema.nullable,
|
|
1352
1089
|
};
|
|
1353
1090
|
}
|
|
1354
1091
|
case 'boolean': {
|
|
@@ -1357,6 +1094,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1357
1094
|
input: typeComposer,
|
|
1358
1095
|
output: typeComposer,
|
|
1359
1096
|
description: subSchema.description,
|
|
1097
|
+
nullable: subSchema.nullable,
|
|
1360
1098
|
};
|
|
1361
1099
|
}
|
|
1362
1100
|
case 'null': {
|
|
@@ -1365,6 +1103,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1365
1103
|
input: typeComposer,
|
|
1366
1104
|
output: typeComposer,
|
|
1367
1105
|
description: subSchema.description,
|
|
1106
|
+
nullable: subSchema.nullable,
|
|
1368
1107
|
};
|
|
1369
1108
|
}
|
|
1370
1109
|
case 'integer': {
|
|
@@ -1374,6 +1113,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1374
1113
|
input: typeComposer,
|
|
1375
1114
|
output: typeComposer,
|
|
1376
1115
|
description: subSchema.description,
|
|
1116
|
+
nullable: subSchema.nullable,
|
|
1377
1117
|
};
|
|
1378
1118
|
}
|
|
1379
1119
|
const typeComposer = schemaComposer.getAnyTC(GraphQLInt);
|
|
@@ -1381,6 +1121,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1381
1121
|
input: typeComposer,
|
|
1382
1122
|
output: typeComposer,
|
|
1383
1123
|
description: subSchema.description,
|
|
1124
|
+
nullable: subSchema.nullable,
|
|
1384
1125
|
};
|
|
1385
1126
|
}
|
|
1386
1127
|
case 'number': {
|
|
@@ -1389,6 +1130,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1389
1130
|
input: typeComposer,
|
|
1390
1131
|
output: typeComposer,
|
|
1391
1132
|
description: subSchema.description,
|
|
1133
|
+
nullable: subSchema.nullable,
|
|
1392
1134
|
};
|
|
1393
1135
|
}
|
|
1394
1136
|
case 'string': {
|
|
@@ -1402,6 +1144,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1402
1144
|
input: typeComposer,
|
|
1403
1145
|
output: typeComposer,
|
|
1404
1146
|
description: subSchema.description,
|
|
1147
|
+
nullable: subSchema.nullable,
|
|
1405
1148
|
};
|
|
1406
1149
|
}
|
|
1407
1150
|
switch (subSchema.format) {
|
|
@@ -1411,6 +1154,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1411
1154
|
input: typeComposer,
|
|
1412
1155
|
output: typeComposer,
|
|
1413
1156
|
description: subSchema.description,
|
|
1157
|
+
nullable: subSchema.nullable,
|
|
1414
1158
|
};
|
|
1415
1159
|
}
|
|
1416
1160
|
case 'time': {
|
|
@@ -1419,6 +1163,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1419
1163
|
input: typeComposer,
|
|
1420
1164
|
output: typeComposer,
|
|
1421
1165
|
description: subSchema.description,
|
|
1166
|
+
nullable: subSchema.nullable,
|
|
1422
1167
|
};
|
|
1423
1168
|
}
|
|
1424
1169
|
case 'email': {
|
|
@@ -1427,6 +1172,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1427
1172
|
input: typeComposer,
|
|
1428
1173
|
output: typeComposer,
|
|
1429
1174
|
description: subSchema.description,
|
|
1175
|
+
nullable: subSchema.nullable,
|
|
1430
1176
|
};
|
|
1431
1177
|
}
|
|
1432
1178
|
case 'ipv4': {
|
|
@@ -1435,6 +1181,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1435
1181
|
input: typeComposer,
|
|
1436
1182
|
output: typeComposer,
|
|
1437
1183
|
description: subSchema.description,
|
|
1184
|
+
nullable: subSchema.nullable,
|
|
1438
1185
|
};
|
|
1439
1186
|
}
|
|
1440
1187
|
case 'ipv6': {
|
|
@@ -1443,6 +1190,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1443
1190
|
input: typeComposer,
|
|
1444
1191
|
output: typeComposer,
|
|
1445
1192
|
description: subSchema.description,
|
|
1193
|
+
nullable: subSchema.nullable,
|
|
1446
1194
|
};
|
|
1447
1195
|
}
|
|
1448
1196
|
case 'uri': {
|
|
@@ -1451,6 +1199,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1451
1199
|
input: typeComposer,
|
|
1452
1200
|
output: typeComposer,
|
|
1453
1201
|
description: subSchema.description,
|
|
1202
|
+
nullable: subSchema.nullable,
|
|
1454
1203
|
};
|
|
1455
1204
|
}
|
|
1456
1205
|
default: {
|
|
@@ -1460,19 +1209,24 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1460
1209
|
input: typeComposer,
|
|
1461
1210
|
output: typeComposer,
|
|
1462
1211
|
description: subSchema.description,
|
|
1212
|
+
nullable: subSchema.nullable,
|
|
1463
1213
|
};
|
|
1464
1214
|
}
|
|
1465
1215
|
}
|
|
1466
1216
|
}
|
|
1467
1217
|
case 'array':
|
|
1468
|
-
if (typeof subSchema.items === 'object' &&
|
|
1469
|
-
!Array.isArray(subSchema.items) &&
|
|
1470
|
-
Object.keys(subSchema.items).length > 0) {
|
|
1471
|
-
const typeComposers = subSchema.items;
|
|
1218
|
+
if (typeof subSchema.items === 'object' && Object.keys(subSchema.items).length > 0) {
|
|
1472
1219
|
return {
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1220
|
+
// These are filled after enter
|
|
1221
|
+
get input() {
|
|
1222
|
+
const typeComposers = visitedSubschemaResultMap.get(subSchema.items);
|
|
1223
|
+
return typeComposers.input.getTypePlural();
|
|
1224
|
+
},
|
|
1225
|
+
get output() {
|
|
1226
|
+
const typeComposers = visitedSubschemaResultMap.get(subSchema.items);
|
|
1227
|
+
return typeComposers.output.getTypePlural();
|
|
1228
|
+
},
|
|
1229
|
+
...subSchema,
|
|
1476
1230
|
};
|
|
1477
1231
|
}
|
|
1478
1232
|
if (subSchema.contains) {
|
|
@@ -1486,26 +1240,7 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1486
1240
|
return {
|
|
1487
1241
|
input: typeComposer,
|
|
1488
1242
|
output: typeComposer,
|
|
1489
|
-
|
|
1490
|
-
}
|
|
1491
|
-
if (typeof subSchema.items === 'object' && Array.isArray(subSchema.items)) {
|
|
1492
|
-
const existingItems = [...subSchema.items];
|
|
1493
|
-
/* TODO
|
|
1494
|
-
if (subSchema.additionalItems) {
|
|
1495
|
-
existingItems.push(subSchema.additionalItems);
|
|
1496
|
-
}
|
|
1497
|
-
*/
|
|
1498
|
-
const { input: inputTypeComposer, output: outputTypeComposer } = getUnionTypeComposers({
|
|
1499
|
-
schemaComposer,
|
|
1500
|
-
ajv,
|
|
1501
|
-
typeComposersList: existingItems,
|
|
1502
|
-
subSchema,
|
|
1503
|
-
generateInterfaceFromSharedFields,
|
|
1504
|
-
});
|
|
1505
|
-
return {
|
|
1506
|
-
input: inputTypeComposer.getTypePlural(),
|
|
1507
|
-
output: outputTypeComposer.getTypePlural(),
|
|
1508
|
-
description: subSchema.description,
|
|
1243
|
+
nullable: subSchema.nullable,
|
|
1509
1244
|
};
|
|
1510
1245
|
}
|
|
1511
1246
|
// If it doesn't have any clue
|
|
@@ -1521,29 +1256,362 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1521
1256
|
input: typeComposer,
|
|
1522
1257
|
output: typeComposer,
|
|
1523
1258
|
description: subSchema.description,
|
|
1259
|
+
nullable: subSchema.nullable,
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
case 'object': {
|
|
1263
|
+
switch (subSchema.title) {
|
|
1264
|
+
case '_schema':
|
|
1265
|
+
return {
|
|
1266
|
+
output: schemaComposer,
|
|
1267
|
+
...subSchema,
|
|
1268
|
+
};
|
|
1269
|
+
case 'Query':
|
|
1270
|
+
return {
|
|
1271
|
+
output: schemaComposer.Query,
|
|
1272
|
+
...subSchema,
|
|
1273
|
+
};
|
|
1274
|
+
case 'Mutation':
|
|
1275
|
+
return {
|
|
1276
|
+
output: schemaComposer.Mutation,
|
|
1277
|
+
...subSchema,
|
|
1278
|
+
};
|
|
1279
|
+
case 'Subscription':
|
|
1280
|
+
return {
|
|
1281
|
+
output: schemaComposer.Subscription,
|
|
1282
|
+
...subSchema,
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
if (subSchema.properties || subSchema.allOf || subSchema.additionalProperties) {
|
|
1286
|
+
return {
|
|
1287
|
+
input: schemaComposer.createInputTC({
|
|
1288
|
+
name: getValidTypeName({
|
|
1289
|
+
schemaComposer,
|
|
1290
|
+
isInput: true,
|
|
1291
|
+
subSchema,
|
|
1292
|
+
}),
|
|
1293
|
+
description: subSchema.description,
|
|
1294
|
+
fields: {},
|
|
1295
|
+
extensions: {
|
|
1296
|
+
examples: subSchema.examples,
|
|
1297
|
+
default: subSchema.default,
|
|
1298
|
+
},
|
|
1299
|
+
}),
|
|
1300
|
+
output: schemaComposer.createObjectTC({
|
|
1301
|
+
name: getValidTypeName({
|
|
1302
|
+
schemaComposer,
|
|
1303
|
+
isInput: false,
|
|
1304
|
+
subSchema,
|
|
1305
|
+
}),
|
|
1306
|
+
description: subSchema.description,
|
|
1307
|
+
fields: {},
|
|
1308
|
+
extensions: {
|
|
1309
|
+
validateWithJSONSchema,
|
|
1310
|
+
examples: subSchema.examples,
|
|
1311
|
+
default: subSchema.default,
|
|
1312
|
+
},
|
|
1313
|
+
}),
|
|
1314
|
+
...subSchema,
|
|
1315
|
+
...(subSchema.properties ? { properties: { ...subSchema.properties } } : {}),
|
|
1316
|
+
...(subSchema.allOf ? { allOf: [...subSchema.allOf] } : {}),
|
|
1317
|
+
...(subSchema.additionalProperties
|
|
1318
|
+
? {
|
|
1319
|
+
additionalProperties: subSchema.additionalProperties === true ? true : { ...subSchema.additionalProperties },
|
|
1320
|
+
}
|
|
1321
|
+
: {}),
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
if (subSchema.oneOf && !subSchema.properties) {
|
|
1327
|
+
let statusCodeOneOfIndexMap;
|
|
1328
|
+
if ((_a = subSchema.$comment) === null || _a === void 0 ? void 0 : _a.startsWith('statusCodeOneOfIndexMap:')) {
|
|
1329
|
+
const statusCodeOneOfIndexMapStr = subSchema.$comment.replace('statusCodeOneOfIndexMap:', '');
|
|
1330
|
+
statusCodeOneOfIndexMap = JSON.parse(statusCodeOneOfIndexMapStr);
|
|
1331
|
+
}
|
|
1332
|
+
const input = schemaComposer.createInputTC({
|
|
1333
|
+
name: getValidTypeName({
|
|
1334
|
+
schemaComposer,
|
|
1335
|
+
isInput: true,
|
|
1336
|
+
subSchema,
|
|
1337
|
+
}),
|
|
1338
|
+
fields: {},
|
|
1339
|
+
directives: [
|
|
1340
|
+
{
|
|
1341
|
+
name: 'oneOf',
|
|
1342
|
+
},
|
|
1343
|
+
],
|
|
1344
|
+
});
|
|
1345
|
+
const output = schemaComposer.createUnionTC({
|
|
1346
|
+
name: getValidTypeName({
|
|
1347
|
+
schemaComposer,
|
|
1348
|
+
isInput: false,
|
|
1349
|
+
subSchema,
|
|
1350
|
+
}),
|
|
1351
|
+
description: subSchema.description,
|
|
1352
|
+
types: [],
|
|
1353
|
+
extensions: {
|
|
1354
|
+
statusCodeOneOfIndexMap,
|
|
1355
|
+
},
|
|
1356
|
+
});
|
|
1357
|
+
return {
|
|
1358
|
+
input,
|
|
1359
|
+
output,
|
|
1360
|
+
...subSchema,
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
if (!subSchema.properties && (subSchema.anyOf || subSchema.allOf)) {
|
|
1364
|
+
if (subSchema.title === 'Any') {
|
|
1365
|
+
const genericJSONScalar = getGenericJSONScalar({
|
|
1366
|
+
schemaComposer,
|
|
1367
|
+
isInput: false,
|
|
1368
|
+
subSchema,
|
|
1369
|
+
validateWithJSONSchema,
|
|
1370
|
+
});
|
|
1371
|
+
return {
|
|
1372
|
+
input: genericJSONScalar,
|
|
1373
|
+
output: genericJSONScalar,
|
|
1374
|
+
nullable: subSchema.nullable,
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
const input = schemaComposer.createInputTC({
|
|
1378
|
+
name: getValidTypeName({
|
|
1379
|
+
schemaComposer,
|
|
1380
|
+
isInput: true,
|
|
1381
|
+
subSchema,
|
|
1382
|
+
}),
|
|
1383
|
+
description: subSchema.description,
|
|
1384
|
+
fields: {},
|
|
1385
|
+
extensions: {
|
|
1386
|
+
examples: subSchema.examples,
|
|
1387
|
+
default: subSchema.default,
|
|
1388
|
+
},
|
|
1389
|
+
});
|
|
1390
|
+
const output = schemaComposer.createObjectTC({
|
|
1391
|
+
name: getValidTypeName({
|
|
1392
|
+
schemaComposer,
|
|
1393
|
+
isInput: false,
|
|
1394
|
+
subSchema,
|
|
1395
|
+
}),
|
|
1396
|
+
description: subSchema.description,
|
|
1397
|
+
fields: {},
|
|
1398
|
+
extensions: {
|
|
1399
|
+
validateWithJSONSchema,
|
|
1400
|
+
examples: subSchema.examples,
|
|
1401
|
+
default: subSchema.default,
|
|
1402
|
+
},
|
|
1403
|
+
});
|
|
1404
|
+
return {
|
|
1405
|
+
input,
|
|
1406
|
+
output,
|
|
1407
|
+
...subSchema,
|
|
1408
|
+
};
|
|
1409
|
+
}
|
|
1410
|
+
return subSchema;
|
|
1411
|
+
},
|
|
1412
|
+
leave(subSchemaAndTypeComposers, { path }) {
|
|
1413
|
+
var _a, _b, _c, _d, _e;
|
|
1414
|
+
logger === null || logger === void 0 ? void 0 : logger.debug(`Leaving ${path} for GraphQL Schema`);
|
|
1415
|
+
const validateWithJSONSchema = getValidateFnForSchemaPath(ajv, path, schema);
|
|
1416
|
+
const subSchemaOnly = {
|
|
1417
|
+
...subSchemaAndTypeComposers,
|
|
1418
|
+
input: undefined,
|
|
1419
|
+
output: undefined,
|
|
1420
|
+
};
|
|
1421
|
+
if (subSchemaAndTypeComposers.oneOf && !subSchemaAndTypeComposers.properties) {
|
|
1422
|
+
const isPlural = subSchemaAndTypeComposers.oneOf.some(({ output }) => 'ofType' in output);
|
|
1423
|
+
if (isPlural) {
|
|
1424
|
+
const { input, output } = getUnionTypeComposers({
|
|
1425
|
+
schemaComposer,
|
|
1426
|
+
ajv,
|
|
1427
|
+
typeComposersList: subSchemaAndTypeComposers.oneOf.map(({ input, output }) => ({
|
|
1428
|
+
input: input.ofType || input,
|
|
1429
|
+
output: output.ofType || output,
|
|
1430
|
+
})),
|
|
1431
|
+
subSchemaAndTypeComposers,
|
|
1432
|
+
});
|
|
1433
|
+
return {
|
|
1434
|
+
input: input.getTypePlural(),
|
|
1435
|
+
output: output.getTypePlural(),
|
|
1436
|
+
nullable: subSchemaAndTypeComposers.nullable,
|
|
1437
|
+
};
|
|
1438
|
+
}
|
|
1439
|
+
return getUnionTypeComposers({
|
|
1440
|
+
schemaComposer,
|
|
1441
|
+
ajv,
|
|
1442
|
+
typeComposersList: subSchemaAndTypeComposers.oneOf,
|
|
1443
|
+
subSchemaAndTypeComposers,
|
|
1444
|
+
});
|
|
1445
|
+
}
|
|
1446
|
+
if (subSchemaAndTypeComposers.allOf && !subSchemaAndTypeComposers.properties) {
|
|
1447
|
+
const inputFieldMap = {};
|
|
1448
|
+
const fieldMap = {};
|
|
1449
|
+
let ableToUseGraphQLInputObjectType = true;
|
|
1450
|
+
for (const maybeTypeComposers of subSchemaAndTypeComposers.allOf) {
|
|
1451
|
+
const { input: inputTypeComposer, output: outputTypeComposer } = maybeTypeComposers;
|
|
1452
|
+
if (inputTypeComposer instanceof ScalarTypeComposer) {
|
|
1453
|
+
ableToUseGraphQLInputObjectType = false;
|
|
1454
|
+
}
|
|
1455
|
+
else {
|
|
1456
|
+
const inputTypeElemFieldMap = inputTypeComposer.getFields();
|
|
1457
|
+
for (const fieldName in inputTypeElemFieldMap) {
|
|
1458
|
+
const field = inputTypeElemFieldMap[fieldName];
|
|
1459
|
+
inputFieldMap[fieldName] = field;
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
if (outputTypeComposer instanceof ScalarTypeComposer) {
|
|
1463
|
+
fieldMap[outputTypeComposer.getTypeName()] = {
|
|
1464
|
+
type: outputTypeComposer,
|
|
1465
|
+
resolve: root => root,
|
|
1524
1466
|
};
|
|
1525
1467
|
}
|
|
1468
|
+
else if (outputTypeComposer instanceof UnionTypeComposer) {
|
|
1469
|
+
const outputTCElems = outputTypeComposer.getTypes();
|
|
1470
|
+
for (const outputTCElem of outputTCElems) {
|
|
1471
|
+
const outputTypeElemFieldMap = outputTCElem.getFields();
|
|
1472
|
+
for (const fieldName in outputTypeElemFieldMap) {
|
|
1473
|
+
const field = outputTypeElemFieldMap[fieldName];
|
|
1474
|
+
fieldMap[fieldName] = field;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
else {
|
|
1479
|
+
const typeElemFieldMap = outputTypeComposer.getFields();
|
|
1480
|
+
for (const fieldName in typeElemFieldMap) {
|
|
1481
|
+
const field = typeElemFieldMap[fieldName];
|
|
1482
|
+
fieldMap[fieldName] = field;
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
}
|
|
1486
|
+
let inputTypeComposer = subSchemaAndTypeComposers.input;
|
|
1487
|
+
subSchemaAndTypeComposers.output.addFields(fieldMap);
|
|
1488
|
+
subSchemaAndTypeComposers.output.setExtensions({
|
|
1489
|
+
validateWithJSONSchema,
|
|
1490
|
+
examples: subSchemaAndTypeComposers.examples,
|
|
1491
|
+
default: subSchemaAndTypeComposers.default,
|
|
1492
|
+
});
|
|
1493
|
+
if (ableToUseGraphQLInputObjectType) {
|
|
1494
|
+
inputTypeComposer.addFields(inputFieldMap);
|
|
1495
|
+
inputTypeComposer.setExtensions({
|
|
1496
|
+
examples: subSchemaAndTypeComposers.examples,
|
|
1497
|
+
default: subSchemaAndTypeComposers.default,
|
|
1498
|
+
});
|
|
1499
|
+
}
|
|
1500
|
+
else {
|
|
1501
|
+
schemaComposer.delete(inputTypeComposer);
|
|
1502
|
+
inputTypeComposer = getGenericJSONScalar({
|
|
1503
|
+
schemaComposer,
|
|
1504
|
+
isInput: true,
|
|
1505
|
+
subSchema: subSchemaOnly,
|
|
1506
|
+
validateWithJSONSchema,
|
|
1507
|
+
});
|
|
1508
|
+
}
|
|
1509
|
+
return {
|
|
1510
|
+
input: inputTypeComposer,
|
|
1511
|
+
output: subSchemaAndTypeComposers.output,
|
|
1512
|
+
nullable: subSchemaAndTypeComposers.nullable,
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
if (subSchemaAndTypeComposers.anyOf && !subSchemaAndTypeComposers.properties) {
|
|
1516
|
+
// It should not have `required` because it is `anyOf` not `allOf`
|
|
1517
|
+
const inputFieldMap = {};
|
|
1518
|
+
const fieldMap = {};
|
|
1519
|
+
let ableToUseGraphQLInputObjectType = true;
|
|
1520
|
+
for (const typeComposers of subSchemaAndTypeComposers.anyOf) {
|
|
1521
|
+
const { input: inputTypeComposer, output: outputTypeComposer } = typeComposers;
|
|
1522
|
+
if (inputTypeComposer instanceof ScalarTypeComposer) {
|
|
1523
|
+
ableToUseGraphQLInputObjectType = false;
|
|
1524
|
+
}
|
|
1525
|
+
else {
|
|
1526
|
+
const inputTypeElemFieldMap = inputTypeComposer.getFields();
|
|
1527
|
+
for (const fieldName in inputTypeElemFieldMap) {
|
|
1528
|
+
const field = inputTypeElemFieldMap[fieldName];
|
|
1529
|
+
inputFieldMap[fieldName] = isNonNullType(field.type.getType())
|
|
1530
|
+
? {
|
|
1531
|
+
...field,
|
|
1532
|
+
type: () => field.type.ofType,
|
|
1533
|
+
}
|
|
1534
|
+
: field;
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
if (outputTypeComposer instanceof ScalarTypeComposer) {
|
|
1538
|
+
const typeName = outputTypeComposer.getTypeName();
|
|
1539
|
+
fieldMap[typeName] = {
|
|
1540
|
+
type: outputTypeComposer,
|
|
1541
|
+
resolve: root => root,
|
|
1542
|
+
};
|
|
1543
|
+
}
|
|
1544
|
+
else {
|
|
1545
|
+
const typeElemFieldMap = outputTypeComposer.getFields();
|
|
1546
|
+
for (const fieldName in typeElemFieldMap) {
|
|
1547
|
+
const field = typeElemFieldMap[fieldName];
|
|
1548
|
+
fieldMap[fieldName] = {
|
|
1549
|
+
type: () => getNamedType(field.type.getType()),
|
|
1550
|
+
...field,
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
}
|
|
1555
|
+
let inputTypeComposer = subSchemaAndTypeComposers.input;
|
|
1556
|
+
subSchemaAndTypeComposers.output.addFields(fieldMap);
|
|
1557
|
+
subSchemaAndTypeComposers.output.setExtensions({
|
|
1558
|
+
validateWithJSONSchema,
|
|
1559
|
+
examples: subSchemaAndTypeComposers.examples,
|
|
1560
|
+
default: subSchemaAndTypeComposers.default,
|
|
1561
|
+
});
|
|
1562
|
+
if (ableToUseGraphQLInputObjectType) {
|
|
1563
|
+
inputTypeComposer.addFields(inputFieldMap);
|
|
1564
|
+
inputTypeComposer.setExtensions({
|
|
1565
|
+
examples: subSchemaAndTypeComposers.examples,
|
|
1566
|
+
default: subSchemaAndTypeComposers.default,
|
|
1567
|
+
});
|
|
1568
|
+
}
|
|
1569
|
+
else {
|
|
1570
|
+
schemaComposer.delete(inputTypeComposer);
|
|
1571
|
+
inputTypeComposer = getGenericJSONScalar({
|
|
1572
|
+
schemaComposer,
|
|
1573
|
+
isInput: true,
|
|
1574
|
+
subSchema: subSchemaOnly,
|
|
1575
|
+
validateWithJSONSchema,
|
|
1576
|
+
});
|
|
1577
|
+
}
|
|
1578
|
+
return {
|
|
1579
|
+
input: inputTypeComposer,
|
|
1580
|
+
output: subSchemaAndTypeComposers.output,
|
|
1581
|
+
nullable: subSchemaAndTypeComposers.nullable,
|
|
1582
|
+
};
|
|
1583
|
+
}
|
|
1584
|
+
switch (subSchemaAndTypeComposers.type) {
|
|
1526
1585
|
case 'object':
|
|
1527
1586
|
const fieldMap = {};
|
|
1528
1587
|
let inputFieldMap = {};
|
|
1529
|
-
if (
|
|
1530
|
-
|
|
1531
|
-
for (const propertyName in subSchema.properties) {
|
|
1588
|
+
if (subSchemaAndTypeComposers.properties) {
|
|
1589
|
+
for (const propertyName in subSchemaAndTypeComposers.properties) {
|
|
1532
1590
|
// TODO: needs to be fixed
|
|
1533
1591
|
if (propertyName === 'additionalProperties') {
|
|
1534
1592
|
continue;
|
|
1535
1593
|
}
|
|
1536
|
-
const typeComposers = subSchema.properties[propertyName];
|
|
1537
1594
|
const fieldName = sanitizeNameForGraphQL(propertyName);
|
|
1538
1595
|
fieldMap[fieldName] = {
|
|
1539
1596
|
type: () => {
|
|
1540
1597
|
var _a;
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1598
|
+
const typeComposers = subSchemaAndTypeComposers.properties[propertyName];
|
|
1599
|
+
let nullable = true;
|
|
1600
|
+
if ((_a = subSchemaAndTypeComposers.required) === null || _a === void 0 ? void 0 : _a.includes(propertyName)) {
|
|
1601
|
+
nullable = false;
|
|
1602
|
+
}
|
|
1603
|
+
// Nullable has more priority
|
|
1604
|
+
if (typeComposers.nullable === false) {
|
|
1605
|
+
nullable = false;
|
|
1606
|
+
}
|
|
1607
|
+
if (typeComposers.nullable === true) {
|
|
1608
|
+
nullable = true;
|
|
1609
|
+
}
|
|
1610
|
+
return !nullable ? typeComposers.output.getTypeNonNull() : typeComposers.output;
|
|
1544
1611
|
},
|
|
1545
1612
|
// Make sure you get the right property
|
|
1546
1613
|
resolve: root => {
|
|
1614
|
+
const typeComposers = subSchemaAndTypeComposers.properties[propertyName];
|
|
1547
1615
|
const actualFieldObj = root[propertyName];
|
|
1548
1616
|
if (actualFieldObj != null) {
|
|
1549
1617
|
const isArray = Array.isArray(actualFieldObj);
|
|
@@ -1557,26 +1625,39 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1557
1625
|
}
|
|
1558
1626
|
return actualFieldObj;
|
|
1559
1627
|
},
|
|
1560
|
-
description:
|
|
1628
|
+
description: subSchemaAndTypeComposers.properties[propertyName].description ||
|
|
1629
|
+
((_a = subSchemaAndTypeComposers.properties[propertyName].output) === null || _a === void 0 ? void 0 : _a.description),
|
|
1561
1630
|
};
|
|
1562
1631
|
inputFieldMap[fieldName] = {
|
|
1563
1632
|
type: () => {
|
|
1564
1633
|
var _a, _b;
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1634
|
+
const typeComposers = subSchemaAndTypeComposers.properties[propertyName];
|
|
1635
|
+
let nullable = true;
|
|
1636
|
+
if ((_a = subSchemaAndTypeComposers.required) === null || _a === void 0 ? void 0 : _a.includes(propertyName)) {
|
|
1637
|
+
nullable = false;
|
|
1638
|
+
}
|
|
1639
|
+
// Nullable has more priority
|
|
1640
|
+
if (typeComposers.nullable === false) {
|
|
1641
|
+
nullable = false;
|
|
1642
|
+
}
|
|
1643
|
+
if (typeComposers.nullable === true) {
|
|
1644
|
+
nullable = true;
|
|
1645
|
+
}
|
|
1646
|
+
return !nullable ? (_b = typeComposers.input) === null || _b === void 0 ? void 0 : _b.getTypeNonNull() : typeComposers.input;
|
|
1568
1647
|
},
|
|
1569
1648
|
// Let execution logic know what is the expected propertyName
|
|
1570
1649
|
extensions: {
|
|
1571
1650
|
propertyName,
|
|
1572
1651
|
},
|
|
1573
|
-
description:
|
|
1574
|
-
|
|
1652
|
+
description: subSchemaAndTypeComposers.properties[propertyName].description ||
|
|
1653
|
+
((_b = subSchemaAndTypeComposers.properties[propertyName].input) === null || _b === void 0 ? void 0 : _b.description),
|
|
1654
|
+
defaultValue: ((_d = (_c = subSchemaAndTypeComposers.properties[propertyName]) === null || _c === void 0 ? void 0 : _c.extensions) === null || _d === void 0 ? void 0 : _d.default) ||
|
|
1655
|
+
((_e = subSchemaAndTypeComposers.properties[propertyName].input) === null || _e === void 0 ? void 0 : _e.default),
|
|
1575
1656
|
};
|
|
1576
1657
|
}
|
|
1577
1658
|
}
|
|
1578
|
-
if (
|
|
1579
|
-
for (const typeComposers of
|
|
1659
|
+
if (subSchemaAndTypeComposers.allOf) {
|
|
1660
|
+
for (const typeComposers of subSchemaAndTypeComposers.allOf) {
|
|
1580
1661
|
const outputTC = typeComposers.output;
|
|
1581
1662
|
if (schemaComposer.isObjectType(outputTC)) {
|
|
1582
1663
|
for (const outputFieldName of outputTC.getFieldNames()) {
|
|
@@ -1595,19 +1676,19 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1595
1676
|
}
|
|
1596
1677
|
}
|
|
1597
1678
|
}
|
|
1598
|
-
if (
|
|
1599
|
-
if (typeof
|
|
1600
|
-
|
|
1679
|
+
if (subSchemaAndTypeComposers.additionalProperties) {
|
|
1680
|
+
if (typeof subSchemaAndTypeComposers.additionalProperties === 'object' &&
|
|
1681
|
+
subSchemaAndTypeComposers.additionalProperties.output instanceof ObjectTypeComposer) {
|
|
1601
1682
|
if (Object.keys(fieldMap).length === 0) {
|
|
1602
|
-
return
|
|
1683
|
+
return subSchemaAndTypeComposers.additionalProperties;
|
|
1603
1684
|
}
|
|
1604
1685
|
else {
|
|
1605
|
-
const outputTC =
|
|
1686
|
+
const outputTC = subSchemaAndTypeComposers.additionalProperties.output;
|
|
1606
1687
|
const outputTCFieldMap = outputTC.getFields();
|
|
1607
1688
|
for (const fieldName in outputTCFieldMap) {
|
|
1608
1689
|
fieldMap[fieldName] = outputTCFieldMap[fieldName];
|
|
1609
1690
|
}
|
|
1610
|
-
const inputTC =
|
|
1691
|
+
const inputTC = subSchemaAndTypeComposers.additionalProperties.input;
|
|
1611
1692
|
const inputTCFieldMap = inputTC.getFields();
|
|
1612
1693
|
for (const fieldName in inputTCFieldMap) {
|
|
1613
1694
|
inputFieldMap[fieldName] = inputTCFieldMap[fieldName];
|
|
@@ -1622,33 +1703,6 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1622
1703
|
inputFieldMap = {};
|
|
1623
1704
|
}
|
|
1624
1705
|
}
|
|
1625
|
-
if (subSchema.title === '_schema') {
|
|
1626
|
-
futureTasks.forEach(futureTask => futureTask());
|
|
1627
|
-
return {
|
|
1628
|
-
output: schemaComposer,
|
|
1629
|
-
};
|
|
1630
|
-
}
|
|
1631
|
-
if (subSchema.title === 'Query') {
|
|
1632
|
-
const typeComposer = schemaComposer.Query;
|
|
1633
|
-
typeComposer.addFields(fieldMap);
|
|
1634
|
-
return {
|
|
1635
|
-
output: typeComposer,
|
|
1636
|
-
};
|
|
1637
|
-
}
|
|
1638
|
-
if (subSchema.title === 'Mutation') {
|
|
1639
|
-
const typeComposer = schemaComposer.Mutation;
|
|
1640
|
-
typeComposer.addFields(fieldMap);
|
|
1641
|
-
return {
|
|
1642
|
-
output: typeComposer,
|
|
1643
|
-
};
|
|
1644
|
-
}
|
|
1645
|
-
if (subSchema.title === 'Subscription') {
|
|
1646
|
-
const typeComposer = schemaComposer.Subscription;
|
|
1647
|
-
typeComposer.addFields(fieldMap);
|
|
1648
|
-
return {
|
|
1649
|
-
output: typeComposer,
|
|
1650
|
-
};
|
|
1651
|
-
}
|
|
1652
1706
|
const getCorrectInputFieldType = (fieldName) => {
|
|
1653
1707
|
const inputType = inputFieldMap[fieldName].type();
|
|
1654
1708
|
const actualInputType = isListTC(inputType) ? inputType.ofType : inputType;
|
|
@@ -1664,112 +1718,77 @@ function getComposerFromJSONSchema(schema, logger, generateInterfaceFromSharedFi
|
|
|
1664
1718
|
}
|
|
1665
1719
|
return inputType;
|
|
1666
1720
|
};
|
|
1667
|
-
if (
|
|
1668
|
-
const typeComposer =
|
|
1669
|
-
for (const fieldName in inputFieldMap) {
|
|
1670
|
-
futureTasks.add(() => typeComposer.addFieldArgs(fieldName, {
|
|
1671
|
-
input: {
|
|
1672
|
-
type: () => getCorrectInputFieldType(fieldName),
|
|
1673
|
-
description: inputFieldMap[fieldName].description,
|
|
1674
|
-
},
|
|
1675
|
-
}));
|
|
1676
|
-
}
|
|
1677
|
-
return {
|
|
1678
|
-
output: typeComposer,
|
|
1679
|
-
};
|
|
1680
|
-
}
|
|
1681
|
-
if (subSchema.title === 'MutationInput') {
|
|
1682
|
-
const typeComposer = schemaComposer.Mutation;
|
|
1683
|
-
for (const fieldName in inputFieldMap) {
|
|
1684
|
-
futureTasks.add(() => typeComposer.addFieldArgs(fieldName, {
|
|
1685
|
-
input: {
|
|
1686
|
-
type: () => getCorrectInputFieldType(fieldName),
|
|
1687
|
-
description: inputFieldMap[fieldName].description,
|
|
1688
|
-
},
|
|
1689
|
-
}));
|
|
1690
|
-
}
|
|
1691
|
-
return {
|
|
1692
|
-
output: typeComposer,
|
|
1693
|
-
};
|
|
1694
|
-
}
|
|
1695
|
-
if (subSchema.title === 'SubscriptionInput') {
|
|
1696
|
-
const typeComposer = schemaComposer.Subscription;
|
|
1721
|
+
if (subSchemaAndTypeComposers.title in rootInputTypeNameComposerMap) {
|
|
1722
|
+
const typeComposer = rootInputTypeNameComposerMap[subSchemaAndTypeComposers.title]();
|
|
1697
1723
|
for (const fieldName in inputFieldMap) {
|
|
1698
|
-
|
|
1724
|
+
typeComposer.addFieldArgs(fieldName, {
|
|
1699
1725
|
input: {
|
|
1700
1726
|
type: () => getCorrectInputFieldType(fieldName),
|
|
1701
1727
|
description: inputFieldMap[fieldName].description,
|
|
1702
1728
|
},
|
|
1703
|
-
})
|
|
1729
|
+
});
|
|
1704
1730
|
}
|
|
1705
1731
|
return {
|
|
1706
1732
|
output: typeComposer,
|
|
1707
1733
|
};
|
|
1708
1734
|
}
|
|
1709
|
-
|
|
1710
|
-
|
|
1735
|
+
let output = subSchemaAndTypeComposers.output;
|
|
1736
|
+
if (Object.keys(fieldMap).length === 0) {
|
|
1737
|
+
output = getGenericJSONScalar({
|
|
1711
1738
|
schemaComposer,
|
|
1712
1739
|
isInput: false,
|
|
1713
|
-
subSchema,
|
|
1740
|
+
subSchema: subSchemaOnly,
|
|
1714
1741
|
validateWithJSONSchema,
|
|
1715
|
-
})
|
|
1716
|
-
: schemaComposer.createObjectTC({
|
|
1717
|
-
name: getValidTypeName({
|
|
1718
|
-
schemaComposer,
|
|
1719
|
-
isInput: false,
|
|
1720
|
-
subSchema,
|
|
1721
|
-
}),
|
|
1722
|
-
description: subSchema.description,
|
|
1723
|
-
fields: fieldMap,
|
|
1724
|
-
extensions: {
|
|
1725
|
-
validateWithJSONSchema,
|
|
1726
|
-
examples: subSchema.examples,
|
|
1727
|
-
default: subSchema.default,
|
|
1728
|
-
},
|
|
1729
1742
|
});
|
|
1730
|
-
|
|
1731
|
-
|
|
1743
|
+
}
|
|
1744
|
+
else if ('addFields' in output) {
|
|
1745
|
+
output.addFields(fieldMap);
|
|
1746
|
+
}
|
|
1747
|
+
let input = subSchemaAndTypeComposers.input;
|
|
1748
|
+
if (Object.keys(inputFieldMap).length === 0) {
|
|
1749
|
+
input = getGenericJSONScalar({
|
|
1732
1750
|
schemaComposer,
|
|
1733
1751
|
isInput: true,
|
|
1734
|
-
subSchema,
|
|
1752
|
+
subSchema: subSchemaOnly,
|
|
1735
1753
|
validateWithJSONSchema,
|
|
1736
|
-
})
|
|
1737
|
-
: schemaComposer.createInputTC({
|
|
1738
|
-
name: getValidTypeName({
|
|
1739
|
-
schemaComposer,
|
|
1740
|
-
isInput: true,
|
|
1741
|
-
subSchema,
|
|
1742
|
-
}),
|
|
1743
|
-
description: subSchema.description,
|
|
1744
|
-
fields: inputFieldMap,
|
|
1745
|
-
extensions: {
|
|
1746
|
-
examples: subSchema.examples,
|
|
1747
|
-
default: subSchema.default,
|
|
1748
|
-
},
|
|
1749
1754
|
});
|
|
1755
|
+
}
|
|
1756
|
+
else if (input != null && 'addFields' in input) {
|
|
1757
|
+
input.addFields(inputFieldMap);
|
|
1758
|
+
}
|
|
1750
1759
|
return {
|
|
1751
1760
|
input,
|
|
1752
1761
|
output,
|
|
1762
|
+
nullable: subSchemaAndTypeComposers.nullable,
|
|
1753
1763
|
};
|
|
1754
1764
|
}
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1765
|
+
if (subSchemaAndTypeComposers.input || subSchemaAndTypeComposers.output) {
|
|
1766
|
+
return {
|
|
1767
|
+
input: subSchemaAndTypeComposers.input,
|
|
1768
|
+
output: subSchemaAndTypeComposers.output,
|
|
1769
|
+
description: subSchemaAndTypeComposers.description,
|
|
1770
|
+
nullable: subSchemaAndTypeComposers.nullable,
|
|
1771
|
+
};
|
|
1772
|
+
}
|
|
1773
|
+
else {
|
|
1774
|
+
logger.warn(`GraphQL Type cannot be created for this JSON Schema definition;`, {
|
|
1775
|
+
subSchema: subSchemaOnly,
|
|
1776
|
+
path,
|
|
1777
|
+
});
|
|
1778
|
+
const typeComposer = schemaComposer.getAnyTC(GraphQLJSON$1);
|
|
1779
|
+
return {
|
|
1780
|
+
input: typeComposer,
|
|
1781
|
+
output: typeComposer,
|
|
1782
|
+
nullable: subSchemaAndTypeComposers.nullable,
|
|
1783
|
+
};
|
|
1784
|
+
}
|
|
1785
|
+
},
|
|
1767
1786
|
});
|
|
1768
1787
|
}
|
|
1769
1788
|
|
|
1770
|
-
async function getGraphQLSchemaFromDereferencedJSONSchema(fullyDeferencedSchema, { fetch, logger, operations, operationHeaders, baseUrl, pubsub,
|
|
1789
|
+
async function getGraphQLSchemaFromDereferencedJSONSchema(fullyDeferencedSchema, { fetch, logger, operations, operationHeaders, baseUrl, pubsub, queryParams, queryStringOptions, }) {
|
|
1771
1790
|
logger.debug(`Generating GraphQL Schema from the bundled JSON Schema`);
|
|
1772
|
-
const visitorResult = await getComposerFromJSONSchema(fullyDeferencedSchema, logger
|
|
1791
|
+
const visitorResult = await getComposerFromJSONSchema(fullyDeferencedSchema, logger);
|
|
1773
1792
|
const schemaComposerWithoutExecutionLogic = visitorResult.output;
|
|
1774
1793
|
if (!(schemaComposerWithoutExecutionLogic instanceof SchemaComposer)) {
|
|
1775
1794
|
throw new Error('The visitor result should be a SchemaComposer instance.');
|
|
@@ -1819,7 +1838,6 @@ async function loadGraphQLSchemaFromJSONSchemas(name, options) {
|
|
|
1819
1838
|
operationHeaders: options.operationHeaders,
|
|
1820
1839
|
baseUrl: options.baseUrl,
|
|
1821
1840
|
pubsub: options.pubsub,
|
|
1822
|
-
generateInterfaceFromSharedFields: options.generateInterfaceFromSharedFields,
|
|
1823
1841
|
queryParams: options.queryParams,
|
|
1824
1842
|
queryStringOptions: options.queryStringOptions,
|
|
1825
1843
|
});
|