@luvio/graphql 0.140.1 → 0.140.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.
@@ -1,5 +1,15 @@
1
1
  'use strict';
2
2
 
3
+ function createFragmentMap(documentNode) {
4
+ const fragments = {};
5
+ documentNode.definitions.forEach((node) => {
6
+ if (node.kind === 'FragmentDefinition') {
7
+ fragments[node.name.value] = node;
8
+ }
9
+ });
10
+ return fragments;
11
+ }
12
+
3
13
  function serializeFieldArguments(argumentNodes, variables) {
4
14
  const mutableArgumentNodes = Object.assign([], argumentNodes);
5
15
  return `args__(${mutableArgumentNodes
@@ -57,4 +67,57 @@ function serializeValueNode(valueNode, variables) {
57
67
  }
58
68
  }
59
69
 
70
+ function serializeOperationNode(operationNode, variables, fragmentMap) {
71
+ return `${serializeSelectionSet(operationNode.selectionSet, variables, fragmentMap)}`;
72
+ }
73
+ function serializeSelectionSet(selectionSetNode, variables, fragmentMap) {
74
+ return `${selectionSetNode.selections
75
+ .map((selection) => serializeSelectionNode(selection, variables, fragmentMap))
76
+ .join()}`;
77
+ }
78
+ /**
79
+ *
80
+ * @description This function takes a GraphQL SelectionNode from an AST and serializes it in a stable way, so we can
81
+ * use it for property names and Store keys.
82
+ * @param selectionNode
83
+ * @param variables
84
+ * @param fragmentMap
85
+ * @returns string
86
+ */
87
+ function serializeSelectionNode(selectionNode, variables, fragmentMap) {
88
+ switch (selectionNode.kind) {
89
+ case 'Field': {
90
+ const hasArguments = selectionNode.arguments !== undefined && selectionNode.arguments.length > 0;
91
+ const argumentSuffix = hasArguments
92
+ ? `__${serializeFieldArguments(selectionNode.arguments, variables)}`
93
+ : '';
94
+ return `${selectionNode.name.value}${argumentSuffix}`;
95
+ }
96
+ case 'FragmentSpread': {
97
+ const fragment = fragmentMap[selectionNode.name.value];
98
+ return fragment === undefined
99
+ ? selectionNode.name.value
100
+ : serializeSelectionSet(fragment.selectionSet, variables, fragmentMap);
101
+ }
102
+ case 'InlineFragment':
103
+ return serializeSelectionSet(selectionNode.selectionSet, variables, fragmentMap);
104
+ }
105
+ }
106
+ function buildQueryTypeStringKey(args) {
107
+ const { keyPrefix, schemaName, queryTypeName, operationNode, variables, fragmentMap } = args;
108
+ return `${keyPrefix}::${schemaName}::${queryTypeName}[${serializeOperationNode(operationNode, variables, fragmentMap)}]`;
109
+ }
110
+ // TODO: W-13079691 - Need to take another pass at what we want to do for structured keys
111
+ function buildQueryTypeStructuredKey(args) {
112
+ const { luvio, keyPrefix, schemaName, queryTypeName, operationNode, variables, fragmentMap } = args;
113
+ return luvio.buildStructuredKey(keyPrefix, queryTypeName, {
114
+ schemaName,
115
+ operation: serializeOperationNode(operationNode, variables, fragmentMap),
116
+ });
117
+ }
118
+
119
+ exports.buildQueryTypeStringKey = buildQueryTypeStringKey;
120
+ exports.buildQueryTypeStructuredKey = buildQueryTypeStructuredKey;
121
+ exports.createFragmentMap = createFragmentMap;
60
122
  exports.serializeFieldArguments = serializeFieldArguments;
123
+ exports.serializeOperationNode = serializeOperationNode;
@@ -1,3 +1,13 @@
1
+ function createFragmentMap(documentNode) {
2
+ const fragments = {};
3
+ documentNode.definitions.forEach((node) => {
4
+ if (node.kind === 'FragmentDefinition') {
5
+ fragments[node.name.value] = node;
6
+ }
7
+ });
8
+ return fragments;
9
+ }
10
+
1
11
  function serializeFieldArguments(argumentNodes, variables) {
2
12
  const mutableArgumentNodes = Object.assign([], argumentNodes);
3
13
  return `args__(${mutableArgumentNodes
@@ -55,4 +65,53 @@ function serializeValueNode(valueNode, variables) {
55
65
  }
56
66
  }
57
67
 
58
- export { serializeFieldArguments };
68
+ function serializeOperationNode(operationNode, variables, fragmentMap) {
69
+ return `${serializeSelectionSet(operationNode.selectionSet, variables, fragmentMap)}`;
70
+ }
71
+ function serializeSelectionSet(selectionSetNode, variables, fragmentMap) {
72
+ return `${selectionSetNode.selections
73
+ .map((selection) => serializeSelectionNode(selection, variables, fragmentMap))
74
+ .join()}`;
75
+ }
76
+ /**
77
+ *
78
+ * @description This function takes a GraphQL SelectionNode from an AST and serializes it in a stable way, so we can
79
+ * use it for property names and Store keys.
80
+ * @param selectionNode
81
+ * @param variables
82
+ * @param fragmentMap
83
+ * @returns string
84
+ */
85
+ function serializeSelectionNode(selectionNode, variables, fragmentMap) {
86
+ switch (selectionNode.kind) {
87
+ case 'Field': {
88
+ const hasArguments = selectionNode.arguments !== undefined && selectionNode.arguments.length > 0;
89
+ const argumentSuffix = hasArguments
90
+ ? `__${serializeFieldArguments(selectionNode.arguments, variables)}`
91
+ : '';
92
+ return `${selectionNode.name.value}${argumentSuffix}`;
93
+ }
94
+ case 'FragmentSpread': {
95
+ const fragment = fragmentMap[selectionNode.name.value];
96
+ return fragment === undefined
97
+ ? selectionNode.name.value
98
+ : serializeSelectionSet(fragment.selectionSet, variables, fragmentMap);
99
+ }
100
+ case 'InlineFragment':
101
+ return serializeSelectionSet(selectionNode.selectionSet, variables, fragmentMap);
102
+ }
103
+ }
104
+ function buildQueryTypeStringKey(args) {
105
+ const { keyPrefix, schemaName, queryTypeName, operationNode, variables, fragmentMap } = args;
106
+ return `${keyPrefix}::${schemaName}::${queryTypeName}[${serializeOperationNode(operationNode, variables, fragmentMap)}]`;
107
+ }
108
+ // TODO: W-13079691 - Need to take another pass at what we want to do for structured keys
109
+ function buildQueryTypeStructuredKey(args) {
110
+ const { luvio, keyPrefix, schemaName, queryTypeName, operationNode, variables, fragmentMap } = args;
111
+ return luvio.buildStructuredKey(keyPrefix, queryTypeName, {
112
+ schemaName,
113
+ operation: serializeOperationNode(operationNode, variables, fragmentMap),
114
+ });
115
+ }
116
+
117
+ export { buildQueryTypeStringKey, buildQueryTypeStructuredKey, createFragmentMap, serializeFieldArguments, serializeOperationNode };
@@ -0,0 +1,3 @@
1
+ import type { DocumentNode } from '@luvio/graphql-parser';
2
+ import type { GraphQLFragmentMap } from './types';
3
+ export declare function createFragmentMap(documentNode: DocumentNode): GraphQLFragmentMap;
@@ -0,0 +1,16 @@
1
+ import type { Luvio, NormalizedKeyMetadata } from '@luvio/engine';
2
+ import type { OperationDefinitionNode } from '@luvio/graphql-parser';
3
+ import type { GraphQLVariables, GraphQLFragmentMap } from './main';
4
+ interface QueryTypeKeyBuilderArgs {
5
+ luvio: Luvio;
6
+ keyPrefix: string;
7
+ schemaName: string;
8
+ queryTypeName: string;
9
+ operationNode: OperationDefinitionNode;
10
+ variables: GraphQLVariables;
11
+ fragmentMap: GraphQLFragmentMap;
12
+ }
13
+ export declare function serializeOperationNode(operationNode: OperationDefinitionNode, variables: GraphQLVariables, fragmentMap: GraphQLFragmentMap): string;
14
+ export declare function buildQueryTypeStringKey(args: QueryTypeKeyBuilderArgs): string;
15
+ export declare function buildQueryTypeStructuredKey(args: QueryTypeKeyBuilderArgs): NormalizedKeyMetadata;
16
+ export {};
@@ -1,3 +1,4 @@
1
- import { serializeFieldArguments } from './fields';
2
- import { GraphQLVariables } from './types';
3
- export { serializeFieldArguments, GraphQLVariables };
1
+ export * from './types';
2
+ export * from './fragments';
3
+ export * from './fields';
4
+ export * from './keys';
@@ -1 +1,3 @@
1
+ import type { FragmentDefinitionNode } from '@luvio/graphql-parser';
1
2
  export type GraphQLVariables = Record<string, any>;
3
+ export type GraphQLFragmentMap = Record<string, FragmentDefinitionNode>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luvio/graphql",
3
- "version": "0.140.1",
3
+ "version": "0.140.3",
4
4
  "description": "GraphQL utilities for Luvio GraphQL adapter support",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,8 +27,8 @@
27
27
  "test:size": "luvioBundlesize"
28
28
  },
29
29
  "dependencies": {
30
- "@luvio/engine": "^0.139.6",
31
- "@luvio/graphql-parser": "^0.139.6"
30
+ "@luvio/engine": "^0.140.3",
31
+ "@luvio/graphql-parser": "^0.140.3"
32
32
  },
33
33
  "volta": {
34
34
  "extends": "../../../package.json"