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

Conversation

SammoMichael
Copy link

@SammoMichael SammoMichael commented Feb 10, 2022

Because of the way some GraphQL Servers are setup, a operationName key is required in the data object

{ "operationName": "SomeQuery", "query": {}, "variables": {}}

Added operationName key to dynamically add query, mutation, or subscription name to data object or anonymous in unnamed using regex matchers to extract the correct name from query

also set up UI to make the data key/value editable to user/opt-in. Users can configure a different keyname if needed

not sure if there is already some way built-in to accomplish this, but wanted to setup at least for my company and maybe if generalizable could be useful for others

Added operationName key to dynamically add query, mutation, or subscription name to data object or anonymous in unnamed

next is to make the key editable to user/opt-in
@@ -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

@@ -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

@SammoMichael
Copy link
Author

Screen Shot 2022-02-11 at 1 22 20 AM

Everything should be working as intended, let me know what you think if you get a chance, thanks! @vepanimas

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

Choose a reason for hiding this comment

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

if setting is enabled, we check the keyname user wants in graphql settings or default to "operationName" then use default operation name value "IntrospectionQuery" since that would be appropriate value in most cases
{"operationName": "IntrospectionQuery", "query": {}}

Copy link
Author

Choose a reason for hiding this comment

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

Can also make this dynamic like the normal request if there is a use case, can't think of one unless the graphql server is configured to only accept Introspection Query with a specific operation name

@@ -371,6 +369,22 @@ public void run(@NotNull ProgressIndicator indicator) {
}
}

private void handleOperationName(GraphQLSettings graphQLSettings, GraphQLQueryContext context, Map<String, Object> requestData) {
Copy link
Author

Choose a reason for hiding this comment

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

extracted to helper method, which handles getting the configured keyname for the operation name, and putting the correct value (either query|mutation|subscription name or "anonymous")

@@ -390,7 +404,8 @@ private void runQuery(Editor editor, VirtualFile virtualFile, GraphQLQueryContex
sw.stop();
}

final boolean reformatJson = contentType != null && contentType.getValue() != null && contentType.getValue().startsWith("application/json");
final boolean reformatJson = contentType != null && contentType.getValue() != null && contentType.getValue().startsWith(
Copy link
Author

Choose a reason for hiding this comment

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

this was autoformatting change, can roll back

e.g. query myQuery(

by capturing first set of (alphanumeric/underscores) in-between keyword and first non-alphanumeric character
@SammoMichael
Copy link
Author

SammoMichael commented Feb 11, 2022

Another question I had was I am having trouble building my fork of the plugin for Android Studio, is there an easiest way to configure a build that will work with Android Studio ? I guess I need some more practice with plugin versioning, even having trouble with Intellij, keep getting hit with different incompatibility errors :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant