-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse operation name from query if omitted
fix #264
- Loading branch information
Showing
3 changed files
with
99 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
graphql-java-kickstart/src/main/java/graphql/kickstart/execution/OperationNameExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
graphql-java-kickstart/src/main/java/graphql/kickstart/execution/StringUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |