graphql-typed-python 0.1.2__py3-none-any.whl → 0.1.3__py3-none-any.whl
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.
- graphql_typed/generator/client.py +54 -35
- graphql_typed/generator/operations.py +21 -4
- graphql_typed/generator/types.py +13 -6
- {graphql_typed_python-0.1.2.dist-info → graphql_typed_python-0.1.3.dist-info}/METADATA +28 -2
- {graphql_typed_python-0.1.2.dist-info → graphql_typed_python-0.1.3.dist-info}/RECORD +8 -8
- {graphql_typed_python-0.1.2.dist-info → graphql_typed_python-0.1.3.dist-info}/WHEEL +0 -0
- {graphql_typed_python-0.1.2.dist-info → graphql_typed_python-0.1.3.dist-info}/entry_points.txt +0 -0
- {graphql_typed_python-0.1.2.dist-info → graphql_typed_python-0.1.3.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
from collections import defaultdict
|
|
2
1
|
from collections.abc import Iterable
|
|
2
|
+
from functools import cache
|
|
3
3
|
|
|
4
4
|
from graphql import (
|
|
5
5
|
FieldNode,
|
|
6
|
+
GraphQLInterfaceType,
|
|
6
7
|
GraphQLNonNull,
|
|
7
8
|
GraphQLObjectType,
|
|
8
9
|
GraphQLSchema,
|
|
9
|
-
|
|
10
|
+
SelectionSetNode,
|
|
10
11
|
get_named_type,
|
|
11
12
|
)
|
|
13
|
+
from graphql.execution.collect_fields import collect_fields
|
|
12
14
|
|
|
13
15
|
from graphql_typed import Operation
|
|
14
16
|
from graphql_typed.generator.operations import Document
|
|
@@ -17,42 +19,59 @@ from graphql_typed.generator.types import Inputs, Names, annotation, dictionary,
|
|
|
17
19
|
|
|
18
20
|
def response(
|
|
19
21
|
name: str,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
+
schema: GraphQLSchema,
|
|
23
|
+
parent: GraphQLObjectType | GraphQLInterfaceType,
|
|
24
|
+
selection_set: SelectionSetNode,
|
|
22
25
|
names: Names,
|
|
23
26
|
definitions: list[str],
|
|
24
27
|
) -> None:
|
|
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
|
-
|
|
28
|
+
# A shared relationship has the same selected children in multiple variants.
|
|
29
|
+
@cache
|
|
30
|
+
def nested_type(
|
|
31
|
+
key: str,
|
|
32
|
+
target: GraphQLObjectType | GraphQLInterfaceType,
|
|
33
|
+
nodes: tuple[FieldNode, ...],
|
|
34
|
+
) -> str:
|
|
35
|
+
nested = names.take(f"{name}_{key}")
|
|
36
|
+
children = tuple(
|
|
37
|
+
child
|
|
38
|
+
for node in nodes
|
|
39
|
+
if node.selection_set
|
|
40
|
+
for child in node.selection_set.selections
|
|
41
|
+
)
|
|
42
|
+
response(
|
|
43
|
+
nested,
|
|
44
|
+
schema,
|
|
45
|
+
target,
|
|
46
|
+
SelectionSetNode(selections=children),
|
|
47
|
+
names,
|
|
48
|
+
definitions,
|
|
49
|
+
)
|
|
50
|
+
return nested
|
|
51
|
+
|
|
52
|
+
abstract = isinstance(parent, GraphQLInterfaceType)
|
|
53
|
+
implementations = schema.get_possible_types(parent) if abstract else (parent,)
|
|
54
|
+
variants = []
|
|
55
|
+
for obj in implementations:
|
|
56
|
+
variant = names.take(f"{name}_{obj.name}") if abstract else name
|
|
57
|
+
variants.append(variant)
|
|
58
|
+
items = []
|
|
59
|
+
for key, nodes in collect_fields(schema, {}, {}, obj, selection_set).items():
|
|
60
|
+
field_name = nodes[0].name.value
|
|
61
|
+
if field_name == "__typename":
|
|
62
|
+
item_type = f"t.Literal[{obj.name!r}]"
|
|
52
63
|
else:
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
field_type = obj.fields[field_name].type
|
|
65
|
+
target = get_named_type(field_type)
|
|
66
|
+
if isinstance(target, (GraphQLObjectType, GraphQLInterfaceType)):
|
|
67
|
+
leaf = nested_type(key, target, tuple(nodes))
|
|
68
|
+
else:
|
|
69
|
+
leaf = scalar(target, input_=False)
|
|
70
|
+
item_type = annotation(field_type, leaf)
|
|
71
|
+
items.append((key, item_type, True))
|
|
72
|
+
definitions.append(dictionary(variant, items))
|
|
73
|
+
if abstract:
|
|
74
|
+
definitions.append(f"{name} = {' | '.join(variants) or 't.Never'}")
|
|
56
75
|
|
|
57
76
|
|
|
58
77
|
def render_client(
|
|
@@ -79,7 +98,7 @@ def render_client(
|
|
|
79
98
|
documents[op.kind][op.name] = doc.text
|
|
80
99
|
data = names.take(op.name + "Data")
|
|
81
100
|
assert doc.root is not None
|
|
82
|
-
response(data, doc.root, doc.
|
|
101
|
+
response(data, schema, doc.root, doc.selection_set, names, types)
|
|
83
102
|
params = ""
|
|
84
103
|
if doc.variables:
|
|
85
104
|
variables = names.take(op.name + "Variables")
|
|
@@ -6,6 +6,7 @@ from typing import Any
|
|
|
6
6
|
from graphql import (
|
|
7
7
|
GraphQLEnumType,
|
|
8
8
|
GraphQLInputObjectType,
|
|
9
|
+
GraphQLInterfaceType,
|
|
9
10
|
GraphQLList,
|
|
10
11
|
GraphQLNonNull,
|
|
11
12
|
GraphQLObjectType,
|
|
@@ -45,6 +46,7 @@ def literal(value: Any) -> str:
|
|
|
45
46
|
class Document:
|
|
46
47
|
def __init__(self, schema: GraphQLSchema, op: Operation):
|
|
47
48
|
assert_name(op.name)
|
|
49
|
+
self.schema = schema
|
|
48
50
|
self.variables: dict[str, GraphQLType] = {}
|
|
49
51
|
self.locations: dict[str, str] = {}
|
|
50
52
|
self.root = {"query": schema.query_type, "mutation": schema.mutation_type}[
|
|
@@ -64,7 +66,7 @@ class Document:
|
|
|
64
66
|
self.text = print_ast(ast)
|
|
65
67
|
definition = ast.definitions[0]
|
|
66
68
|
assert isinstance(definition, OperationDefinitionNode)
|
|
67
|
-
self.
|
|
69
|
+
self.selection_set = definition.selection_set
|
|
68
70
|
|
|
69
71
|
def value(self, value: Any, type_: GraphQLType, path: str) -> str:
|
|
70
72
|
if isinstance(value, Variable):
|
|
@@ -101,9 +103,21 @@ class Document:
|
|
|
101
103
|
return assert_name(value)
|
|
102
104
|
return literal(value)
|
|
103
105
|
|
|
104
|
-
def selection(
|
|
106
|
+
def selection(
|
|
107
|
+
self, parent: GraphQLObjectType | GraphQLInterfaceType, value: object
|
|
108
|
+
) -> str:
|
|
105
109
|
parts = []
|
|
106
110
|
for key, selections in object_value(value, parent.name).items():
|
|
111
|
+
if isinstance(parent, GraphQLInterfaceType) and key.startswith("... on "):
|
|
112
|
+
target = self.schema.get_type(key.removeprefix("... on "))
|
|
113
|
+
if not isinstance(
|
|
114
|
+
target, GraphQLObjectType
|
|
115
|
+
) or not self.schema.is_sub_type(parent, target):
|
|
116
|
+
raise ValueError(
|
|
117
|
+
f"{parent.name}: invalid fragment condition {key!r}"
|
|
118
|
+
)
|
|
119
|
+
parts.append(f"{key} {self.selection(target, selections)}")
|
|
120
|
+
continue
|
|
107
121
|
if selections == []:
|
|
108
122
|
raise ValueError(f"{parent.name}.{key}: empty selection list")
|
|
109
123
|
for item in selections if isinstance(selections, list) else [selections]:
|
|
@@ -116,7 +130,10 @@ class Document:
|
|
|
116
130
|
return "{ " + " ".join(parts) + " }"
|
|
117
131
|
|
|
118
132
|
def field(
|
|
119
|
-
self,
|
|
133
|
+
self,
|
|
134
|
+
parent: GraphQLObjectType | GraphQLInterfaceType,
|
|
135
|
+
key: str,
|
|
136
|
+
options: dict[str, Any],
|
|
120
137
|
) -> str:
|
|
121
138
|
path = f"{parent.name}.{key}"
|
|
122
139
|
if extra := options.keys() - {"alias", "args", "select"}:
|
|
@@ -135,7 +152,7 @@ class Document:
|
|
|
135
152
|
args.append(f"{name}: {self.value(value, arg.type, f'{path}({name})')}")
|
|
136
153
|
call = "(" + ", ".join(args) + ")" if args else ""
|
|
137
154
|
target = get_named_type(field.type)
|
|
138
|
-
if isinstance(target, GraphQLObjectType):
|
|
155
|
+
if isinstance(target, (GraphQLObjectType, GraphQLInterfaceType)):
|
|
139
156
|
child = " " + self.selection(target, options.get("select"))
|
|
140
157
|
elif is_leaf_type(target):
|
|
141
158
|
if "select" in options:
|
graphql_typed/generator/types.py
CHANGED
|
@@ -7,15 +7,16 @@ from graphql import (
|
|
|
7
7
|
GraphQLEnumType,
|
|
8
8
|
GraphQLInputField,
|
|
9
9
|
GraphQLInputObjectType,
|
|
10
|
+
GraphQLInterfaceType,
|
|
10
11
|
GraphQLList,
|
|
11
12
|
GraphQLNamedType,
|
|
12
13
|
GraphQLNonNull,
|
|
13
14
|
GraphQLObjectType,
|
|
14
15
|
GraphQLSchema,
|
|
15
16
|
GraphQLType,
|
|
17
|
+
GraphQLUnionType,
|
|
16
18
|
Undefined,
|
|
17
19
|
get_named_type,
|
|
18
|
-
is_abstract_type,
|
|
19
20
|
)
|
|
20
21
|
|
|
21
22
|
|
|
@@ -109,7 +110,8 @@ def render_schema(schema: GraphQLSchema) -> str:
|
|
|
109
110
|
objects = [
|
|
110
111
|
obj
|
|
111
112
|
for obj in schema.type_map.values()
|
|
112
|
-
if isinstance(obj, GraphQLObjectType
|
|
113
|
+
if isinstance(obj, (GraphQLObjectType, GraphQLInterfaceType))
|
|
114
|
+
and not obj.name.startswith("__")
|
|
113
115
|
]
|
|
114
116
|
fields = {obj.name: names.take(f"{obj.name}Fields") for obj in objects}
|
|
115
117
|
definitions = [
|
|
@@ -122,11 +124,16 @@ def render_schema(schema: GraphQLSchema) -> str:
|
|
|
122
124
|
leaves = f"{leaf} | list[{leaf}]"
|
|
123
125
|
for obj in objects:
|
|
124
126
|
items = [("__typename", leaves, False)]
|
|
127
|
+
if isinstance(obj, GraphQLInterfaceType):
|
|
128
|
+
items.extend(
|
|
129
|
+
(f"... on {impl.name}", fields[impl.name], False)
|
|
130
|
+
for impl in schema.get_possible_types(obj)
|
|
131
|
+
)
|
|
125
132
|
for key, field in obj.fields.items():
|
|
126
133
|
target = get_named_type(field.type)
|
|
127
|
-
if
|
|
134
|
+
if isinstance(target, GraphQLUnionType):
|
|
128
135
|
continue
|
|
129
|
-
if not field.args and not
|
|
136
|
+
if not field.args and target.name not in fields:
|
|
130
137
|
items.append((key, leaves, False))
|
|
131
138
|
continue
|
|
132
139
|
options = [("alias", "str", False)]
|
|
@@ -137,12 +144,12 @@ def render_schema(schema: GraphQLSchema) -> str:
|
|
|
137
144
|
definitions.append(dictionary(args, members))
|
|
138
145
|
required_args = any(required for _, _, required in members)
|
|
139
146
|
options.append(("args", args, required_args))
|
|
140
|
-
if
|
|
147
|
+
if target.name in fields:
|
|
141
148
|
options.append(("select", fields[target.name], True))
|
|
142
149
|
selection = names.take(f"{obj.name}_{key}Selection")
|
|
143
150
|
definitions.append(dictionary(selection, options))
|
|
144
151
|
single = selection
|
|
145
|
-
if not required_args and not
|
|
152
|
+
if not required_args and target.name not in fields:
|
|
146
153
|
single = f"t.Literal[True] | {single}"
|
|
147
154
|
items.append((key, f"{single} | list[{single}]", False))
|
|
148
155
|
definitions.append(dictionary(fields[obj.name], items))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: graphql-typed-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
4
4
|
Summary: Generate typed GraphQL selections, variables, and response dictionaries for Python.
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -87,7 +87,33 @@ Construct a client with `AsyncClient("https://example.test/graphql")`. The sync
|
|
|
87
87
|
- Use `var("name")` at an argument, input object field, or list item. The first use determines its GraphQL type. Further uses must have exactly the same type, including nullability and list markers. Custom scalars retain their GraphQL names and use Python `Any`.
|
|
88
88
|
- Input objects are dictionaries and enum values are string literals. Nullable variables can be omitted or set to `None`. A selected nullable response field has a required key whose value can be `None`.
|
|
89
89
|
- Use `schema.mutation(...)` and `client.mutation("Name")(...)` for mutations. Operations without variables are called as `client.query("Name")()`.
|
|
90
|
-
-
|
|
90
|
+
- Queries and mutations support object and interface results. Union results, subscriptions, and directives on selections are outside the current scope.
|
|
91
|
+
|
|
92
|
+
### Interface selections
|
|
93
|
+
|
|
94
|
+
Select shared fields directly. Add a key such as `"... on InterfaceVirtual"` for fields specific to a concrete implementation. The generated selection type provides completions for each fragment key and for the fields inside it.
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
peers: schema.InterfaceFields = {
|
|
98
|
+
"name": True,
|
|
99
|
+
"__typename": True,
|
|
100
|
+
"... on InterfaceVirtual": {
|
|
101
|
+
"parent_interface": {"select": {"name": True}},
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Use `peers` as the `select` value of an interface-valued field. Given an item from that result, `ty` can narrow on the selected `__typename`:
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
label = peer["name"]
|
|
110
|
+
if peer["__typename"] == "InterfaceVirtual":
|
|
111
|
+
parent = peer["parent_interface"]
|
|
112
|
+
if parent is not None:
|
|
113
|
+
label = parent["name"]
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
The physical branch has only `name` and `__typename`. Selecting `__typename` is optional. If you alias it, use the alias for narrowing. Nested relationships retain their selected fields and schema nullability.
|
|
91
117
|
|
|
92
118
|
## Schema sources and output
|
|
93
119
|
|
|
@@ -3,15 +3,15 @@ graphql_typed/cli.py,sha256=jqc_NqzM6YLIRsKQeQP0nYDzNhPTZ0ixWbUxPof0mIU,1202
|
|
|
3
3
|
graphql_typed/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
4
4
|
graphql_typed/generator/__init__.py,sha256=3tzbT79j_ySBO1BIYfsegpru-28RJppK90piTeXRXQs,38
|
|
5
5
|
graphql_typed/generator/api.py,sha256=1bpZhyIv-nBSlrtTjmWyTF1t9BXOhKqWO7Z6mpL6jNg,2300
|
|
6
|
-
graphql_typed/generator/client.py,sha256=
|
|
6
|
+
graphql_typed/generator/client.py,sha256=ih3PyWU5wnobFqesop81HRGoRQaO_S0p8VsSKkMMqs4,5649
|
|
7
7
|
graphql_typed/generator/factory.py,sha256=lpKC0UKcUdZOzzUCrvinCCcRxvsFR52zE5uPg6LSVOM,1589
|
|
8
8
|
graphql_typed/generator/loader.py,sha256=gZDy_BsMZuap_B_dMJi_6oZ5iBKBhZUpv9WP_J_hQoQ,1422
|
|
9
|
-
graphql_typed/generator/operations.py,sha256=
|
|
10
|
-
graphql_typed/generator/types.py,sha256=
|
|
9
|
+
graphql_typed/generator/operations.py,sha256=Ec6Sin75nSl4MdIZw_XxpTNsvjfWXve6aB2UK1DNmr4,6678
|
|
10
|
+
graphql_typed/generator/types.py,sha256=0IqmEh4g-8PlrqsKFCvHRJHo3QtUmUNRUygUSFgtQGM,5742
|
|
11
11
|
graphql_typed/generator/write.py,sha256=yaGhmm67tGRDS0RD80Y9jJDVLMFVJfUbSGCO1kplAB0,2180
|
|
12
12
|
graphql_typed/templates/transport.py,sha256=QsNa-YwoXF4KEEwo-6vXPSQ6PGbpqGJWftC5fcbZ30s,4098
|
|
13
|
-
graphql_typed_python-0.1.
|
|
14
|
-
graphql_typed_python-0.1.
|
|
15
|
-
graphql_typed_python-0.1.
|
|
16
|
-
graphql_typed_python-0.1.
|
|
17
|
-
graphql_typed_python-0.1.
|
|
13
|
+
graphql_typed_python-0.1.3.dist-info/METADATA,sha256=RBZhaup9XdxnAy8KRswXcASh9-nyW4RRmSuEkzsuTiM,7460
|
|
14
|
+
graphql_typed_python-0.1.3.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
15
|
+
graphql_typed_python-0.1.3.dist-info/entry_points.txt,sha256=ftNhHj6Pmy6vjE1fqbw3X6BdGruTuhezQpGKv516YKI,57
|
|
16
|
+
graphql_typed_python-0.1.3.dist-info/licenses/LICENSE,sha256=rgF1PgtpknpJGs3-6ZTTmwMQPdLOPkve2vLlCCldM7E,1069
|
|
17
|
+
graphql_typed_python-0.1.3.dist-info/RECORD,,
|
|
File without changes
|
{graphql_typed_python-0.1.2.dist-info → graphql_typed_python-0.1.3.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{graphql_typed_python-0.1.2.dist-info → graphql_typed_python-0.1.3.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|