Skip to content

Commit b13e674

Browse files
committed
feat: support make schema from multi files
1 parent bacdc36 commit b13e674

File tree

7 files changed

+74
-3
lines changed

7 files changed

+74
-3
lines changed

gql/__init__.py

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

18-
__version__ = '0.2.2'
18+
__version__ = '0.2.3'

gql/schema.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,42 @@ def make_schema_from_file(
7474
return schema
7575

7676

77+
def parse_from_file(file: str):
78+
with open(file, 'r') as f:
79+
return parse(f.read())
80+
81+
82+
base_type_def = """
83+
type Query
84+
type Mutation
85+
"""
86+
87+
88+
def make_schema_from_files(
89+
files: List[str],
90+
assume_valid: bool = False,
91+
assume_valid_sdl: bool = False,
92+
no_location: bool = False,
93+
experimental_fragment_variables: bool = False,
94+
federation: bool = False,
95+
directives: Dict[str, Type[SchemaDirectiveVisitor]] = None,
96+
):
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,
107+
)
108+
for file in files:
109+
schema = extend_schema(schema, parse_from_file(file))
110+
return schema
111+
112+
77113
def make_federation_schema(
78114
type_defs: str,
79115
assume_valid: bool = False,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-gql"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
description = "Python schema-first GraphQL library based on GraphQL-core"
55
authors = ["ysun <sunyu418@gmail.com>"]
66
license = "MIT"
File renamed without changes.

tests/schema1.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type User {
2+
id: ID!
3+
name: String!
4+
}
5+
extend type Query {
6+
me: User!
7+
}

tests/schema2.graphql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
type Address {
2+
id: ID!
3+
name: String!
4+
}
5+
6+
extend type Query {
7+
addresses: [Address!]!
8+
}
9+
10+
extend type Mutation {
11+
createAddress(name: String!): Address!
12+
}

tests/test_schema.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from gql.schema import make_schema_from_files
2+
3+
from pathlib import Path
4+
5+
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)
15+
assert set(schema.query_type.fields.keys()) == {'me', 'addresses'}
16+
assert set(schema.mutation_type.fields.keys()) == {'createAddress'}

0 commit comments

Comments
 (0)