sofa-api 0.15.2 → 0.15.4

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.
Files changed (3) hide show
  1. package/index.js +40 -23
  2. package/index.mjs +40 -23
  3. package/package.json +2 -2
package/index.js CHANGED
@@ -713,7 +713,8 @@ function resolveFieldType(type, opts) {
713
713
 
714
714
  function buildPathFromOperation({ url, schema, operation, useRequestBody, tags, description, customScalars, }) {
715
715
  const info = getOperationInfo(operation);
716
- const summary = resolveSummary(schema, info.operation);
716
+ const enumTypes = resolveEnumTypes(schema);
717
+ const summary = resolveDescription(schema, info.operation);
717
718
  return Object.assign(Object.assign({ tags,
718
719
  description,
719
720
  summary, operationId: info.name }, (useRequestBody
@@ -721,15 +722,13 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
721
722
  requestBody: {
722
723
  content: {
723
724
  'application/json': {
724
- schema: resolveRequestBody(info.operation.variableDefinitions, {
725
- customScalars,
726
- }),
725
+ schema: resolveRequestBody(info.operation.variableDefinitions, schema, info.operation, { customScalars, enumTypes }),
727
726
  },
728
727
  },
729
728
  },
730
729
  }
731
730
  : {
732
- parameters: resolveParameters(url, info.operation.variableDefinitions, { customScalars }),
731
+ parameters: resolveParameters(url, info.operation.variableDefinitions, schema, info.operation, { customScalars, enumTypes }),
733
732
  })), { responses: {
734
733
  200: {
735
734
  description: summary,
@@ -738,14 +737,25 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
738
737
  schema: resolveResponse({
739
738
  schema,
740
739
  operation: info.operation,
741
- customScalars,
740
+ opts: { customScalars, enumTypes },
742
741
  }),
743
742
  },
744
743
  },
745
744
  },
746
745
  } });
747
746
  }
748
- function resolveParameters(url, variables, opts) {
747
+ function resolveEnumTypes(schema) {
748
+ const enumTypes = Object.values(schema.getTypeMap())
749
+ .filter(graphql.isEnumType);
750
+ return Object.fromEntries(enumTypes.map((type) => [
751
+ type.name,
752
+ {
753
+ type: 'string',
754
+ enum: type.getValues().map((value) => value.name),
755
+ },
756
+ ]));
757
+ }
758
+ function resolveParameters(url, variables, schema, operation, opts) {
749
759
  if (!variables) {
750
760
  return [];
751
761
  }
@@ -755,10 +765,11 @@ function resolveParameters(url, variables, opts) {
755
765
  name: variable.variable.name.value,
756
766
  required: variable.type.kind === graphql.Kind.NON_NULL_TYPE,
757
767
  schema: resolveParamSchema(variable.type, opts),
768
+ description: resolveVariableDescription(schema, operation, variable),
758
769
  };
759
770
  });
760
771
  }
761
- function resolveRequestBody(variables, opts) {
772
+ function resolveRequestBody(variables, schema, operation, opts) {
762
773
  if (!variables) {
763
774
  return {};
764
775
  }
@@ -768,7 +779,7 @@ function resolveRequestBody(variables, opts) {
768
779
  if (variable.type.kind === graphql.Kind.NON_NULL_TYPE) {
769
780
  required.push(variable.variable.name.value);
770
781
  }
771
- properties[variable.variable.name.value] = resolveParamSchema(variable.type, opts);
782
+ properties[variable.variable.name.value] = Object.assign(Object.assign({}, resolveParamSchema(variable.type, opts)), { description: resolveVariableDescription(schema, operation, variable) });
772
783
  });
773
784
  return Object.assign({ type: 'object', properties }, (required.length ? { required } : {}));
774
785
  }
@@ -787,45 +798,51 @@ function resolveParamSchema(type, opts) {
787
798
  }
788
799
  const primitive = mapToPrimitive(type.name.value);
789
800
  return (primitive ||
790
- opts.customScalars[type.name.value] || {
791
- $ref: mapToRef(type.name.value),
792
- });
801
+ opts.customScalars[type.name.value] ||
802
+ opts.enumTypes[type.name.value] || { $ref: mapToRef(type.name.value) });
793
803
  }
794
- function resolveResponse({ schema, operation, customScalars, }) {
804
+ function resolveResponse({ schema, operation, opts, }) {
795
805
  const operationType = operation.operation;
796
806
  const rootField = operation.selectionSet.selections[0];
797
807
  if (rootField.kind === graphql.Kind.FIELD) {
798
808
  if (operationType === 'query') {
799
809
  const queryType = schema.getQueryType();
800
810
  const field = queryType.getFields()[rootField.name.value];
801
- return resolveFieldType(field.type, { customScalars });
811
+ return resolveFieldType(field.type, opts);
802
812
  }
803
813
  if (operationType === 'mutation') {
804
814
  const mutationType = schema.getMutationType();
805
815
  const field = mutationType.getFields()[rootField.name.value];
806
- return resolveFieldType(field.type, { customScalars });
816
+ return resolveFieldType(field.type, opts);
807
817
  }
808
818
  }
809
819
  }
810
820
  function isInPath(url, param) {
811
821
  return url.indexOf(`{${param}}`) !== -1;
812
822
  }
813
- function resolveSummary(schema, operation) {
823
+ function getOperationFieldNode(schema, operation) {
814
824
  const selection = operation.selectionSet.selections[0];
815
825
  const fieldName = selection.name.value;
816
826
  const typeDefinition = schema.getType(titleCase.titleCase(operation.operation));
817
827
  if (!typeDefinition) {
818
- return '';
828
+ return undefined;
819
829
  }
820
830
  const definitionNode = typeDefinition.astNode || graphql.parse(graphql.printType(typeDefinition)).definitions[0];
821
831
  if (!isObjectTypeDefinitionNode(definitionNode)) {
822
- return '';
832
+ return undefined;
823
833
  }
824
- const fieldNode = definitionNode.fields.find((field) => field.name.value === fieldName);
825
- const descriptionDefinition = fieldNode && fieldNode.description;
826
- return descriptionDefinition && descriptionDefinition.value
827
- ? descriptionDefinition.value
828
- : '';
834
+ return definitionNode.fields.find((field) => field.name.value === fieldName);
835
+ }
836
+ function resolveDescription(schema, operation) {
837
+ var _a;
838
+ const fieldNode = getOperationFieldNode(schema, operation);
839
+ return ((_a = fieldNode === null || fieldNode === void 0 ? void 0 : fieldNode.description) === null || _a === void 0 ? void 0 : _a.value) || '';
840
+ }
841
+ function resolveVariableDescription(schema, operation, variable) {
842
+ var _a, _b;
843
+ const fieldNode = getOperationFieldNode(schema, operation);
844
+ 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);
845
+ return (_b = argument === null || argument === void 0 ? void 0 : argument.description) === null || _b === void 0 ? void 0 : _b.value;
829
846
  }
830
847
  function isObjectTypeDefinitionNode(node) {
831
848
  return node.kind === graphql.Kind.OBJECT_TYPE_DEFINITION;
package/index.mjs CHANGED
@@ -707,7 +707,8 @@ function resolveFieldType(type, opts) {
707
707
 
708
708
  function buildPathFromOperation({ url, schema, operation, useRequestBody, tags, description, customScalars, }) {
709
709
  const info = getOperationInfo(operation);
710
- const summary = resolveSummary(schema, info.operation);
710
+ const enumTypes = resolveEnumTypes(schema);
711
+ const summary = resolveDescription(schema, info.operation);
711
712
  return Object.assign(Object.assign({ tags,
712
713
  description,
713
714
  summary, operationId: info.name }, (useRequestBody
@@ -715,15 +716,13 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
715
716
  requestBody: {
716
717
  content: {
717
718
  'application/json': {
718
- schema: resolveRequestBody(info.operation.variableDefinitions, {
719
- customScalars,
720
- }),
719
+ schema: resolveRequestBody(info.operation.variableDefinitions, schema, info.operation, { customScalars, enumTypes }),
721
720
  },
722
721
  },
723
722
  },
724
723
  }
725
724
  : {
726
- parameters: resolveParameters(url, info.operation.variableDefinitions, { customScalars }),
725
+ parameters: resolveParameters(url, info.operation.variableDefinitions, schema, info.operation, { customScalars, enumTypes }),
727
726
  })), { responses: {
728
727
  200: {
729
728
  description: summary,
@@ -732,14 +731,25 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
732
731
  schema: resolveResponse({
733
732
  schema,
734
733
  operation: info.operation,
735
- customScalars,
734
+ opts: { customScalars, enumTypes },
736
735
  }),
737
736
  },
738
737
  },
739
738
  },
740
739
  } });
741
740
  }
742
- function resolveParameters(url, variables, opts) {
741
+ function resolveEnumTypes(schema) {
742
+ const enumTypes = Object.values(schema.getTypeMap())
743
+ .filter(isEnumType);
744
+ return Object.fromEntries(enumTypes.map((type) => [
745
+ type.name,
746
+ {
747
+ type: 'string',
748
+ enum: type.getValues().map((value) => value.name),
749
+ },
750
+ ]));
751
+ }
752
+ function resolveParameters(url, variables, schema, operation, opts) {
743
753
  if (!variables) {
744
754
  return [];
745
755
  }
@@ -749,10 +759,11 @@ function resolveParameters(url, variables, opts) {
749
759
  name: variable.variable.name.value,
750
760
  required: variable.type.kind === Kind.NON_NULL_TYPE,
751
761
  schema: resolveParamSchema(variable.type, opts),
762
+ description: resolveVariableDescription(schema, operation, variable),
752
763
  };
753
764
  });
754
765
  }
755
- function resolveRequestBody(variables, opts) {
766
+ function resolveRequestBody(variables, schema, operation, opts) {
756
767
  if (!variables) {
757
768
  return {};
758
769
  }
@@ -762,7 +773,7 @@ function resolveRequestBody(variables, opts) {
762
773
  if (variable.type.kind === Kind.NON_NULL_TYPE) {
763
774
  required.push(variable.variable.name.value);
764
775
  }
765
- properties[variable.variable.name.value] = resolveParamSchema(variable.type, opts);
776
+ properties[variable.variable.name.value] = Object.assign(Object.assign({}, resolveParamSchema(variable.type, opts)), { description: resolveVariableDescription(schema, operation, variable) });
766
777
  });
767
778
  return Object.assign({ type: 'object', properties }, (required.length ? { required } : {}));
768
779
  }
@@ -781,45 +792,51 @@ function resolveParamSchema(type, opts) {
781
792
  }
782
793
  const primitive = mapToPrimitive(type.name.value);
783
794
  return (primitive ||
784
- opts.customScalars[type.name.value] || {
785
- $ref: mapToRef(type.name.value),
786
- });
795
+ opts.customScalars[type.name.value] ||
796
+ opts.enumTypes[type.name.value] || { $ref: mapToRef(type.name.value) });
787
797
  }
788
- function resolveResponse({ schema, operation, customScalars, }) {
798
+ function resolveResponse({ schema, operation, opts, }) {
789
799
  const operationType = operation.operation;
790
800
  const rootField = operation.selectionSet.selections[0];
791
801
  if (rootField.kind === Kind.FIELD) {
792
802
  if (operationType === 'query') {
793
803
  const queryType = schema.getQueryType();
794
804
  const field = queryType.getFields()[rootField.name.value];
795
- return resolveFieldType(field.type, { customScalars });
805
+ return resolveFieldType(field.type, opts);
796
806
  }
797
807
  if (operationType === 'mutation') {
798
808
  const mutationType = schema.getMutationType();
799
809
  const field = mutationType.getFields()[rootField.name.value];
800
- return resolveFieldType(field.type, { customScalars });
810
+ return resolveFieldType(field.type, opts);
801
811
  }
802
812
  }
803
813
  }
804
814
  function isInPath(url, param) {
805
815
  return url.indexOf(`{${param}}`) !== -1;
806
816
  }
807
- function resolveSummary(schema, operation) {
817
+ function getOperationFieldNode(schema, operation) {
808
818
  const selection = operation.selectionSet.selections[0];
809
819
  const fieldName = selection.name.value;
810
820
  const typeDefinition = schema.getType(titleCase(operation.operation));
811
821
  if (!typeDefinition) {
812
- return '';
822
+ return undefined;
813
823
  }
814
824
  const definitionNode = typeDefinition.astNode || parse(printType(typeDefinition)).definitions[0];
815
825
  if (!isObjectTypeDefinitionNode(definitionNode)) {
816
- return '';
826
+ return undefined;
817
827
  }
818
- const fieldNode = definitionNode.fields.find((field) => field.name.value === fieldName);
819
- const descriptionDefinition = fieldNode && fieldNode.description;
820
- return descriptionDefinition && descriptionDefinition.value
821
- ? descriptionDefinition.value
822
- : '';
828
+ return definitionNode.fields.find((field) => field.name.value === fieldName);
829
+ }
830
+ function resolveDescription(schema, operation) {
831
+ var _a;
832
+ const fieldNode = getOperationFieldNode(schema, operation);
833
+ return ((_a = fieldNode === null || fieldNode === void 0 ? void 0 : fieldNode.description) === null || _a === void 0 ? void 0 : _a.value) || '';
834
+ }
835
+ function resolveVariableDescription(schema, operation, variable) {
836
+ var _a, _b;
837
+ const fieldNode = getOperationFieldNode(schema, operation);
838
+ 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);
839
+ return (_b = argument === null || argument === void 0 ? void 0 : argument.description) === null || _b === void 0 ? void 0 : _b.value;
823
840
  }
824
841
  function isObjectTypeDefinitionNode(node) {
825
842
  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.2",
3
+ "version": "0.15.4",
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.3",
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",