Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operationName for Apollo Federation Gateway #595 #596

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void actionPerformed(@NotNull AnActionEvent e, @NotNull Notification noti
final GraphQLSettings graphQLSettings = GraphQLSettings.getSettings(myProject);
String query = buildIntrospectionQuery(graphQLSettings);

final String requestJson = "{\"query\":\"" + StringEscapeUtils.escapeJavaScript(query) + "\"}";
final String requestJson = "{\"operationName\": \"IntrospectionQuery\", \"query\":\"" + StringEscapeUtils.escapeJavaScript(query) + "\"}";
HttpPost request = createRequest(endpoint, url, requestJson);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key "operationName" is required by my graphQLServer, was thinking could be made more generic by setting the keyname in .graphqlconfig maybe?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ended up putting textfield in GraphQL settings file for that

Task.Backgroundable task = new IntrospectionQueryTask(request, schemaPath, introspectionSourceFile, retry, graphQLSettings, endpoint, url);
ProgressManager.getInstance().run(task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
import java.security.GeneralSecurityException;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class GraphQLUIProjectService implements Disposable, FileEditorManagerListener, GraphQLConfigurationListener {
Expand Down Expand Up @@ -311,7 +313,17 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
final GraphQLQueryContext context = GraphQLQueryContextHighlightVisitor.getQueryContextBufferAndHighlightUnused(editor);

Map<String, Object> requestData = new HashMap<>();
requestData.put("query", context.query);
Pattern pattern = Pattern.compile("(query|mutation|subscription) (.*) ", Pattern.CASE_INSENSITIVE);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically this allows regex to find the name of query to be added to operationName key in data object, if no name is detected, "anonymous" is added to query and operationName key as a workaround since our graphql server wouldn't allow truly nameless queries

Pattern pattern2 = Pattern.compile("(query|mutation|subscription)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(context.query);
Matcher matcher2 = pattern2.matcher(context.query);
if (matcher.find()) {
requestData.put("operationName", matcher.group(2));
requestData.put("query", context.query);
} else if (matcher2.find()) {
requestData.put("operationName", "anonymous");
requestData.put("query", matcher2.group(1) + " anonymous" + context.query.split("(query|mutation|subscription)")[1]);
}
try {
requestData.put("variables", getQueryVariables(editor));
} catch (JsonSyntaxException jse) {
Expand Down