1+ from pathlib import Path
12from typing import Dict , List , Type , Union , cast
23
34from 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 = """
8396type Query
8497type 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
0 commit comments