@luvio/graphql-parser 0.104.0 → 0.106.0
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/gql.d.ts +0 -6
- package/dist/luvioGraphqlParser.js +123 -123
- package/dist/luvioGraphqlParser.mjs +123 -123
- package/package.json +1 -1
- package/src/__tests__/gql.spec.ts +1 -20
- package/src/__tests__/main.spec.ts +4 -2
- package/src/gql.ts +0 -19
- package/src/main.ts +3 -2
package/dist/gql.d.ts
CHANGED
|
@@ -15,7 +15,6 @@ export declare const docMap: Map<string, DocumentNode>;
|
|
|
15
15
|
* As a user shouldn't have access to the Document
|
|
16
16
|
*/
|
|
17
17
|
export declare const referenceMap: WeakMap<Object, DocumentNode>;
|
|
18
|
-
export declare let addMetaschemaDirectives: boolean;
|
|
19
18
|
/**
|
|
20
19
|
* Returns document node if cached or else update the cache and return the document node
|
|
21
20
|
* @param inputString - operation string
|
|
@@ -50,8 +49,3 @@ export declare const astResolver: AstResolver;
|
|
|
50
49
|
* @returns an opaque reference to the parsed document
|
|
51
50
|
*/
|
|
52
51
|
export declare function gql(literals: ReadonlyArray<string> | string, ...subs: (string | object)[]): object | null;
|
|
53
|
-
/**
|
|
54
|
-
* Enable the parser to add corresponding metaschema directives for backwards compatibility
|
|
55
|
-
*/
|
|
56
|
-
export declare function enableAddMetaschemaDirective(): void;
|
|
57
|
-
export declare function disableAddMetaschemaDirective(): void;
|
|
@@ -12607,128 +12607,6 @@ function transform(root) {
|
|
|
12607
12607
|
|
|
12608
12608
|
const { create, keys } = Object;
|
|
12609
12609
|
|
|
12610
|
-
const DIRECTIVE_RECORD_CATEGORY = {
|
|
12611
|
-
kind: 'Directive',
|
|
12612
|
-
name: {
|
|
12613
|
-
kind: 'Name',
|
|
12614
|
-
value: 'category',
|
|
12615
|
-
},
|
|
12616
|
-
arguments: [
|
|
12617
|
-
{
|
|
12618
|
-
kind: 'Argument',
|
|
12619
|
-
name: {
|
|
12620
|
-
kind: 'Name',
|
|
12621
|
-
value: 'name',
|
|
12622
|
-
},
|
|
12623
|
-
value: {
|
|
12624
|
-
kind: 'StringValue',
|
|
12625
|
-
value: 'recordQuery',
|
|
12626
|
-
block: false,
|
|
12627
|
-
},
|
|
12628
|
-
},
|
|
12629
|
-
],
|
|
12630
|
-
};
|
|
12631
|
-
const DIRECTIVE_PARENT_CATEGORY = {
|
|
12632
|
-
kind: 'Directive',
|
|
12633
|
-
name: {
|
|
12634
|
-
kind: 'Name',
|
|
12635
|
-
value: 'category',
|
|
12636
|
-
},
|
|
12637
|
-
arguments: [
|
|
12638
|
-
{
|
|
12639
|
-
kind: 'Argument',
|
|
12640
|
-
name: {
|
|
12641
|
-
kind: 'Name',
|
|
12642
|
-
value: 'name',
|
|
12643
|
-
},
|
|
12644
|
-
value: {
|
|
12645
|
-
kind: 'StringValue',
|
|
12646
|
-
value: 'parentRelationship',
|
|
12647
|
-
block: false,
|
|
12648
|
-
},
|
|
12649
|
-
},
|
|
12650
|
-
],
|
|
12651
|
-
};
|
|
12652
|
-
function substituteDirectives(directives, index, nodeName) {
|
|
12653
|
-
if (directives[index].name.value === CUSTOM_DIRECTIVE_CONNECTION) {
|
|
12654
|
-
// replace the custom directive node with the metaschema directive node
|
|
12655
|
-
// @ts-ignore - Document is read only
|
|
12656
|
-
directives[index] = DIRECTIVE_RECORD_CATEGORY;
|
|
12657
|
-
}
|
|
12658
|
-
else if (directives[index].name.value === CUSTOM_DIRECTIVE_RESOURCE) {
|
|
12659
|
-
// node gets its type from @category recordQuery
|
|
12660
|
-
if (nodeName === 'node') {
|
|
12661
|
-
// @ts-ignore - Document is read only
|
|
12662
|
-
directives.splice(index, 1);
|
|
12663
|
-
}
|
|
12664
|
-
else {
|
|
12665
|
-
// @ts-ignore - Document is read only
|
|
12666
|
-
directives[index] = DIRECTIVE_PARENT_CATEGORY;
|
|
12667
|
-
}
|
|
12668
|
-
}
|
|
12669
|
-
}
|
|
12670
|
-
/**
|
|
12671
|
-
* Returns true if the directive node is of legacy type
|
|
12672
|
-
* @param node : Directive node
|
|
12673
|
-
* @returns
|
|
12674
|
-
*/
|
|
12675
|
-
function isCustomDirective(node) {
|
|
12676
|
-
return (node.name.value === CUSTOM_DIRECTIVE_CONNECTION ||
|
|
12677
|
-
node.name.value === CUSTOM_DIRECTIVE_RESOURCE);
|
|
12678
|
-
}
|
|
12679
|
-
/**
|
|
12680
|
-
* Traverses a selection set and it's nested selections,
|
|
12681
|
-
* to find any legacy custom directives and substitute them with metaschema directives
|
|
12682
|
-
* @param node - SelectionSetNode
|
|
12683
|
-
* @returns SelectionSetNode
|
|
12684
|
-
*/
|
|
12685
|
-
function traverseSelectionSet(node) {
|
|
12686
|
-
if (node === undefined) {
|
|
12687
|
-
return;
|
|
12688
|
-
}
|
|
12689
|
-
for (const selection of node.selections) {
|
|
12690
|
-
// FragmentSpreadNode doesn't have a selection set
|
|
12691
|
-
// which should be handled at this methods entry point
|
|
12692
|
-
const { directives, selectionSet } = selection;
|
|
12693
|
-
let selectionName;
|
|
12694
|
-
if (selection.kind !== 'InlineFragment') {
|
|
12695
|
-
selectionName = selection.name.value;
|
|
12696
|
-
}
|
|
12697
|
-
if (directives !== undefined && directives.length > 0) {
|
|
12698
|
-
// we follow this pattern instead of map to preserve the order of directives
|
|
12699
|
-
// order of directives may be significant as per graphql spec
|
|
12700
|
-
const index = directives.findIndex(isCustomDirective);
|
|
12701
|
-
if (index !== -1) {
|
|
12702
|
-
substituteDirectives(directives, index, selectionName);
|
|
12703
|
-
}
|
|
12704
|
-
}
|
|
12705
|
-
traverseSelectionSet(selectionSet);
|
|
12706
|
-
}
|
|
12707
|
-
return node;
|
|
12708
|
-
}
|
|
12709
|
-
/**
|
|
12710
|
-
* Accepts a document node and replaces the legacy custom directives with metaschema directives "in-place"
|
|
12711
|
-
* @param doc
|
|
12712
|
-
*/
|
|
12713
|
-
function metaschemaMapper(doc) {
|
|
12714
|
-
// this method is only callable for Executable definitions
|
|
12715
|
-
// such as Operations and Fragments
|
|
12716
|
-
// so we have to explicitly cast the definitions for ts
|
|
12717
|
-
const { definitions } = doc;
|
|
12718
|
-
for (const def of definitions) {
|
|
12719
|
-
const { directives, selectionSet } = def;
|
|
12720
|
-
if (directives !== undefined && directives.length > 0) {
|
|
12721
|
-
// we are making an assumption here that only one custom directive can be applied to a node
|
|
12722
|
-
// we can revisit if this condition changes
|
|
12723
|
-
const index = directives.findIndex(isCustomDirective);
|
|
12724
|
-
if (index !== -1) {
|
|
12725
|
-
substituteDirectives(directives, index, undefined);
|
|
12726
|
-
}
|
|
12727
|
-
}
|
|
12728
|
-
traverseSelectionSet(selectionSet);
|
|
12729
|
-
}
|
|
12730
|
-
}
|
|
12731
|
-
|
|
12732
12610
|
/**
|
|
12733
12611
|
* we should look into optimizing this before it turns into a memory hog
|
|
12734
12612
|
* weakmaps, or limiting the size of the cache, or something
|
|
@@ -12910,11 +12788,133 @@ function gql(literals, ...subs) {
|
|
|
12910
12788
|
return updateReferenceMapAndGetKey(insertFragments(document, inputSubstitutionFragments));
|
|
12911
12789
|
}
|
|
12912
12790
|
|
|
12791
|
+
const DIRECTIVE_RECORD_CATEGORY = {
|
|
12792
|
+
kind: 'Directive',
|
|
12793
|
+
name: {
|
|
12794
|
+
kind: 'Name',
|
|
12795
|
+
value: 'category',
|
|
12796
|
+
},
|
|
12797
|
+
arguments: [
|
|
12798
|
+
{
|
|
12799
|
+
kind: 'Argument',
|
|
12800
|
+
name: {
|
|
12801
|
+
kind: 'Name',
|
|
12802
|
+
value: 'name',
|
|
12803
|
+
},
|
|
12804
|
+
value: {
|
|
12805
|
+
kind: 'StringValue',
|
|
12806
|
+
value: 'recordQuery',
|
|
12807
|
+
block: false,
|
|
12808
|
+
},
|
|
12809
|
+
},
|
|
12810
|
+
],
|
|
12811
|
+
};
|
|
12812
|
+
const DIRECTIVE_PARENT_CATEGORY = {
|
|
12813
|
+
kind: 'Directive',
|
|
12814
|
+
name: {
|
|
12815
|
+
kind: 'Name',
|
|
12816
|
+
value: 'category',
|
|
12817
|
+
},
|
|
12818
|
+
arguments: [
|
|
12819
|
+
{
|
|
12820
|
+
kind: 'Argument',
|
|
12821
|
+
name: {
|
|
12822
|
+
kind: 'Name',
|
|
12823
|
+
value: 'name',
|
|
12824
|
+
},
|
|
12825
|
+
value: {
|
|
12826
|
+
kind: 'StringValue',
|
|
12827
|
+
value: 'parentRelationship',
|
|
12828
|
+
block: false,
|
|
12829
|
+
},
|
|
12830
|
+
},
|
|
12831
|
+
],
|
|
12832
|
+
};
|
|
12833
|
+
function substituteDirectives(directives, index, nodeName) {
|
|
12834
|
+
if (directives[index].name.value === CUSTOM_DIRECTIVE_CONNECTION) {
|
|
12835
|
+
// replace the custom directive node with the metaschema directive node
|
|
12836
|
+
// @ts-ignore - Document is read only
|
|
12837
|
+
directives[index] = DIRECTIVE_RECORD_CATEGORY;
|
|
12838
|
+
}
|
|
12839
|
+
else if (directives[index].name.value === CUSTOM_DIRECTIVE_RESOURCE) {
|
|
12840
|
+
// node gets its type from @category recordQuery
|
|
12841
|
+
if (nodeName === 'node') {
|
|
12842
|
+
// @ts-ignore - Document is read only
|
|
12843
|
+
directives.splice(index, 1);
|
|
12844
|
+
}
|
|
12845
|
+
else {
|
|
12846
|
+
// @ts-ignore - Document is read only
|
|
12847
|
+
directives[index] = DIRECTIVE_PARENT_CATEGORY;
|
|
12848
|
+
}
|
|
12849
|
+
}
|
|
12850
|
+
}
|
|
12851
|
+
/**
|
|
12852
|
+
* Returns true if the directive node is of legacy type
|
|
12853
|
+
* @param node : Directive node
|
|
12854
|
+
* @returns
|
|
12855
|
+
*/
|
|
12856
|
+
function isCustomDirective(node) {
|
|
12857
|
+
return (node.name.value === CUSTOM_DIRECTIVE_CONNECTION ||
|
|
12858
|
+
node.name.value === CUSTOM_DIRECTIVE_RESOURCE);
|
|
12859
|
+
}
|
|
12860
|
+
/**
|
|
12861
|
+
* Traverses a selection set and it's nested selections,
|
|
12862
|
+
* to find any legacy custom directives and substitute them with metaschema directives
|
|
12863
|
+
* @param node - SelectionSetNode
|
|
12864
|
+
* @returns SelectionSetNode
|
|
12865
|
+
*/
|
|
12866
|
+
function traverseSelectionSet(node) {
|
|
12867
|
+
if (node === undefined) {
|
|
12868
|
+
return;
|
|
12869
|
+
}
|
|
12870
|
+
for (const selection of node.selections) {
|
|
12871
|
+
// FragmentSpreadNode doesn't have a selection set
|
|
12872
|
+
// which should be handled at this methods entry point
|
|
12873
|
+
const { directives, selectionSet } = selection;
|
|
12874
|
+
let selectionName;
|
|
12875
|
+
if (selection.kind !== 'InlineFragment') {
|
|
12876
|
+
selectionName = selection.name.value;
|
|
12877
|
+
}
|
|
12878
|
+
if (directives !== undefined && directives.length > 0) {
|
|
12879
|
+
// we follow this pattern instead of map to preserve the order of directives
|
|
12880
|
+
// order of directives may be significant as per graphql spec
|
|
12881
|
+
const index = directives.findIndex(isCustomDirective);
|
|
12882
|
+
if (index !== -1) {
|
|
12883
|
+
substituteDirectives(directives, index, selectionName);
|
|
12884
|
+
}
|
|
12885
|
+
}
|
|
12886
|
+
traverseSelectionSet(selectionSet);
|
|
12887
|
+
}
|
|
12888
|
+
return node;
|
|
12889
|
+
}
|
|
12890
|
+
/**
|
|
12891
|
+
* Accepts a document node and replaces the legacy custom directives with metaschema directives "in-place"
|
|
12892
|
+
* @param doc
|
|
12893
|
+
*/
|
|
12894
|
+
function metaschemaMapper(doc) {
|
|
12895
|
+
// this method is only callable for Executable definitions
|
|
12896
|
+
// such as Operations and Fragments
|
|
12897
|
+
// so we have to explicitly cast the definitions for ts
|
|
12898
|
+
const { definitions } = doc;
|
|
12899
|
+
for (const def of definitions) {
|
|
12900
|
+
const { directives, selectionSet } = def;
|
|
12901
|
+
if (directives !== undefined && directives.length > 0) {
|
|
12902
|
+
// we are making an assumption here that only one custom directive can be applied to a node
|
|
12903
|
+
// we can revisit if this condition changes
|
|
12904
|
+
const index = directives.findIndex(isCustomDirective);
|
|
12905
|
+
if (index !== -1) {
|
|
12906
|
+
substituteDirectives(directives, index, undefined);
|
|
12907
|
+
}
|
|
12908
|
+
}
|
|
12909
|
+
traverseSelectionSet(selectionSet);
|
|
12910
|
+
}
|
|
12911
|
+
}
|
|
12912
|
+
|
|
12913
12913
|
/**
|
|
12914
12914
|
* @deprecated In favor of gql tagged template literal
|
|
12915
12915
|
*/
|
|
12916
12916
|
function parseAndVisit(source) {
|
|
12917
|
-
const ast =
|
|
12917
|
+
const ast = parse(source);
|
|
12918
12918
|
const luvioDocumentNode = transform(ast);
|
|
12919
12919
|
// In-place substitution of metaschema annotations
|
|
12920
12920
|
metaschemaMapper(ast);
|
|
@@ -12603,128 +12603,6 @@ function transform(root) {
|
|
|
12603
12603
|
|
|
12604
12604
|
const { create, keys } = Object;
|
|
12605
12605
|
|
|
12606
|
-
const DIRECTIVE_RECORD_CATEGORY = {
|
|
12607
|
-
kind: 'Directive',
|
|
12608
|
-
name: {
|
|
12609
|
-
kind: 'Name',
|
|
12610
|
-
value: 'category',
|
|
12611
|
-
},
|
|
12612
|
-
arguments: [
|
|
12613
|
-
{
|
|
12614
|
-
kind: 'Argument',
|
|
12615
|
-
name: {
|
|
12616
|
-
kind: 'Name',
|
|
12617
|
-
value: 'name',
|
|
12618
|
-
},
|
|
12619
|
-
value: {
|
|
12620
|
-
kind: 'StringValue',
|
|
12621
|
-
value: 'recordQuery',
|
|
12622
|
-
block: false,
|
|
12623
|
-
},
|
|
12624
|
-
},
|
|
12625
|
-
],
|
|
12626
|
-
};
|
|
12627
|
-
const DIRECTIVE_PARENT_CATEGORY = {
|
|
12628
|
-
kind: 'Directive',
|
|
12629
|
-
name: {
|
|
12630
|
-
kind: 'Name',
|
|
12631
|
-
value: 'category',
|
|
12632
|
-
},
|
|
12633
|
-
arguments: [
|
|
12634
|
-
{
|
|
12635
|
-
kind: 'Argument',
|
|
12636
|
-
name: {
|
|
12637
|
-
kind: 'Name',
|
|
12638
|
-
value: 'name',
|
|
12639
|
-
},
|
|
12640
|
-
value: {
|
|
12641
|
-
kind: 'StringValue',
|
|
12642
|
-
value: 'parentRelationship',
|
|
12643
|
-
block: false,
|
|
12644
|
-
},
|
|
12645
|
-
},
|
|
12646
|
-
],
|
|
12647
|
-
};
|
|
12648
|
-
function substituteDirectives(directives, index, nodeName) {
|
|
12649
|
-
if (directives[index].name.value === CUSTOM_DIRECTIVE_CONNECTION) {
|
|
12650
|
-
// replace the custom directive node with the metaschema directive node
|
|
12651
|
-
// @ts-ignore - Document is read only
|
|
12652
|
-
directives[index] = DIRECTIVE_RECORD_CATEGORY;
|
|
12653
|
-
}
|
|
12654
|
-
else if (directives[index].name.value === CUSTOM_DIRECTIVE_RESOURCE) {
|
|
12655
|
-
// node gets its type from @category recordQuery
|
|
12656
|
-
if (nodeName === 'node') {
|
|
12657
|
-
// @ts-ignore - Document is read only
|
|
12658
|
-
directives.splice(index, 1);
|
|
12659
|
-
}
|
|
12660
|
-
else {
|
|
12661
|
-
// @ts-ignore - Document is read only
|
|
12662
|
-
directives[index] = DIRECTIVE_PARENT_CATEGORY;
|
|
12663
|
-
}
|
|
12664
|
-
}
|
|
12665
|
-
}
|
|
12666
|
-
/**
|
|
12667
|
-
* Returns true if the directive node is of legacy type
|
|
12668
|
-
* @param node : Directive node
|
|
12669
|
-
* @returns
|
|
12670
|
-
*/
|
|
12671
|
-
function isCustomDirective(node) {
|
|
12672
|
-
return (node.name.value === CUSTOM_DIRECTIVE_CONNECTION ||
|
|
12673
|
-
node.name.value === CUSTOM_DIRECTIVE_RESOURCE);
|
|
12674
|
-
}
|
|
12675
|
-
/**
|
|
12676
|
-
* Traverses a selection set and it's nested selections,
|
|
12677
|
-
* to find any legacy custom directives and substitute them with metaschema directives
|
|
12678
|
-
* @param node - SelectionSetNode
|
|
12679
|
-
* @returns SelectionSetNode
|
|
12680
|
-
*/
|
|
12681
|
-
function traverseSelectionSet(node) {
|
|
12682
|
-
if (node === undefined) {
|
|
12683
|
-
return;
|
|
12684
|
-
}
|
|
12685
|
-
for (const selection of node.selections) {
|
|
12686
|
-
// FragmentSpreadNode doesn't have a selection set
|
|
12687
|
-
// which should be handled at this methods entry point
|
|
12688
|
-
const { directives, selectionSet } = selection;
|
|
12689
|
-
let selectionName;
|
|
12690
|
-
if (selection.kind !== 'InlineFragment') {
|
|
12691
|
-
selectionName = selection.name.value;
|
|
12692
|
-
}
|
|
12693
|
-
if (directives !== undefined && directives.length > 0) {
|
|
12694
|
-
// we follow this pattern instead of map to preserve the order of directives
|
|
12695
|
-
// order of directives may be significant as per graphql spec
|
|
12696
|
-
const index = directives.findIndex(isCustomDirective);
|
|
12697
|
-
if (index !== -1) {
|
|
12698
|
-
substituteDirectives(directives, index, selectionName);
|
|
12699
|
-
}
|
|
12700
|
-
}
|
|
12701
|
-
traverseSelectionSet(selectionSet);
|
|
12702
|
-
}
|
|
12703
|
-
return node;
|
|
12704
|
-
}
|
|
12705
|
-
/**
|
|
12706
|
-
* Accepts a document node and replaces the legacy custom directives with metaschema directives "in-place"
|
|
12707
|
-
* @param doc
|
|
12708
|
-
*/
|
|
12709
|
-
function metaschemaMapper(doc) {
|
|
12710
|
-
// this method is only callable for Executable definitions
|
|
12711
|
-
// such as Operations and Fragments
|
|
12712
|
-
// so we have to explicitly cast the definitions for ts
|
|
12713
|
-
const { definitions } = doc;
|
|
12714
|
-
for (const def of definitions) {
|
|
12715
|
-
const { directives, selectionSet } = def;
|
|
12716
|
-
if (directives !== undefined && directives.length > 0) {
|
|
12717
|
-
// we are making an assumption here that only one custom directive can be applied to a node
|
|
12718
|
-
// we can revisit if this condition changes
|
|
12719
|
-
const index = directives.findIndex(isCustomDirective);
|
|
12720
|
-
if (index !== -1) {
|
|
12721
|
-
substituteDirectives(directives, index, undefined);
|
|
12722
|
-
}
|
|
12723
|
-
}
|
|
12724
|
-
traverseSelectionSet(selectionSet);
|
|
12725
|
-
}
|
|
12726
|
-
}
|
|
12727
|
-
|
|
12728
12606
|
/**
|
|
12729
12607
|
* we should look into optimizing this before it turns into a memory hog
|
|
12730
12608
|
* weakmaps, or limiting the size of the cache, or something
|
|
@@ -12906,11 +12784,133 @@ function gql(literals, ...subs) {
|
|
|
12906
12784
|
return updateReferenceMapAndGetKey(insertFragments(document, inputSubstitutionFragments));
|
|
12907
12785
|
}
|
|
12908
12786
|
|
|
12787
|
+
const DIRECTIVE_RECORD_CATEGORY = {
|
|
12788
|
+
kind: 'Directive',
|
|
12789
|
+
name: {
|
|
12790
|
+
kind: 'Name',
|
|
12791
|
+
value: 'category',
|
|
12792
|
+
},
|
|
12793
|
+
arguments: [
|
|
12794
|
+
{
|
|
12795
|
+
kind: 'Argument',
|
|
12796
|
+
name: {
|
|
12797
|
+
kind: 'Name',
|
|
12798
|
+
value: 'name',
|
|
12799
|
+
},
|
|
12800
|
+
value: {
|
|
12801
|
+
kind: 'StringValue',
|
|
12802
|
+
value: 'recordQuery',
|
|
12803
|
+
block: false,
|
|
12804
|
+
},
|
|
12805
|
+
},
|
|
12806
|
+
],
|
|
12807
|
+
};
|
|
12808
|
+
const DIRECTIVE_PARENT_CATEGORY = {
|
|
12809
|
+
kind: 'Directive',
|
|
12810
|
+
name: {
|
|
12811
|
+
kind: 'Name',
|
|
12812
|
+
value: 'category',
|
|
12813
|
+
},
|
|
12814
|
+
arguments: [
|
|
12815
|
+
{
|
|
12816
|
+
kind: 'Argument',
|
|
12817
|
+
name: {
|
|
12818
|
+
kind: 'Name',
|
|
12819
|
+
value: 'name',
|
|
12820
|
+
},
|
|
12821
|
+
value: {
|
|
12822
|
+
kind: 'StringValue',
|
|
12823
|
+
value: 'parentRelationship',
|
|
12824
|
+
block: false,
|
|
12825
|
+
},
|
|
12826
|
+
},
|
|
12827
|
+
],
|
|
12828
|
+
};
|
|
12829
|
+
function substituteDirectives(directives, index, nodeName) {
|
|
12830
|
+
if (directives[index].name.value === CUSTOM_DIRECTIVE_CONNECTION) {
|
|
12831
|
+
// replace the custom directive node with the metaschema directive node
|
|
12832
|
+
// @ts-ignore - Document is read only
|
|
12833
|
+
directives[index] = DIRECTIVE_RECORD_CATEGORY;
|
|
12834
|
+
}
|
|
12835
|
+
else if (directives[index].name.value === CUSTOM_DIRECTIVE_RESOURCE) {
|
|
12836
|
+
// node gets its type from @category recordQuery
|
|
12837
|
+
if (nodeName === 'node') {
|
|
12838
|
+
// @ts-ignore - Document is read only
|
|
12839
|
+
directives.splice(index, 1);
|
|
12840
|
+
}
|
|
12841
|
+
else {
|
|
12842
|
+
// @ts-ignore - Document is read only
|
|
12843
|
+
directives[index] = DIRECTIVE_PARENT_CATEGORY;
|
|
12844
|
+
}
|
|
12845
|
+
}
|
|
12846
|
+
}
|
|
12847
|
+
/**
|
|
12848
|
+
* Returns true if the directive node is of legacy type
|
|
12849
|
+
* @param node : Directive node
|
|
12850
|
+
* @returns
|
|
12851
|
+
*/
|
|
12852
|
+
function isCustomDirective(node) {
|
|
12853
|
+
return (node.name.value === CUSTOM_DIRECTIVE_CONNECTION ||
|
|
12854
|
+
node.name.value === CUSTOM_DIRECTIVE_RESOURCE);
|
|
12855
|
+
}
|
|
12856
|
+
/**
|
|
12857
|
+
* Traverses a selection set and it's nested selections,
|
|
12858
|
+
* to find any legacy custom directives and substitute them with metaschema directives
|
|
12859
|
+
* @param node - SelectionSetNode
|
|
12860
|
+
* @returns SelectionSetNode
|
|
12861
|
+
*/
|
|
12862
|
+
function traverseSelectionSet(node) {
|
|
12863
|
+
if (node === undefined) {
|
|
12864
|
+
return;
|
|
12865
|
+
}
|
|
12866
|
+
for (const selection of node.selections) {
|
|
12867
|
+
// FragmentSpreadNode doesn't have a selection set
|
|
12868
|
+
// which should be handled at this methods entry point
|
|
12869
|
+
const { directives, selectionSet } = selection;
|
|
12870
|
+
let selectionName;
|
|
12871
|
+
if (selection.kind !== 'InlineFragment') {
|
|
12872
|
+
selectionName = selection.name.value;
|
|
12873
|
+
}
|
|
12874
|
+
if (directives !== undefined && directives.length > 0) {
|
|
12875
|
+
// we follow this pattern instead of map to preserve the order of directives
|
|
12876
|
+
// order of directives may be significant as per graphql spec
|
|
12877
|
+
const index = directives.findIndex(isCustomDirective);
|
|
12878
|
+
if (index !== -1) {
|
|
12879
|
+
substituteDirectives(directives, index, selectionName);
|
|
12880
|
+
}
|
|
12881
|
+
}
|
|
12882
|
+
traverseSelectionSet(selectionSet);
|
|
12883
|
+
}
|
|
12884
|
+
return node;
|
|
12885
|
+
}
|
|
12886
|
+
/**
|
|
12887
|
+
* Accepts a document node and replaces the legacy custom directives with metaschema directives "in-place"
|
|
12888
|
+
* @param doc
|
|
12889
|
+
*/
|
|
12890
|
+
function metaschemaMapper(doc) {
|
|
12891
|
+
// this method is only callable for Executable definitions
|
|
12892
|
+
// such as Operations and Fragments
|
|
12893
|
+
// so we have to explicitly cast the definitions for ts
|
|
12894
|
+
const { definitions } = doc;
|
|
12895
|
+
for (const def of definitions) {
|
|
12896
|
+
const { directives, selectionSet } = def;
|
|
12897
|
+
if (directives !== undefined && directives.length > 0) {
|
|
12898
|
+
// we are making an assumption here that only one custom directive can be applied to a node
|
|
12899
|
+
// we can revisit if this condition changes
|
|
12900
|
+
const index = directives.findIndex(isCustomDirective);
|
|
12901
|
+
if (index !== -1) {
|
|
12902
|
+
substituteDirectives(directives, index, undefined);
|
|
12903
|
+
}
|
|
12904
|
+
}
|
|
12905
|
+
traverseSelectionSet(selectionSet);
|
|
12906
|
+
}
|
|
12907
|
+
}
|
|
12908
|
+
|
|
12909
12909
|
/**
|
|
12910
12910
|
* @deprecated In favor of gql tagged template literal
|
|
12911
12911
|
*/
|
|
12912
12912
|
function parseAndVisit(source) {
|
|
12913
|
-
const ast =
|
|
12913
|
+
const ast = parse(source);
|
|
12914
12914
|
const luvioDocumentNode = transform(ast);
|
|
12915
12915
|
// In-place substitution of metaschema annotations
|
|
12916
12916
|
metaschemaMapper(ast);
|
package/package.json
CHANGED
|
@@ -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,5 +1,5 @@
|
|
|
1
1
|
import * as parser from '../main';
|
|
2
|
-
import { astResolver,
|
|
2
|
+
import { astResolver, stripLocation } from '../gql';
|
|
3
3
|
|
|
4
4
|
describe('LDS GraphQL Parser', () => {
|
|
5
5
|
describe('parseAndVisit', () => {
|
|
@@ -643,7 +643,9 @@ describe('LDS GraphQL Parser', () => {
|
|
|
643
643
|
const target = parser.parseAndVisit(source);
|
|
644
644
|
const metaschemaAST = astResolver(target);
|
|
645
645
|
|
|
646
|
-
expect(metaschemaAST).toStrictEqual(
|
|
646
|
+
expect(stripLocation(metaschemaAST)).toStrictEqual(
|
|
647
|
+
stripLocation(parser.parse(metaschemaSource))
|
|
648
|
+
);
|
|
647
649
|
});
|
|
648
650
|
});
|
|
649
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:
|
|
@@ -58,11 +55,6 @@ export 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);
|
|
@@ -228,14 +220,3 @@ export function gql(
|
|
|
228
220
|
|
|
229
221
|
return updateReferenceMapAndGetKey(insertFragments(document, inputSubstitutionFragments));
|
|
230
222
|
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Enable the parser to add corresponding metaschema directives for backwards compatibility
|
|
234
|
-
*/
|
|
235
|
-
export function enableAddMetaschemaDirective() {
|
|
236
|
-
addMetaschemaDirectives = true;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export function disableAddMetaschemaDirective() {
|
|
240
|
-
addMetaschemaDirectives = false;
|
|
241
|
-
}
|
package/src/main.ts
CHANGED
|
@@ -28,7 +28,7 @@ import {
|
|
|
28
28
|
LuvioTypeNode,
|
|
29
29
|
} from './ast';
|
|
30
30
|
import { transform } from './document';
|
|
31
|
-
import {
|
|
31
|
+
import { updateReferenceMapWithKnownKey } from './gql';
|
|
32
32
|
import { metaschemaMapper } from './metaschema';
|
|
33
33
|
|
|
34
34
|
export type {
|
|
@@ -65,6 +65,7 @@ export type {
|
|
|
65
65
|
InputObjectTypeDefinitionNode,
|
|
66
66
|
InlineFragmentNode,
|
|
67
67
|
} from 'graphql/language';
|
|
68
|
+
import { parse } from 'graphql/language';
|
|
68
69
|
export { ASTVisitor, parse, Kind, print, visit } from 'graphql/language';
|
|
69
70
|
export { isScalarType } from 'graphql/type';
|
|
70
71
|
export { stripIgnoredCharacters } from 'graphql/utilities';
|
|
@@ -105,7 +106,7 @@ export {
|
|
|
105
106
|
* @deprecated In favor of gql tagged template literal
|
|
106
107
|
*/
|
|
107
108
|
export function parseAndVisit(source: string): LuvioDocumentNode {
|
|
108
|
-
const ast =
|
|
109
|
+
const ast = parse(source);
|
|
109
110
|
const luvioDocumentNode = transform(ast!);
|
|
110
111
|
|
|
111
112
|
// In-place substitution of metaschema annotations
|