Skip to content

Commit de5cf4f

Browse files
committed
Make imports global
1 parent 1acdf34 commit de5cf4f

File tree

9 files changed

+273
-53
lines changed

9 files changed

+273
-53
lines changed

graphql/__init__.py

Lines changed: 139 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'''
2-
GraphQL provides a Python implementation for the GraphQL specification
2+
GraphQL.js provides a reference implementation for the GraphQL specification
33
but is also a useful utility for operating on GraphQL files and building
44
sophisticated tools.
55
@@ -14,33 +14,143 @@
1414
1515
This also includes utility functions for operating on GraphQL types and
1616
GraphQL documents to facilitate building tools.
17+
18+
You may also import from each sub-directory directly. For example, the
19+
following two import statements are equivalent:
20+
21+
from graphql import parse
22+
from graphql.language.base import parse
1723
'''
1824

19-
from .execution import ExecutionResult, execute
20-
from .language.parser import parse
21-
from .language.source import Source
22-
from .validation import validate
23-
24-
25-
def graphql(schema, request='', root=None, args=None, operation_name=None):
26-
try:
27-
source = Source(request, 'GraphQL request')
28-
ast = parse(source)
29-
validation_errors = validate(schema, ast)
30-
if validation_errors:
31-
return ExecutionResult(
32-
errors=validation_errors,
33-
invalid=True,
34-
)
35-
return execute(
36-
schema,
37-
ast,
38-
root,
39-
operation_name=operation_name,
40-
variable_values=args or {},
41-
)
42-
except Exception as e:
43-
return ExecutionResult(
44-
errors=[e],
45-
invalid=True,
46-
)
25+
26+
# The primary entry point into fulfilling a GraphQL request.
27+
from .graphql import (
28+
graphql
29+
)
30+
31+
32+
# Create and operate on GraphQL type definitions and schema.
33+
from .type import ( # no import order
34+
GraphQLSchema,
35+
36+
# Definitions
37+
GraphQLScalarType,
38+
GraphQLObjectType,
39+
GraphQLInterfaceType,
40+
GraphQLUnionType,
41+
GraphQLEnumType,
42+
GraphQLInputObjectType,
43+
GraphQLList,
44+
GraphQLNonNull,
45+
46+
# Scalars
47+
GraphQLInt,
48+
GraphQLFloat,
49+
GraphQLString,
50+
GraphQLBoolean,
51+
GraphQLID,
52+
53+
# Predicates
54+
is_type,
55+
is_input_type,
56+
is_output_type,
57+
is_leaf_type,
58+
is_composite_type,
59+
is_abstract_type,
60+
61+
# Un-modifiers
62+
get_nullable_type,
63+
get_named_type,
64+
)
65+
66+
67+
# Parse and operate on GraphQL language source files.
68+
from .language.base import ( # no import order
69+
Source,
70+
get_location,
71+
72+
# Parse
73+
parse,
74+
parse_value,
75+
76+
# Print
77+
print_ast,
78+
79+
# Visit
80+
visit,
81+
ParallelVisitor,
82+
TypeInfoVisitor,
83+
BREAK,
84+
)
85+
86+
87+
# Execute GraphQL queries.
88+
from .execution import ( # no import order
89+
execute,
90+
)
91+
92+
93+
# Validate GraphQL queries.
94+
from .validation import ( # no import order
95+
validate,
96+
specified_rules,
97+
)
98+
99+
# Create and format GraphQL errors.
100+
from .error import ( # no import order
101+
GraphQLError,
102+
format_error,
103+
)
104+
105+
106+
# Utilities for operating on GraphQL type schema and parsed sources.
107+
from .utils.base import ( # no import order
108+
# The GraphQL query recommended for a full schema introspection.
109+
introspection_query,
110+
111+
# Gets the target Operation from a Document
112+
get_operation_ast,
113+
114+
# Build a GraphQLSchema from an introspection result.
115+
build_client_schema,
116+
117+
# Build a GraphQLSchema from a parsed GraphQL Schema language AST.
118+
build_ast_schema,
119+
120+
# Extends an existing GraphQLSchema from a parsed GraphQL Schema
121+
# language AST.
122+
extend_schema,
123+
124+
# Print a GraphQLSchema to GraphQL Schema language.
125+
print_schema,
126+
127+
# Create a GraphQLType from a GraphQL language AST.
128+
type_from_ast,
129+
130+
# Create a JavaScript value from a GraphQL language AST.
131+
value_from_ast,
132+
133+
# Create a GraphQL language AST from a JavaScript value.
134+
ast_from_value,
135+
136+
# A helper to use within recursive-descent visitors which need to be aware of
137+
# the GraphQL type system.
138+
TypeInfo,
139+
140+
# Determine if JavaScript values adhere to a GraphQL type.
141+
is_valid_value,
142+
143+
# Determine if AST values adhere to a GraphQL type.
144+
is_valid_literal_value,
145+
146+
# Concatenates multiple AST together.
147+
concat_ast,
148+
149+
# Comparators for types
150+
is_equal_type,
151+
is_type_sub_type_of,
152+
do_types_overlap,
153+
154+
# Asserts a string is a valid GraphQL name.
155+
assert_valid_name,
156+
)

graphql/error.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ class Error(Exception):
66

77

88
class GraphQLError(Error):
9-
__slots__ = 'message', 'nodes', 'stack', '_source', '_positions'
9+
__slots__ = 'message', 'nodes', 'original_error', '_source', '_positions'
1010

11-
def __init__(self, message, nodes=None, stack=None, source=None, positions=None):
11+
def __init__(self, message, nodes=None, original_error=None, source=None, positions=None):
1212
super(GraphQLError, self).__init__(message)
1313
self.message = message or 'An unknown error occurred.'
1414
self.nodes = nodes
15-
self.stack = stack or message
15+
self.original_error = original_error
1616
self._source = source
1717
self._positions = positions
1818

graphql/graphql.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from .execution import ExecutionResult, execute
2+
from .language.parser import parse
3+
from .language.source import Source
4+
from .validation import validate
5+
6+
7+
# This is the primary entry point function for fulfilling GraphQL operations
8+
# by parsing, validating, and executing a GraphQL document along side a
9+
# GraphQL schema.
10+
11+
# More sophisticated GraphQL servers, such as those which persist queries,
12+
# may wish to separate the validation and execution phases to a static time
13+
# tooling step, and a server runtime step.
14+
15+
# schema:
16+
# The GraphQL type system to use when validating and executing a query.
17+
# requestString:
18+
# A GraphQL language formatted string representing the requested operation.
19+
# rootValue:
20+
# The value provided as the first argument to resolver functions on the top
21+
# level type (e.g. the query object type).
22+
# variableValues:
23+
# A mapping of variable name to runtime value to use for all variables
24+
# defined in the requestString.
25+
# operationName:
26+
# The name of the operation to use if requestString contains multiple
27+
# possible operations. Can be omitted if requestString contains only
28+
# one operation.
29+
def graphql(schema, request_string='', root_value=None, context_value=None,
30+
variable_values=None, operation_name=None, executor=None):
31+
try:
32+
source = Source(request_string, 'GraphQL request')
33+
ast = parse(source)
34+
validation_errors = validate(schema, ast)
35+
if validation_errors:
36+
return ExecutionResult(
37+
errors=validation_errors,
38+
invalid=True,
39+
)
40+
return execute(
41+
schema,
42+
ast,
43+
root_value,
44+
context_value,
45+
operation_name=operation_name,
46+
variable_values=variable_values or {},
47+
executor=executor
48+
)
49+
except Exception as e:
50+
return ExecutionResult(
51+
errors=[e],
52+
invalid=True,
53+
)

graphql/language/base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from .location import ( get_location )
2+
from .lexer import ( Lexer )
3+
from .parser import ( parse, parse_value )
4+
from .printer import ( print_ast )
5+
from .source import ( Source )
6+
from .visitor import ( visit, ParallelVisitor, TypeInfoVisitor, BREAK )

graphql/type/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
is_composite_type,
1818
is_input_type,
1919
is_leaf_type,
20+
is_type,
21+
get_nullable_type,
2022
is_output_type
2123
)
2224
from .directives import (

graphql/utils/base.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# The GraphQL query recommended for a full schema introspection.
2+
from .introspection_query import ( introspection_query )
3+
4+
# Gets the target Operation from a Document
5+
from .get_operation_ast import ( get_operation_ast )
6+
7+
# Build a GraphQLSchema from an introspection result.
8+
from .build_client_schema import ( build_client_schema )
9+
10+
# Build a GraphQLSchema from a parsed GraphQL Schema language AST.
11+
from .build_ast_schema import ( build_ast_schema )
12+
13+
# Extends an existing GraphQLSchema from a parsed GraphQL Schema language AST.
14+
from .extend_schema import ( extend_schema )
15+
16+
# Print a GraphQLSchema to GraphQL Schema language.
17+
from .schema_printer import ( print_schema, print_introspection_schema )
18+
19+
# Create a GraphQLType from a GraphQL language AST.
20+
from .type_from_ast import ( type_from_ast )
21+
22+
# Create a JavaScript value from a GraphQL language AST.
23+
from .value_from_ast import ( value_from_ast )
24+
25+
# Create a GraphQL language AST from a JavaScript value.
26+
from .ast_from_value import ( ast_from_value )
27+
28+
# A helper to use within recursive-descent visitors which need to be aware of
29+
# the GraphQL type system.
30+
from .type_info import ( TypeInfo )
31+
32+
# Determine if JavaScript values adhere to a GraphQL type.
33+
from .is_valid_value import ( is_valid_value )
34+
35+
# Determine if AST values adhere to a GraphQL type.
36+
from .is_valid_literal_value import ( is_valid_literal_value )
37+
38+
# Concatenates multiple AST together.
39+
from .concat_ast import ( concat_ast )
40+
41+
# Comparators for types
42+
from .type_comparators import (
43+
is_equal_type,
44+
is_type_sub_type_of,
45+
do_types_overlap
46+
)
47+
48+
# Asserts that a string is a valid GraphQL name
49+
from .assert_valid_name import ( assert_valid_name )

graphql/validation/__init__.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,2 @@
1-
from ..language.visitor import ParallelVisitor, TypeInfoVisitor, visit
2-
from ..type import GraphQLSchema
3-
from ..utils.type_info import TypeInfo
4-
from .context import ValidationContext
1+
from .validation import validate
52
from .rules import specified_rules
6-
7-
8-
def validate(schema, ast, rules=specified_rules):
9-
assert schema, 'Must provide schema'
10-
assert ast, 'Must provide document'
11-
assert isinstance(schema, GraphQLSchema)
12-
type_info = TypeInfo(schema)
13-
return visit_using_rules(schema, type_info, ast, rules)
14-
15-
16-
def visit_using_rules(schema, type_info, ast, rules):
17-
context = ValidationContext(schema, ast, type_info)
18-
visitors = [rule(context) for rule in rules]
19-
visit(ast, TypeInfoVisitor(type_info, ParallelVisitor(visitors)))
20-
return context.get_errors()

graphql/validation/tests/test_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from graphql import parse, validate
22
from graphql.utils.type_info import TypeInfo
3-
from graphql.validation import visit_using_rules
3+
from graphql.validation.validate import visit_using_rules
44
from graphql.validation.rules import specified_rules
55

66
from .utils import test_schema

graphql/validation/context.py renamed to graphql/validation/validation.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
from ..language.ast import (FragmentDefinition, FragmentSpread,
22
OperationDefinition)
3-
from ..language.visitor import TypeInfoVisitor, Visitor, visit
3+
from ..language.visitor import ParallelVisitor, TypeInfoVisitor, Visitor, visit
4+
from ..type import GraphQLSchema
5+
from ..utils.type_info import TypeInfo
6+
from .rules import specified_rules
7+
8+
9+
def validate(schema, ast, rules=specified_rules):
10+
assert schema, 'Must provide schema'
11+
assert ast, 'Must provide document'
12+
assert isinstance(schema, GraphQLSchema)
13+
type_info = TypeInfo(schema)
14+
return visit_using_rules(schema, type_info, ast, rules)
15+
16+
17+
def visit_using_rules(schema, type_info, ast, rules):
18+
context = ValidationContext(schema, ast, type_info)
19+
visitors = [rule(context) for rule in rules]
20+
visit(ast, TypeInfoVisitor(type_info, ParallelVisitor(visitors)))
21+
return context.get_errors()
422

523

624
class VariableUsage(object):

0 commit comments

Comments
 (0)