Skip to content

Commit 35cad75

Browse files
committed
feat: update make_schema_from_files to make_schema_from_path
1 parent b13e674 commit 35cad75

File tree

6 files changed

+74
-52
lines changed

6 files changed

+74
-52
lines changed

examples/t.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import graphql
2+
3+
4+
type_defs = """
5+
type User {
6+
id: ID!
7+
name: String!
8+
}
9+
type Query {
10+
me: User!
11+
}
12+
type Mutation
13+
14+
type Address {
15+
id: ID!
16+
name: String!
17+
}
18+
19+
extend type Query {
20+
addresses: [Address!]!
21+
}
22+
23+
extend type Mutation {
24+
createAddress(name: String!): Address!
25+
}
26+
"""
27+
28+
if __name__ == '__main__':
29+
graphql.build_schema(type_defs)

gql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
type_resolver,
1212
)
1313
from .scalar import scalar_type # noqa
14-
from .schema import make_schema, make_schema_from_file, make_schema_from_files # noqa
14+
from .schema import make_schema, make_schema_from_file, make_schema_from_path # noqa
1515
from .schema_visitor import SchemaDirectiveVisitor # noqa
1616
from .utils import gql # noqa
1717

gql/schema.py

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from typing import Dict, List, Type, Union, cast
23

34
from graphql import (
@@ -36,12 +37,22 @@ def make_schema(
3637
if isinstance(type_defs, list):
3738
type_defs = join_type_defs(type_defs)
3839

39-
if not federation:
40+
if federation:
41+
# Remove custom schema directives (to avoid apollo-gateway crashes).
42+
sdl = purge_schema_directives(type_defs)
43+
44+
# remove subscription because Apollo Federation not support subscription yet.
45+
sdl = remove_subscription(type_defs)
46+
47+
type_defs = join_type_defs([type_defs, federation_service_type_defs])
4048
schema = build_schema(
4149
type_defs, assume_valid, assume_valid_sdl, no_location, experimental_fragment_variables
4250
)
51+
fix_federation_schema(schema)
4352
else:
44-
schema = make_federation_schema(type_defs)
53+
schema = build_schema(
54+
type_defs, assume_valid, assume_valid_sdl, no_location, experimental_fragment_variables
55+
)
4556

4657
register_resolvers(schema)
4758
register_enums(schema)
@@ -74,59 +85,50 @@ def make_schema_from_file(
7485
return schema
7586

7687

77-
def parse_from_file(file: str):
78-
with open(file, 'r') as f:
79-
return parse(f.read())
88+
def parse_from_file(file: Path):
89+
with file.open('r') as f:
90+
type_defs = f.read()
91+
parse(type_defs)
92+
return type_defs
8093

8194

82-
base_type_def = """
95+
base_type_defs = """
8396
type Query
8497
type Mutation
8598
"""
8699

87100

88-
def make_schema_from_files(
89-
files: List[str],
101+
def make_schema_from_path(
102+
path: str,
90103
assume_valid: bool = False,
91104
assume_valid_sdl: bool = False,
92105
no_location: bool = False,
93106
experimental_fragment_variables: bool = False,
94107
federation: bool = False,
95108
directives: Dict[str, Type[SchemaDirectiveVisitor]] = None,
96109
):
97-
if not files:
98-
return None
99-
schema = make_schema(
100-
base_type_def,
101-
assume_valid=assume_valid,
102-
assume_valid_sdl=assume_valid_sdl,
103-
no_location=no_location,
104-
experimental_fragment_variables=experimental_fragment_variables,
105-
federation=federation,
106-
directives=directives,
110+
p = Path(path)
111+
if p.is_file():
112+
type_defs = parse_from_file(p)
113+
elif p.is_dir():
114+
type_defs = [base_type_defs]
115+
for file in p.glob('*.graphql'):
116+
type_defs.extend([parse_from_file(file)])
117+
else:
118+
raise RuntimeError('path: expect a file or directory!')
119+
120+
return make_schema(
121+
type_defs,
122+
assume_valid,
123+
assume_valid_sdl,
124+
no_location,
125+
experimental_fragment_variables,
126+
federation,
127+
directives,
107128
)
108-
for file in files:
109-
schema = extend_schema(schema, parse_from_file(file))
110-
return schema
111-
112129

113-
def make_federation_schema(
114-
type_defs: str,
115-
assume_valid: bool = False,
116-
assume_valid_sdl: bool = False,
117-
no_location: bool = False,
118-
experimental_fragment_variables: bool = False,
119-
):
120-
# Remove custom schema directives (to avoid apollo-gateway crashes).
121-
sdl = purge_schema_directives(type_defs)
122130

123-
# remove subscription because Apollo Federation not support subscription yet.
124-
sdl = remove_subscription(type_defs)
125-
126-
type_defs = join_type_defs([type_defs, federation_service_type_defs])
127-
schema = build_schema(
128-
type_defs, assume_valid, assume_valid_sdl, no_location, experimental_fragment_variables
129-
)
131+
def fix_federation_schema(schema: GraphQLSchema):
130132
entity_types = get_entity_types(schema)
131133
if entity_types:
132134
schema = extend_schema(schema, parse(federation_entity_type_defs))
@@ -147,5 +149,3 @@ def make_federation_schema(
147149
if query_type:
148150
query_type = cast(GraphQLObjectType, query_type)
149151
query_type.fields["_service"].resolve = lambda _service, info: {"sdl": sdl}
150-
151-
return schema

tests/test_schema.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
from gql.schema import make_schema_from_files
1+
from gql.schema import make_schema_from_path
22

33
from pathlib import Path
44

55

6-
def test_make_schema_from_files():
7-
assert make_schema_from_files([]) is None
8-
9-
current_dir = Path(__file__).parent
10-
files = [
11-
str(current_dir / 'schema1.graphql'),
12-
str(current_dir / 'schema2.graphql')
13-
]
14-
schema = make_schema_from_files(files)
6+
def test_make_schema_from_path():
7+
schema = make_schema_from_path(str(Path(__file__).parent / 'schema'))
158
assert set(schema.query_type.fields.keys()) == {'me', 'addresses'}
16-
assert set(schema.mutation_type.fields.keys()) == {'createAddress'}
9+
assert set(schema.mutation_type.fields.keys()) == {'createAddress'}

0 commit comments

Comments
 (0)