Skip to content

Commit

Permalink
Parse operation name from query if omitted
Browse files Browse the repository at this point in the history
fix #264
  • Loading branch information
oliemansm committed Nov 13, 2020
1 parent 6451779 commit 556380d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package graphql.kickstart.execution;

import static graphql.kickstart.execution.OperationNameExtractor.extractOperationName;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

Expand All @@ -12,59 +14,57 @@
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class GraphQLRequest {
private String query;
@JsonDeserialize(using = VariablesDeserializer.class)
private Map<String, Object> variables = new HashMap<>();
private String operationName;

public GraphQLRequest() {
}
private String query;
@JsonDeserialize(using = VariablesDeserializer.class)
private Map<String, Object> variables = new HashMap<>();
private String operationName;

public GraphQLRequest(String query, Map<String, Object> variables, String operationName) {
this.query = query;
this.operationName = operationName;
if (variables != null) {
this.variables = variables;
}
}
public GraphQLRequest() {
}

public static GraphQLRequest createIntrospectionRequest() {
return new GraphQLRequest(IntrospectionQuery.INTROSPECTION_QUERY, new HashMap<>(), null);
public GraphQLRequest(String query, Map<String, Object> variables, String operationName) {
this.query = query;
this.operationName = operationName;
if (variables != null) {
this.variables = variables;
}
}

public static GraphQLRequest createQueryOnlyRequest(String query) {
return new GraphQLRequest(query, new HashMap<>(), null);
}
public static GraphQLRequest createIntrospectionRequest() {
return new GraphQLRequest(IntrospectionQuery.INTROSPECTION_QUERY, new HashMap<>(), "IntrospectionQuery");
}

public String getQuery() {
return query;
}
public static GraphQLRequest createQueryOnlyRequest(String query) {
return new GraphQLRequest(query, new HashMap<>(), null);
}

public void setQuery(String query) {
this.query = query;
}
public String getQuery() {
return query;
}

public Map<String, Object> getVariables() {
return variables;
}
public void setQuery(String query) {
this.query = query;
}

public Map<String, Object> getVariables() {
return variables;
}

public void setVariables(Map<String, Object> variables) {
if (variables != null) {
this.variables = variables;
}
public void setVariables(Map<String, Object> variables) {
if (variables != null) {
this.variables = variables;
}
}

public String getOperationName() {
if (operationName != null && !operationName.isEmpty()) {
return operationName;
}
public String getOperationName() {
return extractOperationName(query, operationName, null);
}

return null;
}
public void setOperationName(String operationName) {
this.operationName = operationName;
}

public void setOperationName(String operationName) {
this.operationName = operationName;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package graphql.kickstart.execution;

import static graphql.kickstart.execution.StringUtils.isNotEmpty;

import graphql.language.Document;
import graphql.language.OperationDefinition;
import graphql.parser.InvalidSyntaxException;
import graphql.parser.Parser;
import java.util.List;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
class OperationNameExtractor {

static String extractOperationName(String gqlQuery, String requestedOperationName, String defaultIfNotFound) {
if (isNotEmpty(requestedOperationName)) {
return requestedOperationName;
}
if (isNotEmpty(gqlQuery)) {
return parseForOperationName(gqlQuery, defaultIfNotFound);
}
return defaultIfNotFound;
}

private static String parseForOperationName(String gqlQuery, String defaultIfNotFound) {
try {
Document document = new Parser().parseDocument(gqlQuery);
List<OperationDefinition> operations = document.getDefinitionsOfType(OperationDefinition.class);
if (operations.size() == 1) {
String name = operations.get(0).getName();
if (isNotEmpty(name)) {
return name;
}
}
} catch (InvalidSyntaxException ignored) {
// ignored
}
return defaultIfNotFound;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package graphql.kickstart.execution;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
class StringUtils {

static boolean isNotEmpty(CharSequence cs) {
return !isEmpty(cs);
}

static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}

}

0 comments on commit 556380d

Please sign in to comment.