Skip to content

Commit 9f9ba13

Browse files
committed
implement converter isntead of a parser
1 parent 52704e8 commit 9f9ba13

File tree

15 files changed

+208
-2035
lines changed

15 files changed

+208
-2035
lines changed

example/query.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
query my {
1+
query myQuery {
22
hello
33
}

example/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
type Query {
2-
hello: String!
2+
hello: String! @deprecated(test: "", reason: "!")
33
}

packages/graphql-estree/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
"prepack": "bob prepack"
1717
},
1818
"dependencies": {
19+
"clone-deep": "4.0.1",
1920
"@graphql-eslint/types": "0.0.1",
2021
"@graphql-tools/utils": "6.2.0"
2122
},
2223
"devDependencies": {
24+
"@types/clone-deep": "4.0.1",
2325
"@types/eslint": "7.2.2",
2426
"bob-the-bundler": "1.1.0",
2527
"graphql": "15.3.0",
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { convertDescription, GraphQLESTreeNode, SafeGraphQLType, convertLocation, convertRange } from "@graphql-eslint/types";
2+
import {
3+
ASTNode,
4+
TypeNode,
5+
TypeInfo,
6+
visit,
7+
visitWithTypeInfo,
8+
} from "graphql";
9+
import cloneAst from "clone-deep";
10+
11+
export function convertToESTree<T extends ASTNode>(
12+
node: T,
13+
typeInfo?: TypeInfo
14+
): GraphQLESTreeNode<T> {
15+
const visitor = { leave: convertNode(typeInfo) };
16+
return visit(node, typeInfo ? visitWithTypeInfo(typeInfo, visitor) : visitor);
17+
}
18+
19+
function hasTypeField<T extends ASTNode>(
20+
obj: any
21+
): obj is T & { readonly type: TypeNode } {
22+
return obj && !!(obj as any).type;
23+
}
24+
25+
const convertNode = (typeInfo?: TypeInfo) => <T extends ASTNode>(
26+
node: T
27+
): GraphQLESTreeNode<T> => {
28+
const rawNode = cloneAst(node);
29+
const commonFields = {
30+
typeInfo: typeInfo
31+
? {
32+
argument: typeInfo.getArgument(),
33+
defaultValue: typeInfo.getDefaultValue(),
34+
directive: typeInfo.getDirective(),
35+
enumValue: typeInfo.getEnumValue(),
36+
fieldDef: typeInfo.getFieldDef(),
37+
inputType: typeInfo.getInputType(),
38+
parentInputType: typeInfo.getParentInputType(),
39+
parentType: typeInfo.getParentType(),
40+
gqlType: typeInfo.getType(),
41+
}
42+
: {},
43+
leadingComments: convertDescription(node),
44+
trailingComments: [],
45+
loc: convertLocation(node.loc),
46+
range: convertRange(node.loc),
47+
};
48+
49+
if (hasTypeField<T>(node)) {
50+
const { type: gqlType, loc: gqlLocation, ...rest } = node;
51+
const typeFieldSafe: SafeGraphQLType<T> = {
52+
...rest,
53+
gqlType,
54+
} as SafeGraphQLType<T & { readonly type: TypeNode }>;
55+
const estreeNode: GraphQLESTreeNode<T> = {
56+
...typeFieldSafe,
57+
...commonFields,
58+
type: node.kind,
59+
rawNode,
60+
gqlLocation,
61+
} as any as GraphQLESTreeNode<T>;
62+
63+
return estreeNode;
64+
} else {
65+
const { loc: gqlLocation, ...rest } = node;
66+
const typeFieldSafe: SafeGraphQLType<T> = rest as SafeGraphQLType<T & { readonly type: TypeNode }>;
67+
const estreeNode: GraphQLESTreeNode<T> = {
68+
...typeFieldSafe,
69+
...commonFields,
70+
type: node.kind,
71+
rawNode,
72+
gqlLocation,
73+
} as any as GraphQLESTreeNode<T>;
74+
75+
return estreeNode;
76+
}
77+
};

packages/graphql-estree/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./parser";
1+
export * from "./converter";

0 commit comments

Comments
 (0)