Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/graphql/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum ErrorType {

InvalidSyntax,
ValidationError,
DataFetchingException
DataFetchingException,
MutationNotSupported

}
7 changes: 1 addition & 6 deletions src/main/java/graphql/ExceptionWhileDataFetching.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ExceptionWhileDataFetching that = (ExceptionWhileDataFetching) o;

return Helper.equals(this, that);
return Helper.equals(this, o);
}

@Override
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/graphql/GraphQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, Exe

private GraphQL(GraphQLSchema graphQLSchema, ExecutionStrategy queryStrategy, ExecutionStrategy mutationStrategy, ExecutionIdProvider idProvider, Instrumentation instrumentation) {
this.graphQLSchema = assertNotNull(graphQLSchema,"queryStrategy must be non null");
this.queryStrategy = assertNotNull(queryStrategy, "queryStrategy must be non null");
this.queryStrategy = queryStrategy != null ? queryStrategy : new SimpleExecutionStrategy();
this.mutationStrategy = mutationStrategy != null ? mutationStrategy : new SimpleExecutionStrategy();
this.idProvider = assertNotNull(idProvider, "idProvider must be non null");
this.mutationStrategy = mutationStrategy;
this.instrumentation = instrumentation;
}

Expand Down Expand Up @@ -143,6 +143,9 @@ public Builder executionIdProvider(ExecutionIdProvider executionIdProvider) {
}

public GraphQL build() {
assertNotNull(graphQLSchema,"queryStrategy must be non null");
assertNotNull(queryExecutionStrategy, "queryStrategy must be non null");
assertNotNull(idProvider, "idProvider must be non null");
return new GraphQL(graphQLSchema, queryExecutionStrategy, mutationExecutionStrategy, idProvider, instrumentation);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/graphql/GraphQLError.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public static int hashCode(GraphQLError dis) {
return result;
}

public static boolean equals(GraphQLError dis, GraphQLError dat) {
if (dis == dat) {
public static boolean equals(GraphQLError dis, Object o) {
if (dis == o) {
return true;
}
if (o == null || dis.getClass() != o.getClass()) return false;

GraphQLError dat = (GraphQLError) o;

if (dis.getMessage() != null ? !dis.getMessage().equals(dat.getMessage()) : dat.getMessage() != null)
return false;
if (dis.getLocations() != null ? !dis.getLocations().equals(dat.getLocations()) : dat.getLocations() != null)
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/graphql/InvalidSyntaxError.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

InvalidSyntaxError that = (InvalidSyntaxError) o;

return Helper.equals(this,that);
return Helper.equals(this, o);
}

@Override
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/graphql/MutationNotSupportedError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package graphql;


import graphql.language.SourceLocation;

import java.util.List;

/**
* The graphql spec says that mutations are optional but it does not specify how to respond
* when it is not supported. This error is returned in this case.
*
* http://facebook.github.io/graphql/#sec-Initial-types
*/
public class MutationNotSupportedError implements GraphQLError {

@Override
public String getMessage() {
return "Mutations are not supported onm this schema";
}

@Override
public List<SourceLocation> getLocations() {
return null;
}

@Override
public ErrorType getErrorType() {
return ErrorType.MutationNotSupported;
}

@Override
public String toString() {
return "MutationNotSupportedError";
}

@Override
public boolean equals(Object o) {
return Helper.equals(this, o);
}

@Override
public int hashCode() {
return Helper.hashCode(this);
}
}
28 changes: 22 additions & 6 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQLException;
import graphql.MutationNotSupportedError;
import graphql.execution.instrumentation.Instrumentation;
import graphql.execution.instrumentation.InstrumentationContext;
import graphql.execution.instrumentation.parameters.DataFetchParameters;
Expand All @@ -13,10 +15,14 @@
import graphql.schema.GraphQLSchema;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import static graphql.language.OperationDefinition.Operation.MUTATION;
import static graphql.language.OperationDefinition.Operation.QUERY;

public class Execution {

private final FieldCollector fieldCollector = new FieldCollector();
Expand All @@ -38,15 +44,15 @@ public ExecutionResult execute(ExecutionId executionId, GraphQLSchema graphQLSch
return executeOperation(executionContext, root, executionContext.getOperationDefinition());
}

private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) {
if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) {
private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition.Operation operation) {
if (operation == MUTATION) {
return graphQLSchema.getMutationType();

} else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) {
} else if (operation == QUERY) {
return graphQLSchema.getQueryType();

} else {
throw new GraphQLException();
throw new GraphQLException("Unhandled case. An extra operation enum has been added without code support");
}
}

Expand All @@ -57,13 +63,23 @@ private ExecutionResult executeOperation(

InstrumentationContext<ExecutionResult> dataFetchCtx = instrumentation.beginDataFetch(new DataFetchParameters(executionContext));

GraphQLObjectType operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operationDefinition);
OperationDefinition.Operation operation = operationDefinition.getOperation();
GraphQLObjectType operationRootType = getOperationRootType(executionContext.getGraphQLSchema(), operation);

//
// do we have a mutation operation root type. The spec says if we don't then mutations are not allowed to be executed
//
// for the record earlier code has asserted that we have a query type in the schema since the spec says this is
// ALWAYS required
if (operation == MUTATION && operationRootType == null) {
return new ExecutionResultImpl(Collections.singletonList(new MutationNotSupportedError()));
}

Map<String, List<Field>> fields = new LinkedHashMap<String, List<Field>>();
fieldCollector.collectFields(executionContext, operationRootType, operationDefinition.getSelectionSet(), new ArrayList<String>(), fields);

ExecutionResult result;
if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) {
if (operation == MUTATION) {
result = mutationStrategy.execute(executionContext, operationRootType, root, fields);
} else {
result = queryStrategy.execute(executionContext, operationRootType, root, fields);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
package graphql.schema.validation;

import java.util.Collection;

import graphql.GraphQLException;

import java.util.Collection;

public class InvalidSchemaException extends GraphQLException {

private final String message;


public InvalidSchemaException(Collection<ValidationError> errors) {
super(buildErrorMsg(errors));
}

private static String buildErrorMsg(Collection<ValidationError> errors) {
StringBuilder message = new StringBuilder("invalid schema:");
for (ValidationError error : errors) {
message.append("\n").append(error.getDescription());
}
this.message = message.toString();
}

@Override
public String getMessage() {
return message;
return message.toString();
}
}
7 changes: 1 addition & 6 deletions src/main/java/graphql/validation/ValidationError.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,7 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

ValidationError that = (ValidationError) o;

return Helper.equals(this, that);
return Helper.equals(this, o);
}

@Override
Expand Down
33 changes: 25 additions & 8 deletions src/test/groovy/graphql/GraphQLTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ class GraphQLTest extends Specification {
set.add("Two")

def schema = GraphQLSchema.newSchema()
.query(GraphQLObjectType.newObject()
.name("QueryType")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("set")
.type(new GraphQLList(GraphQLString))
.dataFetcher({ set })))
.build()
.query(GraphQLObjectType.newObject()
.name("QueryType")
.field(GraphQLFieldDefinition.newFieldDefinition()
.name("set")
.type(new GraphQLList(GraphQLString))
.dataFetcher({ set })))
.build()

when:
def data = GraphQL.newGraphQL(schema).build().execute("query { set }").data
Expand Down Expand Up @@ -203,7 +203,7 @@ class GraphQLTest extends Specification {
.name("RootQueryType")
.field(newFieldDefinition().name("name").type(GraphQLString))
)
.build()
.build()

def query = """
query Query1 { name }
Expand All @@ -216,4 +216,21 @@ class GraphQLTest extends Specification {
then:
thrown(GraphQLException)
}

def "null mutation type does not throw an npe re: #345 but returns and error"() {
given:

GraphQLSchema schema = newSchema().query(
newObject()
.name("Query")
)
.build()

when:
def result = new GraphQL(schema).execute("mutation { doesNotExist }");

then:
result.errors.size() == 1
result.errors[0].class == MutationNotSupportedError
}
}