@luvio/graphql-parser 0.102.2 → 0.104.1

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/dist/main.d.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import { GraphQLSchema, GraphQLScalarType, execute, buildSchema, getNamedType, isObjectType } from 'graphql';
2
2
  import { buildASTSchema } from 'graphql/utilities';
3
3
  import { LuvioDocumentNode, LuvioArgumentNode, LuvioDefinitionNode, LuvioObjectValueNode, LuvioOperationDefinitionNode, LuvioSelectionCustomFieldNode, LuvioSelectionNode, LuvioSelectionObjectFieldNode, LuvioSelectionScalarFieldNode, LuvioValueNode, LuvioFieldNode, LuvioVariableDefinitionNode, LuvioVariableNode, LuvioNamedTypeNode, LuvioListTypeNode, LuvioListValueNode, LuvioTypeNode } from './ast';
4
- export declare function parseAndVisit(source: string): LuvioDocumentNode;
5
4
  export type { GraphQLObjectType, GraphQLInterfaceType, GraphQLDirective, GraphQLUnionType, GraphQLNamedType, } from 'graphql/type';
6
- export type { ListTypeNode, BooleanValueNode, EnumTypeDefinitionNode, FieldDefinitionNode, FloatValueNode, InterfaceTypeDefinitionNode, IntValueNode, NamedTypeNode, ObjectTypeDefinitionNode, StringValueNode, TypeNode, UnionTypeDefinitionNode, DocumentNode, OperationDefinitionNode, FieldNode, ArgumentNode, ValueNode, SelectionNode, SelectionSetNode, FragmentDefinitionNode, DirectiveNode, ObjectFieldNode, ScalarTypeDefinitionNode, InputObjectTypeDefinitionNode, } from 'graphql/language';
5
+ export type { ListTypeNode, BooleanValueNode, EnumTypeDefinitionNode, FieldDefinitionNode, FloatValueNode, InterfaceTypeDefinitionNode, IntValueNode, NamedTypeNode, ObjectTypeDefinitionNode, StringValueNode, TypeNode, UnionTypeDefinitionNode, DocumentNode, OperationDefinitionNode, FieldNode, ArgumentNode, ValueNode, SelectionNode, SelectionSetNode, FragmentDefinitionNode, DirectiveNode, ObjectFieldNode, ScalarTypeDefinitionNode, InputObjectTypeDefinitionNode, InlineFragmentNode, } from 'graphql/language';
7
6
  export { ASTVisitor, parse, Kind, print, visit } from 'graphql/language';
8
7
  export { isScalarType } from 'graphql/type';
9
8
  export { stripIgnoredCharacters } from 'graphql/utilities';
10
- export { gql, enableAddMetaschemaDirective, disableAddMetaschemaDirective, astResolver, } from './gql';
9
+ export { gql, astResolver } from './gql';
11
10
  export type { AstResolver } from './gql';
12
11
  /**
13
12
  * @deprecated - Schema-backed adapters will use standard graphql types re-exported from @luvio/graphql
14
13
  */
15
14
  export { LuvioDocumentNode, LuvioArgumentNode, LuvioDefinitionNode, LuvioObjectValueNode, LuvioOperationDefinitionNode, LuvioSelectionCustomFieldNode, LuvioSelectionNode, LuvioSelectionObjectFieldNode, LuvioSelectionScalarFieldNode, LuvioValueNode, LuvioFieldNode, LuvioVariableDefinitionNode, LuvioVariableNode, LuvioNamedTypeNode, LuvioListTypeNode, LuvioListValueNode, LuvioTypeNode, GraphQLSchema, GraphQLScalarType, buildASTSchema, execute, buildSchema, getNamedType, isObjectType, };
15
+ /**
16
+ * @deprecated In favor of gql tagged template literal
17
+ */
18
+ export declare function parseAndVisit(source: string): LuvioDocumentNode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@luvio/graphql-parser",
3
- "version": "0.102.2",
3
+ "version": "0.104.1",
4
4
  "description": "GraphQL parser for Luvio GraphQL adapter support",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,13 +1,4 @@
1
- import {
2
- gql,
3
- docMap,
4
- astResolver,
5
- processSubstitutions,
6
- stripLocation,
7
- addMetaschemaDirectives,
8
- enableAddMetaschemaDirective,
9
- disableAddMetaschemaDirective,
10
- } from '../gql';
1
+ import { gql, docMap, astResolver, processSubstitutions, stripLocation } from '../gql';
11
2
  import ast from './ast.json';
12
3
  import astNoLoc from './astNoLoc.json';
13
4
 
@@ -672,13 +663,3 @@ describe('stripLocation', () => {
672
663
  expect(doc).toStrictEqual(astNoLoc);
673
664
  });
674
665
  });
675
-
676
- describe('addMetaschemaDirectives', () => {
677
- it('flips', () => {
678
- expect(addMetaschemaDirectives).toBe(false);
679
- enableAddMetaschemaDirective();
680
- expect(addMetaschemaDirectives).toBe(true);
681
- disableAddMetaschemaDirective();
682
- expect(addMetaschemaDirectives).toBe(false);
683
- });
684
- });
@@ -1,4 +1,5 @@
1
1
  import * as parser from '../main';
2
+ import { astResolver, stripLocation } from '../gql';
2
3
 
3
4
  describe('LDS GraphQL Parser', () => {
4
5
  describe('parseAndVisit', () => {
@@ -597,4 +598,54 @@ describe('LDS GraphQL Parser', () => {
597
598
  expect(target).toStrictEqual(expected);
598
599
  });
599
600
  });
601
+
602
+ describe('metaschema', () => {
603
+ it('should be able to use luvio document node as key to get metaschema AST', () => {
604
+ const source = /* GraphQL */ `
605
+ query {
606
+ uiapi {
607
+ query {
608
+ Account(where: { Name: { like: "Account1" } }) @connection {
609
+ edges {
610
+ node @resource(type: "Record") {
611
+ Name {
612
+ value
613
+ displayValue
614
+ }
615
+ }
616
+ }
617
+ }
618
+ }
619
+ }
620
+ }
621
+ `;
622
+
623
+ const metaschemaSource = /* GraphQL */ `
624
+ query {
625
+ uiapi {
626
+ query {
627
+ Account(where: { Name: { like: "Account1" } })
628
+ @category(name: "recordQuery") {
629
+ edges {
630
+ node {
631
+ Name {
632
+ value
633
+ displayValue
634
+ }
635
+ }
636
+ }
637
+ }
638
+ }
639
+ }
640
+ }
641
+ `;
642
+
643
+ const target = parser.parseAndVisit(source);
644
+ const metaschemaAST = astResolver(target);
645
+
646
+ expect(stripLocation(metaschemaAST)).toStrictEqual(
647
+ stripLocation(parser.parse(metaschemaSource))
648
+ );
649
+ });
650
+ });
600
651
  });
package/src/gql.ts CHANGED
@@ -7,7 +7,6 @@ import type { DefinitionNode, DocumentNode } from 'graphql/language';
7
7
  import { parse } from 'graphql/language';
8
8
  import { stripIgnoredCharacters } from 'graphql/utilities';
9
9
  import { ObjectCreate, ObjectKeys } from './util/language';
10
- import { metaschemaMapper } from './metaschema';
11
10
 
12
11
  export type AstResolver = (astReference: any) => DocumentNode | undefined;
13
12
 
@@ -22,8 +21,6 @@ export const docMap = new Map<string, DocumentNode>();
22
21
  */
23
22
  export const referenceMap = new WeakMap<Object, DocumentNode>();
24
23
 
25
- export let addMetaschemaDirectives: boolean = false;
26
-
27
24
  /**
28
25
  * Strips characters that are not significant to the validity or execution
29
26
  * of a GraphQL document:
@@ -43,7 +40,7 @@ function operationKeyBuilder(inputString: string): string {
43
40
  * @param inputString - operation string
44
41
  * @returns DocumentNode
45
42
  */
46
- function parseDocument(inputString: string): DocumentNode | null {
43
+ export function parseDocument(inputString: string): DocumentNode | null {
47
44
  const operationKey = operationKeyBuilder(inputString);
48
45
  const cachedDoc = docMap.get(operationKey);
49
46
  if (cachedDoc !== undefined) {
@@ -58,11 +55,6 @@ function parseDocument(inputString: string): DocumentNode | null {
58
55
  return null;
59
56
  }
60
57
 
61
- // in-place substitution for removal of legacy and adding metaschema directives
62
- if (addMetaschemaDirectives) {
63
- metaschemaMapper(parsedDoc);
64
- }
65
-
66
58
  const parsedDocNoLoc = stripLocation(parsedDoc);
67
59
 
68
60
  docMap.set(operationKey, parsedDocNoLoc);
@@ -85,9 +77,13 @@ function insertFragments(doc: DocumentNode, fragments: DefinitionNode[]): Docume
85
77
  return doc;
86
78
  }
87
79
 
88
- function createReference(document: DocumentNode): object {
80
+ export function updateReferenceMapWithKnownKey(doc: DocumentNode, key: object): void {
81
+ referenceMap.set(key, doc);
82
+ }
83
+
84
+ export function updateReferenceMapAndGetKey(doc: DocumentNode): object {
89
85
  const key = ObjectCreate(null);
90
- referenceMap.set(key, document);
86
+ updateReferenceMapWithKnownKey(doc, key);
91
87
 
92
88
  return key;
93
89
  }
@@ -219,19 +215,8 @@ export function gql(
219
215
  }
220
216
 
221
217
  if (inputSubstitutionFragments.length === 0) {
222
- return createReference(document);
218
+ return updateReferenceMapAndGetKey(document);
223
219
  }
224
220
 
225
- return createReference(insertFragments(document, inputSubstitutionFragments));
226
- }
227
-
228
- /**
229
- * Enable the parser to add corresponding metaschema directives for backwards compatibility
230
- */
231
- export function enableAddMetaschemaDirective() {
232
- addMetaschemaDirectives = true;
233
- }
234
-
235
- export function disableAddMetaschemaDirective() {
236
- addMetaschemaDirectives = false;
221
+ return updateReferenceMapAndGetKey(insertFragments(document, inputSubstitutionFragments));
237
222
  }
package/src/main.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { parse } from 'graphql/language';
2
1
  import {
3
2
  GraphQLSchema,
4
3
  GraphQLScalarType,
@@ -29,14 +28,8 @@ import {
29
28
  LuvioTypeNode,
30
29
  } from './ast';
31
30
  import { transform } from './document';
32
-
33
- /*
34
- Deprecated - Remove after existing usages are removed.
35
- */
36
- export function parseAndVisit(source: string): LuvioDocumentNode {
37
- const ast = parse(source);
38
- return transform(ast);
39
- }
31
+ import { updateReferenceMapWithKnownKey } from './gql';
32
+ import { metaschemaMapper } from './metaschema';
40
33
 
41
34
  export type {
42
35
  GraphQLObjectType,
@@ -70,16 +63,13 @@ export type {
70
63
  ObjectFieldNode,
71
64
  ScalarTypeDefinitionNode,
72
65
  InputObjectTypeDefinitionNode,
66
+ InlineFragmentNode,
73
67
  } from 'graphql/language';
68
+ import { parse } from 'graphql/language';
74
69
  export { ASTVisitor, parse, Kind, print, visit } from 'graphql/language';
75
70
  export { isScalarType } from 'graphql/type';
76
71
  export { stripIgnoredCharacters } from 'graphql/utilities';
77
- export {
78
- gql,
79
- enableAddMetaschemaDirective,
80
- disableAddMetaschemaDirective,
81
- astResolver,
82
- } from './gql';
72
+ export { gql, astResolver } from './gql';
83
73
  export type { AstResolver } from './gql';
84
74
 
85
75
  /**
@@ -111,3 +101,19 @@ export {
111
101
  getNamedType,
112
102
  isObjectType,
113
103
  };
104
+
105
+ /**
106
+ * @deprecated In favor of gql tagged template literal
107
+ */
108
+ export function parseAndVisit(source: string): LuvioDocumentNode {
109
+ const ast = parse(source);
110
+ const luvioDocumentNode = transform(ast!);
111
+
112
+ // In-place substitution of metaschema annotations
113
+ metaschemaMapper(ast!);
114
+
115
+ // Set the AST reference map with LuvioDocumentNode as the key
116
+ // ASTResolver will resolve it to metaschema mapped AST
117
+ updateReferenceMapWithKnownKey(ast!, luvioDocumentNode);
118
+ return luvioDocumentNode;
119
+ }