Skip to content

Commit 889fc34

Browse files
committed
Schema Parser Tests
* Implement schema parser tests * Also implement util `ast_to_code` which will convert an AST the python code representation of the AST.
1 parent 2420a68 commit 889fc34

File tree

7 files changed

+799
-1
lines changed

7 files changed

+799
-1
lines changed

graphql/core/language/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ def parse_scalar_type_definition(parser):
639639

640640

641641
def parse_enum_type_definition(parser):
642-
start = parser.tokens.start
642+
start = parser.token.start
643643
expect_keyword(parser, 'enum')
644644

645645
return ast.EnumTypeDefinition(

graphql/core/utils/ast_to_code.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from ..language.parser import Loc
2+
from ..language.ast import Node
3+
4+
5+
def ast_to_code(ast, indent=0):
6+
"""
7+
Converts an ast into a python code representation of the AST.
8+
"""
9+
code = []
10+
append = lambda line: code.append((' ' * indent) + line)
11+
12+
if isinstance(ast, Node):
13+
append('ast.{}('.format(ast.__class__.__name__))
14+
indent += 1
15+
for i, k in enumerate(ast._fields, 1):
16+
v = getattr(ast, k)
17+
append('{}={},'.format(
18+
k,
19+
ast_to_code(v, indent),
20+
))
21+
if ast.loc:
22+
append('loc={}'.format(ast_to_code(ast.loc, indent)))
23+
24+
indent -= 1
25+
append(')')
26+
27+
elif isinstance(ast, Loc):
28+
append('loc({}, {})'.format(ast.start, ast.end))
29+
30+
elif isinstance(ast, list):
31+
if ast:
32+
append('[')
33+
indent += 1
34+
35+
for i, it in enumerate(ast, 1):
36+
is_last = i == len(ast)
37+
append(ast_to_code(it, indent) + (',' if not is_last else ''))
38+
39+
indent -= 1
40+
append(']')
41+
else:
42+
append('[]')
43+
44+
else:
45+
append(repr(ast))
46+
47+
return '\n'.join(code).strip()

tests/__init__.py

Whitespace-only changes.

tests/core_language/__init__.py

Whitespace-only changes.

tests/core_language/fixtures.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,45 @@
4444
query
4545
}
4646
"""
47+
48+
SCHEMA_KITCHEN_SINK = """
49+
50+
# Copyright (c) 2015, Facebook, Inc.
51+
# All rights reserved.
52+
#
53+
# This source code is licensed under the BSD-style license found in the
54+
# LICENSE file in the root directory of this source tree. An additional grant
55+
# of patent rights can be found in the PATENTS file in the same directory.
56+
57+
type Foo implements Bar {
58+
one: Type
59+
two(argument: InputType!): Type
60+
three(argument: InputType, other: String): Int
61+
four(argument: String = "string"): String
62+
five(argument: [String] = ["string", "string"]): String
63+
six(argument: InputType = {key: "value"}): Type
64+
}
65+
66+
interface Bar {
67+
one: Type
68+
four(argument: String = "string"): String
69+
}
70+
71+
union Feed = Story | Article | Advert
72+
73+
scalar CustomScalar
74+
75+
enum Site {
76+
DESKTOP
77+
MOBILE
78+
}
79+
80+
input InputType {
81+
key: String!
82+
answer: Int = 42
83+
}
84+
85+
extend type Foo {
86+
seven(argument: [String]): Type
87+
}
88+
"""

0 commit comments

Comments
 (0)