@graphql-mesh/runtime 0.96.8 → 0.96.9-alpha-20231001010331-3dd758d10

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/cjs/get-mesh.js CHANGED
@@ -12,6 +12,7 @@ const utils_2 = require("@graphql-tools/utils");
12
12
  const fetch_1 = require("@whatwg-node/fetch");
13
13
  const constants_js_1 = require("./constants.js");
14
14
  const in_context_sdk_js_1 = require("./in-context-sdk.js");
15
+ const sjs_js_1 = require("./sjs.js");
15
16
  const useSubschema_js_1 = require("./useSubschema.js");
16
17
  const utils_js_1 = require("./utils.js");
17
18
  const memoizedGetEnvelopedFactory = (0, utils_2.memoize1)(function getEnvelopedFactory(plugins) {
@@ -212,6 +213,7 @@ async function getMesh(options) {
212
213
  return !(0, utils_js_1.isStreamOperation)(args.document);
213
214
  },
214
215
  }),
216
+ (0, sjs_js_1.useSJS)(),
215
217
  ]),
216
218
  (0, core_1.useExtendContext)(() => {
217
219
  if (!inContextSDK) {
package/cjs/sjs.js ADDED
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getOperationSerializer = exports.useSJS = exports.getOperationsAndFragments = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const graphql_1 = require("graphql");
6
+ const execute_1 = require("graphql/execution/execute");
7
+ const slow_json_stringify_1 = tslib_1.__importDefault(require("slow-json-stringify"));
8
+ const utils_1 = require("@graphql-tools/utils");
9
+ exports.getOperationsAndFragments = (0, utils_1.memoize1)(function getOperationsAndFragments(document) {
10
+ const operations = {};
11
+ const fragments = {};
12
+ let anonymousCnt = 0;
13
+ for (const definition of document.definitions) {
14
+ if (definition.kind === graphql_1.Kind.OPERATION_DEFINITION) {
15
+ const operationName = definition.name?.value || `Anonymous_${anonymousCnt++}`;
16
+ operations[operationName] = definition;
17
+ }
18
+ if (definition.kind === graphql_1.Kind.FRAGMENT_DEFINITION) {
19
+ const fragmentName = definition.name.value;
20
+ fragments[fragmentName] = definition;
21
+ }
22
+ }
23
+ return {
24
+ operations,
25
+ fragments,
26
+ };
27
+ });
28
+ function useSJS() {
29
+ return {
30
+ onExecute() {
31
+ return {
32
+ onExecuteDone({ args, result }) {
33
+ const { operations, fragments } = (0, exports.getOperationsAndFragments)(args.document);
34
+ const operation = args.operationName
35
+ ? operations[args.operationName]
36
+ : operations[Object.keys(operations)[0]];
37
+ result.stringify = (0, exports.getOperationSerializer)(args.schema, operation, fragments, args.variableValues);
38
+ },
39
+ };
40
+ },
41
+ };
42
+ }
43
+ exports.useSJS = useSJS;
44
+ exports.getOperationSerializer = (0, utils_1.memoize2of4)(function getOperationSerializer(schema, operation, fragments, variableValues) {
45
+ const rootType = (0, utils_1.getDefinedRootType)(schema, operation.operation);
46
+ const rootFieldProperties = Object.create(null);
47
+ const { fields } = (0, utils_1.collectFields)(schema, fragments, variableValues, rootType, operation.selectionSet);
48
+ for (const [responseName, rootFieldNodes] of fields) {
49
+ for (const rootFieldNode of rootFieldNodes) {
50
+ const rootField = (0, execute_1.getFieldDef)(schema, rootType, rootFieldNode);
51
+ const rootFieldType = rootField.type;
52
+ rootFieldProperties[responseName] = getFieldPropertySchema(schema, fragments, variableValues, rootFieldType, rootFieldNodes);
53
+ }
54
+ }
55
+ return slow_json_stringify_1.default.sjs({
56
+ data: rootFieldProperties,
57
+ errors: slow_json_stringify_1.default.attr('array', slow_json_stringify_1.default.sjs({
58
+ message: slow_json_stringify_1.default.attr('string'),
59
+ path: slow_json_stringify_1.default.attr('array'),
60
+ locations: slow_json_stringify_1.default.attr('array', slow_json_stringify_1.default.sjs({
61
+ line: slow_json_stringify_1.default.attr('number'),
62
+ column: slow_json_stringify_1.default.attr('number'),
63
+ })),
64
+ })),
65
+ });
66
+ });
67
+ function getFieldPropertySchema(schema, fragments, variableValues, fieldType, fieldNodes) {
68
+ if ((0, graphql_1.isNonNullType)(fieldType)) {
69
+ return getFieldPropertySchema(schema, fragments, variableValues, fieldType.ofType, fieldNodes);
70
+ }
71
+ if ((0, graphql_1.isListType)(fieldType)) {
72
+ return slow_json_stringify_1.default.attr('array', slow_json_stringify_1.default.sjs(getFieldPropertySchema(schema, fragments, variableValues, fieldType.ofType, fieldNodes)));
73
+ }
74
+ if ((0, graphql_1.isAbstractType)(fieldType)) {
75
+ const fieldProperties = Object.create(null);
76
+ for (const possibleFieldType of schema.getPossibleTypes(fieldType)) {
77
+ const { fields: subFields } = (0, utils_1.collectSubFields)(schema, fragments, variableValues, possibleFieldType, fieldNodes);
78
+ for (const [subResponseName, subSubFields] of subFields) {
79
+ for (const subSubField of subSubFields) {
80
+ const subSubFieldDef = (0, execute_1.getFieldDef)(schema, possibleFieldType, subSubField);
81
+ const subFieldType = subSubFieldDef.type;
82
+ fieldProperties[subResponseName] = getFieldPropertySchema(schema, fragments, variableValues, subFieldType, subSubFields);
83
+ }
84
+ }
85
+ }
86
+ return fieldProperties;
87
+ }
88
+ if ((0, graphql_1.isObjectType)(fieldType)) {
89
+ const fieldProperties = Object.create(null);
90
+ const { fields: subFields } = (0, utils_1.collectSubFields)(schema, fragments, variableValues, fieldType, fieldNodes);
91
+ for (const [subResponseName, subSubFields] of subFields) {
92
+ const subFieldType = (0, execute_1.getFieldDef)(schema, fieldType, subSubFields[0]).type;
93
+ fieldProperties[subResponseName] = getFieldPropertySchema(schema, fragments, variableValues, subFieldType, subSubFields);
94
+ }
95
+ return fieldProperties;
96
+ }
97
+ if ((0, graphql_1.isEnumType)(fieldType)) {
98
+ return slow_json_stringify_1.default.attr('string');
99
+ }
100
+ if ((0, graphql_1.isScalarType)(fieldType)) {
101
+ switch (fieldType.name) {
102
+ case 'Int':
103
+ case 'Float':
104
+ return slow_json_stringify_1.default.attr('number');
105
+ case 'Boolean':
106
+ return slow_json_stringify_1.default.attr('boolean');
107
+ default:
108
+ return slow_json_stringify_1.default.attr('string');
109
+ }
110
+ }
111
+ }
@@ -7,6 +7,7 @@ const core_1 = require("@envelop/core");
7
7
  const utils_1 = require("@graphql-mesh/utils");
8
8
  const delegate_1 = require("@graphql-tools/delegate");
9
9
  const utils_2 = require("@graphql-tools/utils");
10
+ const sjs_1 = require("./sjs");
10
11
  var IntrospectionQueryType;
11
12
  (function (IntrospectionQueryType) {
12
13
  IntrospectionQueryType["FEDERATION"] = "FEDERATION";
@@ -46,8 +47,10 @@ function getExecuteFn(subschema) {
46
47
  rootValue: args.rootValue,
47
48
  context: args.contextValue,
48
49
  };
49
- const operationAST = (0, utils_2.getOperationASTFromRequest)(originalRequest);
50
- // TODO: We need more elegant solution
50
+ const { operations, fragments } = (0, sjs_1.getOperationsAndFragments)(args.document);
51
+ const operationAST = args.operationName
52
+ ? operations[args.operationName]
53
+ : operations[Object.keys(operations)[0]];
51
54
  const introspectionQueryType = getIntrospectionOperationType(operationAST);
52
55
  if (introspectionQueryType === IntrospectionQueryType.FEDERATION) {
53
56
  const executionResult = {
@@ -99,21 +102,21 @@ function getExecuteFn(subschema) {
99
102
  const result$ = compiledQuery.subscribe(request.rootValue, request.context, request.variables);
100
103
  if ((0, utils_2.isPromise)(result$)) {
101
104
  return result$.then(result => {
102
- result.stringify = compiledQuery.stringify;
105
+ result.stringify = (0, sjs_1.getOperationSerializer)(subschema.schema, operationAST, fragments, request.variables);
103
106
  return result;
104
107
  });
105
108
  }
106
- result$.stringify = compiledQuery.stringify;
109
+ result$.stringify = (0, sjs_1.getOperationSerializer)(subschema.schema, operationAST, fragments, request.variables);
107
110
  return result$;
108
111
  }
109
112
  const result$ = compiledQuery.query(request.rootValue, request.context, request.variables);
110
113
  if ((0, utils_2.isPromise)(result$)) {
111
114
  return result$.then(result => {
112
- result.stringify = compiledQuery.stringify;
115
+ result.stringify = (0, sjs_1.getOperationSerializer)(subschema.schema, operationAST, fragments, request.variables);
113
116
  return result;
114
117
  });
115
118
  }
116
- result$.stringify = compiledQuery.stringify;
119
+ result$.stringify = (0, sjs_1.getOperationSerializer)(subschema.schema, operationAST, fragments, request.variables);
117
120
  return result$;
118
121
  };
119
122
  }
package/esm/get-mesh.js CHANGED
@@ -9,6 +9,7 @@ import { getRootTypeMap, inspect, isAsyncIterable, isPromise, mapAsyncIterator,
9
9
  import { fetch as defaultFetchFn } from '@whatwg-node/fetch';
10
10
  import { MESH_CONTEXT_SYMBOL } from './constants.js';
11
11
  import { getInContextSDK } from './in-context-sdk.js';
12
+ import { useSJS } from './sjs.js';
12
13
  import { useSubschema } from './useSubschema.js';
13
14
  import { isStreamOperation, iterateAsync } from './utils.js';
14
15
  const memoizedGetEnvelopedFactory = memoize1(function getEnvelopedFactory(plugins) {
@@ -208,6 +209,7 @@ export async function getMesh(options) {
208
209
  return !isStreamOperation(args.document);
209
210
  },
210
211
  }),
212
+ useSJS(),
211
213
  ]),
212
214
  useExtendContext(() => {
213
215
  if (!inContextSDK) {
package/esm/sjs.js ADDED
@@ -0,0 +1,106 @@
1
+ import { isAbstractType, isEnumType, isListType, isNonNullType, isObjectType, isScalarType, Kind, } from 'graphql';
2
+ import { getFieldDef } from 'graphql/execution/execute';
3
+ import SJS from 'slow-json-stringify';
4
+ import { collectFields, collectSubFields, getDefinedRootType, memoize1, memoize2of4, } from '@graphql-tools/utils';
5
+ export const getOperationsAndFragments = memoize1(function getOperationsAndFragments(document) {
6
+ const operations = {};
7
+ const fragments = {};
8
+ let anonymousCnt = 0;
9
+ for (const definition of document.definitions) {
10
+ if (definition.kind === Kind.OPERATION_DEFINITION) {
11
+ const operationName = definition.name?.value || `Anonymous_${anonymousCnt++}`;
12
+ operations[operationName] = definition;
13
+ }
14
+ if (definition.kind === Kind.FRAGMENT_DEFINITION) {
15
+ const fragmentName = definition.name.value;
16
+ fragments[fragmentName] = definition;
17
+ }
18
+ }
19
+ return {
20
+ operations,
21
+ fragments,
22
+ };
23
+ });
24
+ export function useSJS() {
25
+ return {
26
+ onExecute() {
27
+ return {
28
+ onExecuteDone({ args, result }) {
29
+ const { operations, fragments } = getOperationsAndFragments(args.document);
30
+ const operation = args.operationName
31
+ ? operations[args.operationName]
32
+ : operations[Object.keys(operations)[0]];
33
+ result.stringify = getOperationSerializer(args.schema, operation, fragments, args.variableValues);
34
+ },
35
+ };
36
+ },
37
+ };
38
+ }
39
+ export const getOperationSerializer = memoize2of4(function getOperationSerializer(schema, operation, fragments, variableValues) {
40
+ const rootType = getDefinedRootType(schema, operation.operation);
41
+ const rootFieldProperties = Object.create(null);
42
+ const { fields } = collectFields(schema, fragments, variableValues, rootType, operation.selectionSet);
43
+ for (const [responseName, rootFieldNodes] of fields) {
44
+ for (const rootFieldNode of rootFieldNodes) {
45
+ const rootField = getFieldDef(schema, rootType, rootFieldNode);
46
+ const rootFieldType = rootField.type;
47
+ rootFieldProperties[responseName] = getFieldPropertySchema(schema, fragments, variableValues, rootFieldType, rootFieldNodes);
48
+ }
49
+ }
50
+ return SJS.sjs({
51
+ data: rootFieldProperties,
52
+ errors: SJS.attr('array', SJS.sjs({
53
+ message: SJS.attr('string'),
54
+ path: SJS.attr('array'),
55
+ locations: SJS.attr('array', SJS.sjs({
56
+ line: SJS.attr('number'),
57
+ column: SJS.attr('number'),
58
+ })),
59
+ })),
60
+ });
61
+ });
62
+ function getFieldPropertySchema(schema, fragments, variableValues, fieldType, fieldNodes) {
63
+ if (isNonNullType(fieldType)) {
64
+ return getFieldPropertySchema(schema, fragments, variableValues, fieldType.ofType, fieldNodes);
65
+ }
66
+ if (isListType(fieldType)) {
67
+ return SJS.attr('array', SJS.sjs(getFieldPropertySchema(schema, fragments, variableValues, fieldType.ofType, fieldNodes)));
68
+ }
69
+ if (isAbstractType(fieldType)) {
70
+ const fieldProperties = Object.create(null);
71
+ for (const possibleFieldType of schema.getPossibleTypes(fieldType)) {
72
+ const { fields: subFields } = collectSubFields(schema, fragments, variableValues, possibleFieldType, fieldNodes);
73
+ for (const [subResponseName, subSubFields] of subFields) {
74
+ for (const subSubField of subSubFields) {
75
+ const subSubFieldDef = getFieldDef(schema, possibleFieldType, subSubField);
76
+ const subFieldType = subSubFieldDef.type;
77
+ fieldProperties[subResponseName] = getFieldPropertySchema(schema, fragments, variableValues, subFieldType, subSubFields);
78
+ }
79
+ }
80
+ }
81
+ return fieldProperties;
82
+ }
83
+ if (isObjectType(fieldType)) {
84
+ const fieldProperties = Object.create(null);
85
+ const { fields: subFields } = collectSubFields(schema, fragments, variableValues, fieldType, fieldNodes);
86
+ for (const [subResponseName, subSubFields] of subFields) {
87
+ const subFieldType = getFieldDef(schema, fieldType, subSubFields[0]).type;
88
+ fieldProperties[subResponseName] = getFieldPropertySchema(schema, fragments, variableValues, subFieldType, subSubFields);
89
+ }
90
+ return fieldProperties;
91
+ }
92
+ if (isEnumType(fieldType)) {
93
+ return SJS.attr('string');
94
+ }
95
+ if (isScalarType(fieldType)) {
96
+ switch (fieldType.name) {
97
+ case 'Int':
98
+ case 'Float':
99
+ return SJS.attr('number');
100
+ case 'Boolean':
101
+ return SJS.attr('boolean');
102
+ default:
103
+ return SJS.attr('string');
104
+ }
105
+ }
106
+ }
@@ -3,7 +3,8 @@ import { compileQuery, isCompiledQuery } from 'graphql-jit';
3
3
  import { mapAsyncIterator } from '@envelop/core';
4
4
  import { applyRequestTransforms, applyResultTransforms } from '@graphql-mesh/utils';
5
5
  import { applySchemaTransforms, createDefaultExecutor, } from '@graphql-tools/delegate';
6
- import { getDefinedRootType, getOperationASTFromRequest, isAsyncIterable, isPromise, memoize1, printSchemaWithDirectives, } from '@graphql-tools/utils';
6
+ import { getDefinedRootType, isAsyncIterable, isPromise, memoize1, printSchemaWithDirectives, } from '@graphql-tools/utils';
7
+ import { getOperationsAndFragments, getOperationSerializer } from './sjs';
7
8
  var IntrospectionQueryType;
8
9
  (function (IntrospectionQueryType) {
9
10
  IntrospectionQueryType["FEDERATION"] = "FEDERATION";
@@ -43,8 +44,10 @@ function getExecuteFn(subschema) {
43
44
  rootValue: args.rootValue,
44
45
  context: args.contextValue,
45
46
  };
46
- const operationAST = getOperationASTFromRequest(originalRequest);
47
- // TODO: We need more elegant solution
47
+ const { operations, fragments } = getOperationsAndFragments(args.document);
48
+ const operationAST = args.operationName
49
+ ? operations[args.operationName]
50
+ : operations[Object.keys(operations)[0]];
48
51
  const introspectionQueryType = getIntrospectionOperationType(operationAST);
49
52
  if (introspectionQueryType === IntrospectionQueryType.FEDERATION) {
50
53
  const executionResult = {
@@ -96,21 +99,21 @@ function getExecuteFn(subschema) {
96
99
  const result$ = compiledQuery.subscribe(request.rootValue, request.context, request.variables);
97
100
  if (isPromise(result$)) {
98
101
  return result$.then(result => {
99
- result.stringify = compiledQuery.stringify;
102
+ result.stringify = getOperationSerializer(subschema.schema, operationAST, fragments, request.variables);
100
103
  return result;
101
104
  });
102
105
  }
103
- result$.stringify = compiledQuery.stringify;
106
+ result$.stringify = getOperationSerializer(subschema.schema, operationAST, fragments, request.variables);
104
107
  return result$;
105
108
  }
106
109
  const result$ = compiledQuery.query(request.rootValue, request.context, request.variables);
107
110
  if (isPromise(result$)) {
108
111
  return result$.then(result => {
109
- result.stringify = compiledQuery.stringify;
112
+ result.stringify = getOperationSerializer(subschema.schema, operationAST, fragments, request.variables);
110
113
  return result;
111
114
  });
112
115
  }
113
- result$.stringify = compiledQuery.stringify;
116
+ result$.stringify = getOperationSerializer(subschema.schema, operationAST, fragments, request.variables);
114
117
  return result$;
115
118
  };
116
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-mesh/runtime",
3
- "version": "0.96.8",
3
+ "version": "0.96.9-alpha-20231001010331-3dd758d10",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "@graphql-mesh/cross-helpers": "^0.4.1",
@@ -19,7 +19,8 @@
19
19
  "@graphql-tools/delegate": "^10.0.0",
20
20
  "@graphql-tools/wrap": "^10.0.0",
21
21
  "@whatwg-node/fetch": "^0.9.0",
22
- "graphql-jit": "0.8.2"
22
+ "graphql-jit": "0.8.2",
23
+ "slow-json-stringify": "2.0.1"
23
24
  },
24
25
  "repository": {
25
26
  "type": "git",
@@ -0,0 +1,9 @@
1
+ import { DocumentNode, FragmentDefinitionNode, GraphQLSchema, OperationDefinitionNode } from 'graphql';
2
+ import SJS from 'slow-json-stringify';
3
+ import { MeshPlugin } from '@graphql-mesh/types';
4
+ export declare const getOperationsAndFragments: (document: DocumentNode) => {
5
+ operations: Record<string, OperationDefinitionNode>;
6
+ fragments: Record<string, FragmentDefinitionNode>;
7
+ };
8
+ export declare function useSJS(): MeshPlugin<any>;
9
+ export declare const getOperationSerializer: (schema: GraphQLSchema, operation: OperationDefinitionNode, fragments: Record<string, FragmentDefinitionNode>, variableValues: any) => SJS.SjsSerializer;
@@ -0,0 +1,9 @@
1
+ import { DocumentNode, FragmentDefinitionNode, GraphQLSchema, OperationDefinitionNode } from 'graphql';
2
+ import SJS from 'slow-json-stringify';
3
+ import { MeshPlugin } from '@graphql-mesh/types';
4
+ export declare const getOperationsAndFragments: (document: DocumentNode) => {
5
+ operations: Record<string, OperationDefinitionNode>;
6
+ fragments: Record<string, FragmentDefinitionNode>;
7
+ };
8
+ export declare function useSJS(): MeshPlugin<any>;
9
+ export declare const getOperationSerializer: (schema: GraphQLSchema, operation: OperationDefinitionNode, fragments: Record<string, FragmentDefinitionNode>, variableValues: any) => SJS.SjsSerializer;