sofa-api 0.15.3 → 0.15.5
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/index.js +36 -25
- package/index.mjs +36 -25
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -12,7 +12,6 @@ const fetch = require('@whatwg-node/fetch');
|
|
|
12
12
|
const colors = _interopDefault(require('ansi-colors'));
|
|
13
13
|
const router = require('@whatwg-node/router');
|
|
14
14
|
const titleCase = require('title-case');
|
|
15
|
-
const definition = require('graphql/type/definition');
|
|
16
15
|
|
|
17
16
|
function getOperationInfo(doc) {
|
|
18
17
|
const op = graphql.getOperationAST(doc, null);
|
|
@@ -580,6 +579,7 @@ function isContextFn(context) {
|
|
|
580
579
|
// they might represent an Object that is not a model
|
|
581
580
|
// We check it later, when an operation is being built
|
|
582
581
|
function extractsModels(schema) {
|
|
582
|
+
var _a, _b;
|
|
583
583
|
const modelMap = {};
|
|
584
584
|
const query = schema.getQueryType();
|
|
585
585
|
const fields = query.getFields();
|
|
@@ -599,7 +599,7 @@ function extractsModels(schema) {
|
|
|
599
599
|
// add to registry with `list: true`
|
|
600
600
|
const sameName = isNameEqual(field.name, namedType.name + 's');
|
|
601
601
|
const allOptionalArguments = !field.args.some((arg) => graphql.isNonNullType(arg.type));
|
|
602
|
-
modelMap[namedType.name].list = sameName && allOptionalArguments;
|
|
602
|
+
(_a = modelMap[namedType.name]).list || (_a.list = sameName && allOptionalArguments);
|
|
603
603
|
}
|
|
604
604
|
else if (graphql.isObjectType(field.type) ||
|
|
605
605
|
(graphql.isNonNullType(field.type) && graphql.isObjectType(field.type.ofType))) {
|
|
@@ -609,7 +609,7 @@ function extractsModels(schema) {
|
|
|
609
609
|
// add to registry with `single: true`
|
|
610
610
|
const sameName = isNameEqual(field.name, namedType.name);
|
|
611
611
|
const hasIdArgument = field.args.length === 1 && field.args[0].name === 'id';
|
|
612
|
-
modelMap[namedType.name].single = sameName && hasIdArgument;
|
|
612
|
+
(_b = modelMap[namedType.name]).single || (_b.single = sameName && hasIdArgument);
|
|
613
613
|
}
|
|
614
614
|
}
|
|
615
615
|
}
|
|
@@ -715,23 +715,25 @@ function resolveFieldType(type, opts) {
|
|
|
715
715
|
function buildPathFromOperation({ url, schema, operation, useRequestBody, tags, description, customScalars, }) {
|
|
716
716
|
const info = getOperationInfo(operation);
|
|
717
717
|
const enumTypes = resolveEnumTypes(schema);
|
|
718
|
-
const summary =
|
|
718
|
+
const summary = resolveDescription(schema, info.operation);
|
|
719
|
+
const variables = info.operation.variableDefinitions;
|
|
720
|
+
const pathParams = variables === null || variables === void 0 ? void 0 : variables.filter((variable) => isInPath(url, variable.variable.name.value));
|
|
721
|
+
const bodyParams = variables === null || variables === void 0 ? void 0 : variables.filter((variable) => !isInPath(url, variable.variable.name.value));
|
|
719
722
|
return Object.assign(Object.assign({ tags,
|
|
720
723
|
description,
|
|
721
724
|
summary, operationId: info.name }, (useRequestBody
|
|
722
725
|
? {
|
|
726
|
+
parameters: resolveParameters(url, pathParams, schema, info.operation, { customScalars, enumTypes }),
|
|
723
727
|
requestBody: {
|
|
724
728
|
content: {
|
|
725
729
|
'application/json': {
|
|
726
|
-
schema: resolveRequestBody(info.operation
|
|
727
|
-
customScalars, enumTypes
|
|
728
|
-
}),
|
|
730
|
+
schema: resolveRequestBody(bodyParams, schema, info.operation, { customScalars, enumTypes }),
|
|
729
731
|
},
|
|
730
732
|
},
|
|
731
733
|
},
|
|
732
734
|
}
|
|
733
735
|
: {
|
|
734
|
-
parameters: resolveParameters(url, info.operation
|
|
736
|
+
parameters: resolveParameters(url, variables, schema, info.operation, { customScalars, enumTypes }),
|
|
735
737
|
})), { responses: {
|
|
736
738
|
200: {
|
|
737
739
|
description: summary,
|
|
@@ -748,15 +750,17 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
|
|
|
748
750
|
} });
|
|
749
751
|
}
|
|
750
752
|
function resolveEnumTypes(schema) {
|
|
751
|
-
const enumTypes = Object.values(schema.getTypeMap())
|
|
752
|
-
|
|
753
|
+
const enumTypes = Object.values(schema.getTypeMap())
|
|
754
|
+
.filter(graphql.isEnumType);
|
|
755
|
+
return Object.fromEntries(enumTypes.map((type) => [
|
|
756
|
+
type.name,
|
|
753
757
|
{
|
|
754
758
|
type: 'string',
|
|
755
|
-
enum: type.getValues().map(value => value.name),
|
|
759
|
+
enum: type.getValues().map((value) => value.name),
|
|
756
760
|
},
|
|
757
|
-
]))
|
|
761
|
+
]));
|
|
758
762
|
}
|
|
759
|
-
function resolveParameters(url, variables, opts) {
|
|
763
|
+
function resolveParameters(url, variables, schema, operation, opts) {
|
|
760
764
|
if (!variables) {
|
|
761
765
|
return [];
|
|
762
766
|
}
|
|
@@ -766,10 +770,11 @@ function resolveParameters(url, variables, opts) {
|
|
|
766
770
|
name: variable.variable.name.value,
|
|
767
771
|
required: variable.type.kind === graphql.Kind.NON_NULL_TYPE,
|
|
768
772
|
schema: resolveParamSchema(variable.type, opts),
|
|
773
|
+
description: resolveVariableDescription(schema, operation, variable),
|
|
769
774
|
};
|
|
770
775
|
});
|
|
771
776
|
}
|
|
772
|
-
function resolveRequestBody(variables, opts) {
|
|
777
|
+
function resolveRequestBody(variables, schema, operation, opts) {
|
|
773
778
|
if (!variables) {
|
|
774
779
|
return {};
|
|
775
780
|
}
|
|
@@ -779,7 +784,7 @@ function resolveRequestBody(variables, opts) {
|
|
|
779
784
|
if (variable.type.kind === graphql.Kind.NON_NULL_TYPE) {
|
|
780
785
|
required.push(variable.variable.name.value);
|
|
781
786
|
}
|
|
782
|
-
properties[variable.variable.name.value] = resolveParamSchema(variable.type, opts);
|
|
787
|
+
properties[variable.variable.name.value] = Object.assign(Object.assign({}, resolveParamSchema(variable.type, opts)), { description: resolveVariableDescription(schema, operation, variable) });
|
|
783
788
|
});
|
|
784
789
|
return Object.assign({ type: 'object', properties }, (required.length ? { required } : {}));
|
|
785
790
|
}
|
|
@@ -799,8 +804,7 @@ function resolveParamSchema(type, opts) {
|
|
|
799
804
|
const primitive = mapToPrimitive(type.name.value);
|
|
800
805
|
return (primitive ||
|
|
801
806
|
opts.customScalars[type.name.value] ||
|
|
802
|
-
opts.enumTypes[type.name.value] ||
|
|
803
|
-
{ $ref: mapToRef(type.name.value) });
|
|
807
|
+
opts.enumTypes[type.name.value] || { $ref: mapToRef(type.name.value) });
|
|
804
808
|
}
|
|
805
809
|
function resolveResponse({ schema, operation, opts, }) {
|
|
806
810
|
const operationType = operation.operation;
|
|
@@ -821,22 +825,29 @@ function resolveResponse({ schema, operation, opts, }) {
|
|
|
821
825
|
function isInPath(url, param) {
|
|
822
826
|
return url.indexOf(`{${param}}`) !== -1;
|
|
823
827
|
}
|
|
824
|
-
function
|
|
828
|
+
function getOperationFieldNode(schema, operation) {
|
|
825
829
|
const selection = operation.selectionSet.selections[0];
|
|
826
830
|
const fieldName = selection.name.value;
|
|
827
831
|
const typeDefinition = schema.getType(titleCase.titleCase(operation.operation));
|
|
828
832
|
if (!typeDefinition) {
|
|
829
|
-
return
|
|
833
|
+
return undefined;
|
|
830
834
|
}
|
|
831
835
|
const definitionNode = typeDefinition.astNode || graphql.parse(graphql.printType(typeDefinition)).definitions[0];
|
|
832
836
|
if (!isObjectTypeDefinitionNode(definitionNode)) {
|
|
833
|
-
return
|
|
837
|
+
return undefined;
|
|
834
838
|
}
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
839
|
+
return definitionNode.fields.find((field) => field.name.value === fieldName);
|
|
840
|
+
}
|
|
841
|
+
function resolveDescription(schema, operation) {
|
|
842
|
+
var _a;
|
|
843
|
+
const fieldNode = getOperationFieldNode(schema, operation);
|
|
844
|
+
return ((_a = fieldNode === null || fieldNode === void 0 ? void 0 : fieldNode.description) === null || _a === void 0 ? void 0 : _a.value) || '';
|
|
845
|
+
}
|
|
846
|
+
function resolveVariableDescription(schema, operation, variable) {
|
|
847
|
+
var _a, _b;
|
|
848
|
+
const fieldNode = getOperationFieldNode(schema, operation);
|
|
849
|
+
const argument = (_a = fieldNode === null || fieldNode === void 0 ? void 0 : fieldNode.arguments) === null || _a === void 0 ? void 0 : _a.find((arg) => arg.name.value === variable.variable.name.value);
|
|
850
|
+
return (_b = argument === null || argument === void 0 ? void 0 : argument.description) === null || _b === void 0 ? void 0 : _b.value;
|
|
840
851
|
}
|
|
841
852
|
function isObjectTypeDefinitionNode(node) {
|
|
842
853
|
return node.kind === graphql.Kind.OBJECT_TYPE_DEFINITION;
|
package/index.mjs
CHANGED
|
@@ -6,7 +6,6 @@ import { crypto, fetch } from '@whatwg-node/fetch';
|
|
|
6
6
|
import colors from 'ansi-colors';
|
|
7
7
|
import { createRouter as createRouter$1, Response } from '@whatwg-node/router';
|
|
8
8
|
import { titleCase } from 'title-case';
|
|
9
|
-
import { assertEnumType } from 'graphql/type/definition';
|
|
10
9
|
|
|
11
10
|
function getOperationInfo(doc) {
|
|
12
11
|
const op = getOperationAST(doc, null);
|
|
@@ -574,6 +573,7 @@ function isContextFn(context) {
|
|
|
574
573
|
// they might represent an Object that is not a model
|
|
575
574
|
// We check it later, when an operation is being built
|
|
576
575
|
function extractsModels(schema) {
|
|
576
|
+
var _a, _b;
|
|
577
577
|
const modelMap = {};
|
|
578
578
|
const query = schema.getQueryType();
|
|
579
579
|
const fields = query.getFields();
|
|
@@ -593,7 +593,7 @@ function extractsModels(schema) {
|
|
|
593
593
|
// add to registry with `list: true`
|
|
594
594
|
const sameName = isNameEqual(field.name, namedType.name + 's');
|
|
595
595
|
const allOptionalArguments = !field.args.some((arg) => isNonNullType(arg.type));
|
|
596
|
-
modelMap[namedType.name].list = sameName && allOptionalArguments;
|
|
596
|
+
(_a = modelMap[namedType.name]).list || (_a.list = sameName && allOptionalArguments);
|
|
597
597
|
}
|
|
598
598
|
else if (isObjectType(field.type) ||
|
|
599
599
|
(isNonNullType(field.type) && isObjectType(field.type.ofType))) {
|
|
@@ -603,7 +603,7 @@ function extractsModels(schema) {
|
|
|
603
603
|
// add to registry with `single: true`
|
|
604
604
|
const sameName = isNameEqual(field.name, namedType.name);
|
|
605
605
|
const hasIdArgument = field.args.length === 1 && field.args[0].name === 'id';
|
|
606
|
-
modelMap[namedType.name].single = sameName && hasIdArgument;
|
|
606
|
+
(_b = modelMap[namedType.name]).single || (_b.single = sameName && hasIdArgument);
|
|
607
607
|
}
|
|
608
608
|
}
|
|
609
609
|
}
|
|
@@ -709,23 +709,25 @@ function resolveFieldType(type, opts) {
|
|
|
709
709
|
function buildPathFromOperation({ url, schema, operation, useRequestBody, tags, description, customScalars, }) {
|
|
710
710
|
const info = getOperationInfo(operation);
|
|
711
711
|
const enumTypes = resolveEnumTypes(schema);
|
|
712
|
-
const summary =
|
|
712
|
+
const summary = resolveDescription(schema, info.operation);
|
|
713
|
+
const variables = info.operation.variableDefinitions;
|
|
714
|
+
const pathParams = variables === null || variables === void 0 ? void 0 : variables.filter((variable) => isInPath(url, variable.variable.name.value));
|
|
715
|
+
const bodyParams = variables === null || variables === void 0 ? void 0 : variables.filter((variable) => !isInPath(url, variable.variable.name.value));
|
|
713
716
|
return Object.assign(Object.assign({ tags,
|
|
714
717
|
description,
|
|
715
718
|
summary, operationId: info.name }, (useRequestBody
|
|
716
719
|
? {
|
|
720
|
+
parameters: resolveParameters(url, pathParams, schema, info.operation, { customScalars, enumTypes }),
|
|
717
721
|
requestBody: {
|
|
718
722
|
content: {
|
|
719
723
|
'application/json': {
|
|
720
|
-
schema: resolveRequestBody(info.operation
|
|
721
|
-
customScalars, enumTypes
|
|
722
|
-
}),
|
|
724
|
+
schema: resolveRequestBody(bodyParams, schema, info.operation, { customScalars, enumTypes }),
|
|
723
725
|
},
|
|
724
726
|
},
|
|
725
727
|
},
|
|
726
728
|
}
|
|
727
729
|
: {
|
|
728
|
-
parameters: resolveParameters(url, info.operation
|
|
730
|
+
parameters: resolveParameters(url, variables, schema, info.operation, { customScalars, enumTypes }),
|
|
729
731
|
})), { responses: {
|
|
730
732
|
200: {
|
|
731
733
|
description: summary,
|
|
@@ -742,15 +744,17 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
|
|
|
742
744
|
} });
|
|
743
745
|
}
|
|
744
746
|
function resolveEnumTypes(schema) {
|
|
745
|
-
const enumTypes = Object.values(schema.getTypeMap())
|
|
746
|
-
|
|
747
|
+
const enumTypes = Object.values(schema.getTypeMap())
|
|
748
|
+
.filter(isEnumType);
|
|
749
|
+
return Object.fromEntries(enumTypes.map((type) => [
|
|
750
|
+
type.name,
|
|
747
751
|
{
|
|
748
752
|
type: 'string',
|
|
749
|
-
enum: type.getValues().map(value => value.name),
|
|
753
|
+
enum: type.getValues().map((value) => value.name),
|
|
750
754
|
},
|
|
751
|
-
]))
|
|
755
|
+
]));
|
|
752
756
|
}
|
|
753
|
-
function resolveParameters(url, variables, opts) {
|
|
757
|
+
function resolveParameters(url, variables, schema, operation, opts) {
|
|
754
758
|
if (!variables) {
|
|
755
759
|
return [];
|
|
756
760
|
}
|
|
@@ -760,10 +764,11 @@ function resolveParameters(url, variables, opts) {
|
|
|
760
764
|
name: variable.variable.name.value,
|
|
761
765
|
required: variable.type.kind === Kind.NON_NULL_TYPE,
|
|
762
766
|
schema: resolveParamSchema(variable.type, opts),
|
|
767
|
+
description: resolveVariableDescription(schema, operation, variable),
|
|
763
768
|
};
|
|
764
769
|
});
|
|
765
770
|
}
|
|
766
|
-
function resolveRequestBody(variables, opts) {
|
|
771
|
+
function resolveRequestBody(variables, schema, operation, opts) {
|
|
767
772
|
if (!variables) {
|
|
768
773
|
return {};
|
|
769
774
|
}
|
|
@@ -773,7 +778,7 @@ function resolveRequestBody(variables, opts) {
|
|
|
773
778
|
if (variable.type.kind === Kind.NON_NULL_TYPE) {
|
|
774
779
|
required.push(variable.variable.name.value);
|
|
775
780
|
}
|
|
776
|
-
properties[variable.variable.name.value] = resolveParamSchema(variable.type, opts);
|
|
781
|
+
properties[variable.variable.name.value] = Object.assign(Object.assign({}, resolveParamSchema(variable.type, opts)), { description: resolveVariableDescription(schema, operation, variable) });
|
|
777
782
|
});
|
|
778
783
|
return Object.assign({ type: 'object', properties }, (required.length ? { required } : {}));
|
|
779
784
|
}
|
|
@@ -793,8 +798,7 @@ function resolveParamSchema(type, opts) {
|
|
|
793
798
|
const primitive = mapToPrimitive(type.name.value);
|
|
794
799
|
return (primitive ||
|
|
795
800
|
opts.customScalars[type.name.value] ||
|
|
796
|
-
opts.enumTypes[type.name.value] ||
|
|
797
|
-
{ $ref: mapToRef(type.name.value) });
|
|
801
|
+
opts.enumTypes[type.name.value] || { $ref: mapToRef(type.name.value) });
|
|
798
802
|
}
|
|
799
803
|
function resolveResponse({ schema, operation, opts, }) {
|
|
800
804
|
const operationType = operation.operation;
|
|
@@ -815,22 +819,29 @@ function resolveResponse({ schema, operation, opts, }) {
|
|
|
815
819
|
function isInPath(url, param) {
|
|
816
820
|
return url.indexOf(`{${param}}`) !== -1;
|
|
817
821
|
}
|
|
818
|
-
function
|
|
822
|
+
function getOperationFieldNode(schema, operation) {
|
|
819
823
|
const selection = operation.selectionSet.selections[0];
|
|
820
824
|
const fieldName = selection.name.value;
|
|
821
825
|
const typeDefinition = schema.getType(titleCase(operation.operation));
|
|
822
826
|
if (!typeDefinition) {
|
|
823
|
-
return
|
|
827
|
+
return undefined;
|
|
824
828
|
}
|
|
825
829
|
const definitionNode = typeDefinition.astNode || parse(printType(typeDefinition)).definitions[0];
|
|
826
830
|
if (!isObjectTypeDefinitionNode(definitionNode)) {
|
|
827
|
-
return
|
|
831
|
+
return undefined;
|
|
828
832
|
}
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
833
|
+
return definitionNode.fields.find((field) => field.name.value === fieldName);
|
|
834
|
+
}
|
|
835
|
+
function resolveDescription(schema, operation) {
|
|
836
|
+
var _a;
|
|
837
|
+
const fieldNode = getOperationFieldNode(schema, operation);
|
|
838
|
+
return ((_a = fieldNode === null || fieldNode === void 0 ? void 0 : fieldNode.description) === null || _a === void 0 ? void 0 : _a.value) || '';
|
|
839
|
+
}
|
|
840
|
+
function resolveVariableDescription(schema, operation, variable) {
|
|
841
|
+
var _a, _b;
|
|
842
|
+
const fieldNode = getOperationFieldNode(schema, operation);
|
|
843
|
+
const argument = (_a = fieldNode === null || fieldNode === void 0 ? void 0 : fieldNode.arguments) === null || _a === void 0 ? void 0 : _a.find((arg) => arg.name.value === variable.variable.name.value);
|
|
844
|
+
return (_b = argument === null || argument === void 0 ? void 0 : argument.description) === null || _b === void 0 ? void 0 : _b.value;
|
|
834
845
|
}
|
|
835
846
|
function isObjectTypeDefinitionNode(node) {
|
|
836
847
|
return node.kind === Kind.OBJECT_TYPE_DEFINITION;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sofa-api",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.5",
|
|
4
4
|
"description": "Create REST APIs with GraphQL",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"peerDependencies": {
|
|
7
7
|
"graphql": "^0.13.2 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
|
-
"@graphql-tools/utils": "9.1.
|
|
10
|
+
"@graphql-tools/utils": "9.1.4",
|
|
11
11
|
"@whatwg-node/fetch": "^0.6.0",
|
|
12
12
|
"@whatwg-node/router": "^0.1.2",
|
|
13
13
|
"ansi-colors": "4.1.3",
|