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