2323import graphql .language .Type ;
2424import graphql .language .TypeDefinition ;
2525import graphql .language .TypeExtensionDefinition ;
26+ import graphql .language .TypeName ;
2627import graphql .language .UnionTypeDefinition ;
2728import graphql .language .Value ;
2829import graphql .schema .DataFetcher ;
@@ -128,11 +129,6 @@ void put(GraphQLInputType inputType) {
128129 RuntimeWiring getWiring () {
129130 return wiring ;
130131 }
131-
132- @ SuppressWarnings ("OptionalGetWithoutIsPresent" )
133- public SchemaDefinition getSchemaDefinition () {
134- return typeRegistry .schemaDefinition ().get ();
135- }
136132 }
137133
138134 private final SchemaTypeChecker typeChecker = new SchemaTypeChecker ();
@@ -145,7 +141,9 @@ public SchemaGenerator() {
145141 *
146142 * @param typeRegistry this can be obtained via {@link SchemaParser#parse(String)}
147143 * @param wiring this can be built using {@link RuntimeWiring#newRuntimeWiring()}
144+ *
148145 * @return an executable schema
146+ *
149147 * @throws SchemaProblem if there are problems in assembling a schema such as missing type resolvers or no operations defined
150148 */
151149 public GraphQLSchema makeExecutableSchema (TypeDefinitionRegistry typeRegistry , RuntimeWiring wiring ) throws SchemaProblem {
@@ -159,32 +157,56 @@ public GraphQLSchema makeExecutableSchema(TypeDefinitionRegistry typeRegistry, R
159157 }
160158
161159 private GraphQLSchema makeExecutableSchemaImpl (BuildContext buildCtx ) {
160+ GraphQLObjectType query ;
161+ GraphQLObjectType mutation ;
162+ GraphQLObjectType subscription ;
162163
163- SchemaDefinition schemaDefinition = buildCtx .getSchemaDefinition ();
164- List <OperationTypeDefinition > operationTypes = schemaDefinition .getOperationTypeDefinitions ();
164+ GraphQLSchema .Builder schemaBuilder = GraphQLSchema .newSchema ();
165165
166- // pre-flight checked via checker
167- @ SuppressWarnings ("OptionalGetWithoutIsPresent" )
168- OperationTypeDefinition queryOp = operationTypes .stream ().filter (op -> "query" .equals (op .getName ())).findFirst ().get ();
169- Optional <OperationTypeDefinition > mutationOp = operationTypes .stream ().filter (op -> "mutation" .equals (op .getName ())).findFirst ();
170- Optional <OperationTypeDefinition > subscriptionOp = operationTypes .stream ().filter (op -> "subscription" .equals (op .getName ())).findFirst ();
166+ //
167+ // Schema can be missing if the type is called 'Query'. Pre flight checks have checked that!
168+ //
169+ TypeDefinitionRegistry typeRegistry = buildCtx .getTypeRegistry ();
170+ if (!typeRegistry .schemaDefinition ().isPresent ()) {
171+ @ SuppressWarnings ("OptionalGetWithoutIsPresent" )
172+ TypeDefinition queryTypeDef = typeRegistry .getType ("Query" ).get ();
171173
172- GraphQLObjectType query = buildOperation (buildCtx , queryOp );
173- GraphQLObjectType mutation ;
174- GraphQLObjectType subscription ;
174+ query = buildOutputType (buildCtx , new TypeName (queryTypeDef .getName ()));
175+ schemaBuilder .query (query );
175176
176- GraphQLSchema .Builder schemaBuilder = GraphQLSchema
177- .newSchema ()
178- .query (query );
177+ Optional <TypeDefinition > mutationTypeDef = typeRegistry .getType ("Mutation" );
178+ if (mutationTypeDef .isPresent ()) {
179+ mutation = buildOutputType (buildCtx , new TypeName (mutationTypeDef .get ().getName ()));
180+ schemaBuilder .mutation (mutation );
181+ }
182+ Optional <TypeDefinition > subscriptionTypeDef = typeRegistry .getType ("Subscription" );
183+ if (subscriptionTypeDef .isPresent ()) {
184+ subscription = buildOutputType (buildCtx , new TypeName (subscriptionTypeDef .get ().getName ()));
185+ schemaBuilder .subscription (subscription );
186+ }
187+ } else {
188+ SchemaDefinition schemaDefinition = typeRegistry .schemaDefinition ().get ();
189+ List <OperationTypeDefinition > operationTypes = schemaDefinition .getOperationTypeDefinitions ();
179190
180- if (mutationOp .isPresent ()) {
181- mutation = buildOperation (buildCtx , mutationOp .get ());
182- schemaBuilder .mutation (mutation );
183- }
184- if (subscriptionOp .isPresent ()) {
185- subscription = buildOperation (buildCtx , subscriptionOp .get ());
186- schemaBuilder .subscription (subscription );
191+ // pre-flight checked via checker
192+ @ SuppressWarnings ("OptionalGetWithoutIsPresent" )
193+ OperationTypeDefinition queryOp = operationTypes .stream ().filter (op -> "query" .equals (op .getName ())).findFirst ().get ();
194+ Optional <OperationTypeDefinition > mutationOp = operationTypes .stream ().filter (op -> "mutation" .equals (op .getName ())).findFirst ();
195+ Optional <OperationTypeDefinition > subscriptionOp = operationTypes .stream ().filter (op -> "subscription" .equals (op .getName ())).findFirst ();
196+
197+ query = buildOperation (buildCtx , queryOp );
198+ schemaBuilder .query (query );
199+
200+ if (mutationOp .isPresent ()) {
201+ mutation = buildOperation (buildCtx , mutationOp .get ());
202+ schemaBuilder .mutation (mutation );
203+ }
204+ if (subscriptionOp .isPresent ()) {
205+ subscription = buildOperation (buildCtx , subscriptionOp .get ());
206+ schemaBuilder .subscription (subscription );
207+ }
187208 }
209+
188210 return schemaBuilder .build ();
189211 }
190212
@@ -200,6 +222,7 @@ private GraphQLObjectType buildOperation(BuildContext buildCtx, OperationTypeDef
200222 *
201223 * @param buildCtx the context we need to work out what we are doing
202224 * @param rawType the type to be built
225+ *
203226 * @return an output type
204227 */
205228 @ SuppressWarnings ("unchecked" )
@@ -535,8 +558,8 @@ private TypeResolver getTypeResolverForInterface(BuildContext buildCtx, Interfac
535558 private String buildDescription (Node node ) {
536559 List <Comment > comments = node .getComments ();
537560 List <String > lines = new ArrayList <>();
538- for (int i = 0 ; i < comments . size (); i ++ ) {
539- String commentLine = comments . get ( i ) .getContent ();
561+ for (Comment comment : comments ) {
562+ String commentLine = comment .getContent ();
540563 if (commentLine .trim ().isEmpty ()) {
541564 lines .clear ();
542565 } else {
0 commit comments