-
Notifications
You must be signed in to change notification settings - Fork 51
/
__init__.py
89 lines (77 loc) · 5.45 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Copyright 2017-present Kensho Technologies, LLC.
"""Commonly-used functions and data types from this package."""
from .compiler import CompilationResult, OutputMetadata # noqa
from .compiler import compile_graphql_to_gremlin, compile_graphql_to_match # noqa
from .query_formatting import insert_arguments_into_query # noqa
from .query_formatting.graphql_formatting import pretty_print_graphql # noqa
from .exceptions import GraphQLError # noqa
from .exceptions import GraphQLParsingError, GraphQLCompilationError # noqa
from .exceptions import GraphQLValidationError, GraphQLInvalidArgumentError # noqa
from .schema import DIRECTIVES # noqa
from .schema import GraphQLDate, GraphQLDateTime, GraphQLDecimal # noqa
__package_name__ = 'graphql-compiler'
__version__ = '1.7.1'
def graphql_to_match(schema, graphql_query, parameters, type_equivalence_hints=None):
"""Compile the GraphQL input using the schema into a MATCH query and associated metadata.
Args:
schema: GraphQL schema object describing the schema of the graph to be queried
graphql_string: the GraphQL query to compile to MATCH, as a string
parameters: dict, mapping argument name to its value, for every parameter the query expects.
type_equivalence_hints: optional dict of GraphQL interface or type -> GraphQL union.
Used as a workaround for GraphQL's lack of support for
inheritance across "types" (i.e. non-interfaces), as well as a
workaround for Gremlin's total lack of inheritance-awareness.
The key-value pairs in the dict specify that the "key" type
is equivalent to the "value" type, i.e. that the GraphQL type or
interface in the key is the most-derived common supertype
of every GraphQL type in the "value" GraphQL union.
Recursive expansion of type equivalence hints is not performed,
and only type-level correctness of this argument is enforced.
See README.md for more details on everything this parameter does.
*****
Be very careful with this option, as bad input here will
lead to incorrect output queries being generated.
*****
Returns:
a CompilationResult object, containing:
- query: string, the resulting compiled and parameterized query string
- language: string, specifying the language to which the query was compiled
- output_metadata: dict, output name -> OutputMetadata namedtuple object
- input_metadata: dict, name of input variables -> inferred GraphQL type, based on use
"""
compilation_result = compile_graphql_to_match(
schema, graphql_query, type_equivalence_hints=type_equivalence_hints)
return compilation_result._replace(
query=insert_arguments_into_query(compilation_result, parameters))
def graphql_to_gremlin(schema, graphql_query, parameters, type_equivalence_hints=None):
"""Compile the GraphQL input using the schema into a Gremlin query and associated metadata.
Args:
schema: GraphQL schema object describing the schema of the graph to be queried
graphql_string: the GraphQL query to compile to Gremlin, as a string
parameters: dict, mapping argument name to its value, for every parameter the query expects.
type_equivalence_hints: optional dict of GraphQL interface or type -> GraphQL union.
Used as a workaround for GraphQL's lack of support for
inheritance across "types" (i.e. non-interfaces), as well as a
workaround for Gremlin's total lack of inheritance-awareness.
The key-value pairs in the dict specify that the "key" type
is equivalent to the "value" type, i.e. that the GraphQL type or
interface in the key is the most-derived common supertype
of every GraphQL type in the "value" GraphQL union.
Recursive expansion of type equivalence hints is not performed,
and only type-level correctness of this argument is enforced.
See README.md for more details on everything this parameter does.
*****
Be very careful with this option, as bad input here will
lead to incorrect output queries being generated.
*****
Returns:
a CompilationResult object, containing:
- query: string, the resulting compiled and parameterized query string
- language: string, specifying the language to which the query was compiled
- output_metadata: dict, output name -> OutputMetadata namedtuple object
- input_metadata: dict, name of input variables -> inferred GraphQL type, based on use
"""
compilation_result = compile_graphql_to_gremlin(
schema, graphql_query, type_equivalence_hints=type_equivalence_hints)
return compilation_result._replace(
query=insert_arguments_into_query(compilation_result, parameters))