sofa-api 0.15.2 → 0.15.3

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 +20 -9
  2. package/index.mjs +20 -9
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -12,6 +12,7 @@ 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');
15
16
 
16
17
  function getOperationInfo(doc) {
17
18
  const op = graphql.getOperationAST(doc, null);
@@ -713,6 +714,7 @@ function resolveFieldType(type, opts) {
713
714
 
714
715
  function buildPathFromOperation({ url, schema, operation, useRequestBody, tags, description, customScalars, }) {
715
716
  const info = getOperationInfo(operation);
717
+ const enumTypes = resolveEnumTypes(schema);
716
718
  const summary = resolveSummary(schema, info.operation);
717
719
  return Object.assign(Object.assign({ tags,
718
720
  description,
@@ -722,14 +724,14 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
722
724
  content: {
723
725
  'application/json': {
724
726
  schema: resolveRequestBody(info.operation.variableDefinitions, {
725
- customScalars,
727
+ customScalars, enumTypes
726
728
  }),
727
729
  },
728
730
  },
729
731
  },
730
732
  }
731
733
  : {
732
- parameters: resolveParameters(url, info.operation.variableDefinitions, { customScalars }),
734
+ parameters: resolveParameters(url, info.operation.variableDefinitions, { customScalars, enumTypes }),
733
735
  })), { responses: {
734
736
  200: {
735
737
  description: summary,
@@ -738,13 +740,22 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
738
740
  schema: resolveResponse({
739
741
  schema,
740
742
  operation: info.operation,
741
- customScalars,
743
+ opts: { customScalars, enumTypes },
742
744
  }),
743
745
  },
744
746
  },
745
747
  },
746
748
  } });
747
749
  }
750
+ 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,
753
+ {
754
+ type: 'string',
755
+ enum: type.getValues().map(value => value.name),
756
+ },
757
+ ])));
758
+ }
748
759
  function resolveParameters(url, variables, opts) {
749
760
  if (!variables) {
750
761
  return [];
@@ -787,23 +798,23 @@ 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] ||
803
+ { $ref: mapToRef(type.name.value) });
793
804
  }
794
- function resolveResponse({ schema, operation, customScalars, }) {
805
+ function resolveResponse({ schema, operation, opts, }) {
795
806
  const operationType = operation.operation;
796
807
  const rootField = operation.selectionSet.selections[0];
797
808
  if (rootField.kind === graphql.Kind.FIELD) {
798
809
  if (operationType === 'query') {
799
810
  const queryType = schema.getQueryType();
800
811
  const field = queryType.getFields()[rootField.name.value];
801
- return resolveFieldType(field.type, { customScalars });
812
+ return resolveFieldType(field.type, opts);
802
813
  }
803
814
  if (operationType === 'mutation') {
804
815
  const mutationType = schema.getMutationType();
805
816
  const field = mutationType.getFields()[rootField.name.value];
806
- return resolveFieldType(field.type, { customScalars });
817
+ return resolveFieldType(field.type, opts);
807
818
  }
808
819
  }
809
820
  }
package/index.mjs CHANGED
@@ -6,6 +6,7 @@ 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';
9
10
 
10
11
  function getOperationInfo(doc) {
11
12
  const op = getOperationAST(doc, null);
@@ -707,6 +708,7 @@ function resolveFieldType(type, opts) {
707
708
 
708
709
  function buildPathFromOperation({ url, schema, operation, useRequestBody, tags, description, customScalars, }) {
709
710
  const info = getOperationInfo(operation);
711
+ const enumTypes = resolveEnumTypes(schema);
710
712
  const summary = resolveSummary(schema, info.operation);
711
713
  return Object.assign(Object.assign({ tags,
712
714
  description,
@@ -716,14 +718,14 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
716
718
  content: {
717
719
  'application/json': {
718
720
  schema: resolveRequestBody(info.operation.variableDefinitions, {
719
- customScalars,
721
+ customScalars, enumTypes
720
722
  }),
721
723
  },
722
724
  },
723
725
  },
724
726
  }
725
727
  : {
726
- parameters: resolveParameters(url, info.operation.variableDefinitions, { customScalars }),
728
+ parameters: resolveParameters(url, info.operation.variableDefinitions, { customScalars, enumTypes }),
727
729
  })), { responses: {
728
730
  200: {
729
731
  description: summary,
@@ -732,13 +734,22 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
732
734
  schema: resolveResponse({
733
735
  schema,
734
736
  operation: info.operation,
735
- customScalars,
737
+ opts: { customScalars, enumTypes },
736
738
  }),
737
739
  },
738
740
  },
739
741
  },
740
742
  } });
741
743
  }
744
+ function resolveEnumTypes(schema) {
745
+ const enumTypes = Object.values(schema.getTypeMap()).filter(isEnumType).map(assertEnumType);
746
+ return Object.fromEntries(enumTypes.map(type => ([type.name,
747
+ {
748
+ type: 'string',
749
+ enum: type.getValues().map(value => value.name),
750
+ },
751
+ ])));
752
+ }
742
753
  function resolveParameters(url, variables, opts) {
743
754
  if (!variables) {
744
755
  return [];
@@ -781,23 +792,23 @@ 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] ||
797
+ { $ref: mapToRef(type.name.value) });
787
798
  }
788
- function resolveResponse({ schema, operation, customScalars, }) {
799
+ function resolveResponse({ schema, operation, opts, }) {
789
800
  const operationType = operation.operation;
790
801
  const rootField = operation.selectionSet.selections[0];
791
802
  if (rootField.kind === Kind.FIELD) {
792
803
  if (operationType === 'query') {
793
804
  const queryType = schema.getQueryType();
794
805
  const field = queryType.getFields()[rootField.name.value];
795
- return resolveFieldType(field.type, { customScalars });
806
+ return resolveFieldType(field.type, opts);
796
807
  }
797
808
  if (operationType === 'mutation') {
798
809
  const mutationType = schema.getMutationType();
799
810
  const field = mutationType.getFields()[rootField.name.value];
800
- return resolveFieldType(field.type, { customScalars });
811
+ return resolveFieldType(field.type, opts);
801
812
  }
802
813
  }
803
814
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sofa-api",
3
- "version": "0.15.2",
3
+ "version": "0.15.3",
4
4
  "description": "Create REST APIs with GraphQL",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {