graphql.kickstart.execution.OperationNameExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphql-java-kickstart-jakarta5 Show documentation
Show all versions of graphql-java-kickstart-jakarta5 Show documentation
relay.js-compatible GraphQL servlet
The newest version!
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 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;
}
}