package graphql.execution.nextgen;

import graphql.Internal;
import graphql.execution.MissingRootTypeException;
import graphql.language.OperationDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

import static graphql.Assert.assertShouldNeverHappen;
import static graphql.language.OperationDefinition.Operation.MUTATION;
import static graphql.language.OperationDefinition.Operation.QUERY;
import static graphql.language.OperationDefinition.Operation.SUBSCRIPTION;

@Internal
public class Common {

    public static GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) {
        OperationDefinition.Operation operation = operationDefinition.getOperation();
        if (operation == MUTATION) {
            GraphQLObjectType mutationType = graphQLSchema.getMutationType();
            if (mutationType == null) {
                throw new MissingRootTypeException("Schema is not configured for mutations.", operationDefinition.getSourceLocation());
            }
            return mutationType;
        } else if (operation == QUERY) {
            GraphQLObjectType queryType = graphQLSchema.getQueryType();
            if (queryType == null) {
                throw new MissingRootTypeException("Schema does not define the required query root type.", operationDefinition.getSourceLocation());
            }
            return queryType;
        } else if (operation == SUBSCRIPTION) {
            GraphQLObjectType subscriptionType = graphQLSchema.getSubscriptionType();
            if (subscriptionType == null) {
                throw new MissingRootTypeException("Schema is not configured for subscriptions.", operationDefinition.getSourceLocation());
            }
            return subscriptionType;
        } else {
            return assertShouldNeverHappen("Unhandled case.  An extra operation enum has been added without code support");
        }
    }



}

@@ -229,16 +229,44 @@
 chema;%0A%0A
+import java.util.Optional;%0A%0A
 import s
@@ -901,19 +901,42 @@
         
-if 
+return Optional.ofNullable
 (mutatio
@@ -936,35 +936,25 @@
 mutationType
- == null) %7B
+)
 %0A           
@@ -950,37 +950,54 @@
                 
-throw
+    .orElseThrow(() -%3E
  new MissingRoot
@@ -1097,55 +1097,9 @@
 n())
-;%0A            %7D%0A            return mutationType
+)
 ;%0A  
@@ -1225,54 +1225,84 @@
     
-if (queryType == null) %7B%0A                throw
+return Optional.ofNullable(queryType)%0A                    .orElseThrow(() -%3E
  new
@@ -1427,52 +1427,9 @@
 n())
-;%0A            %7D%0A            return queryType
+)
 ;%0A  
@@ -1572,19 +1572,42 @@
         
-if 
+return Optional.ofNullable
 (subscri
@@ -1619,20 +1619,12 @@
 Type
- == null) %7B%0A
+)%0A  
     
@@ -1639,13 +1639,28 @@
     
-throw
+  .orElseThrow(() -%3E
  new
@@ -1776,59 +1776,9 @@
 n())
-;%0A            %7D%0A            return subscriptionType
+)
 ;%0A  
@@ -1932,13 +1932,10 @@
 %7D%0A    %7D%0A
-%0A%0A%0A
 %7D%0A

Change the way throwing an exception using Optional (#2375)