Skip to content

operationName for Apollo Federation Gateway #595 #596

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions src/main/com/intellij/lang/jsgraphql/GraphQLSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,20 @@ public String getIntrospectionQuery() {
return myState.introspectionQuery;
}

public String getOperationName() {
return myState.operationName;
}

public void setIntrospectionQuery(String introspectionQuery) {
myState.introspectionQuery = introspectionQuery;
settingsChanged();
}

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

public boolean isEnableIntrospectionDefaultValues() {
return myState.enableIntrospectionDefaultValues;
}
Expand Down Expand Up @@ -114,18 +123,29 @@ public void setFederationSupportEnabled(boolean enableFederationSupport) {
settingsChanged();
}

public boolean isOperationNameEnabled() {
return myState.enableOperationName;
}

public void setOperationNameEnabled(boolean enableOperationName) {
myState.enableOperationName = enableOperationName;
settingsChanged();
}

/**
* The state class that is persisted as XML
* NOTE!!!: 1. Class must be static, and 2. Fields must be public for settings serialization to work
*/
public static class GraphQLSettingsState {
public String introspectionQuery = "";
public String operationName = "";
public boolean enableIntrospectionDefaultValues = true;
public boolean enableIntrospectionRepeatableDirectives = false;
public boolean openEditorWithIntrospectionResult = true;

public boolean enableRelayModernFrameworkSupport;
public boolean enableFederationSupport = false;
public boolean enableOperationName = false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,17 @@ public void actionPerformed(@NotNull AnActionEvent e, @NotNull Notification noti
try {
final GraphQLSettings graphQLSettings = GraphQLSettings.getSettings(myProject);
String query = buildIntrospectionQuery(graphQLSettings);

final String requestJson = "{\"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

String operationName = graphQLSettings.getOperationName().isEmpty() ? "operationName" : graphQLSettings.getOperationName();
final String requestJson = "{" + "\"" + operationName + "\"" + ": \"IntrospectionQuery\", \"query\":\"" + StringEscapeUtils.escapeJavaScript(
query) + "\"}";
request = createRequest(endpoint, url, requestJson);
} else {
final String requestJson = "{\"query\":\"" + StringEscapeUtils.escapeJavaScript(
query) + "\"}";
request = createRequest(endpoint, url, requestJson);
}
Task.Backgroundable task = new IntrospectionQueryTask(request, schemaPath, introspectionSourceFile, retry, graphQLSettings, endpoint, url);
ProgressManager.getInstance().run(task);
} catch (IllegalStateException | IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.intellij.json.JsonFileType;
import com.intellij.lang.jsgraphql.GraphQLFileType;
import com.intellij.lang.jsgraphql.GraphQLParserDefinition;
import com.intellij.lang.jsgraphql.GraphQLSettings;
import com.intellij.lang.jsgraphql.ide.actions.GraphQLEditConfigAction;
import com.intellij.lang.jsgraphql.ide.actions.GraphQLExecuteEditorAction;
import com.intellij.lang.jsgraphql.ide.actions.GraphQLToggleVariablesAction;
Expand Down Expand Up @@ -80,6 +81,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 @@ -303,15 +306,21 @@ private JComponent createToolbar(ActionGroup actionGroup, JComponent parent) {
}

public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
final GraphQLSettings graphQLSettings = GraphQLSettings.getSettings(myProject);
final GraphQLEndpointsModel endpointsModel = editor.getUserData(GRAPH_QL_ENDPOINTS_MODEL);
if (endpointsModel != null) {
final GraphQLConfigEndpoint selectedEndpoint = endpointsModel.getSelectedItem();
if (selectedEndpoint != null && selectedEndpoint.url != null) {
final GraphQLConfigVariableAwareEndpoint endpoint = new GraphQLConfigVariableAwareEndpoint(selectedEndpoint, myProject, virtualFile);
final GraphQLConfigVariableAwareEndpoint endpoint = new GraphQLConfigVariableAwareEndpoint(selectedEndpoint, myProject,
virtualFile);
final GraphQLQueryContext context = GraphQLQueryContextHighlightVisitor.getQueryContextBufferAndHighlightUnused(editor);

Map<String, Object> requestData = new HashMap<>();
requestData.put("query", context.query);
if (graphQLSettings.isOperationNameEnabled()) {
handleOperationName(graphQLSettings, context, requestData);
} else {
requestData.put("query", context.query);
}
try {
requestData.put("variables", getQueryVariables(editor));
} catch (JsonSyntaxException jse) {
Expand All @@ -321,7 +330,8 @@ public void executeGraphQL(Editor editor, VirtualFile virtualFile) {
errorEditor.getContentComponent().grabFocus();
final VirtualFile errorFile = FileDocumentManager.getInstance().getFile(errorEditor.getDocument());
if (errorFile != null) {
final List<CodeSmellInfo> errors = CodeSmellDetector.getInstance(myProject).findCodeSmells(Collections.singletonList(errorFile));
final List<CodeSmellInfo> errors = CodeSmellDetector.getInstance(myProject).findCodeSmells(
Collections.singletonList(errorFile));
for (CodeSmellInfo error : errors) {
errorMessage = error.getDescription();
errorEditor.getCaretModel().moveToOffset(error.getTextRange().getStartOffset());
Expand Down Expand Up @@ -359,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")

String operationName = graphQLSettings.getOperationName().isEmpty() ? "operationName" : graphQLSettings.getOperationName();
Pattern pattern = Pattern.compile("(query|mutation|subscription)\\s(\\w+)", Pattern.CASE_INSENSITIVE);
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]);
}
}

private void runQuery(Editor editor, VirtualFile virtualFile, GraphQLQueryContext context, String url, HttpPost request) {
GraphQLIntrospectionService introspectionService = GraphQLIntrospectionService.getInstance(myProject);
try {
Expand All @@ -378,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

"application/json");
final Integer errorCount = getErrorCount(responseJson);
ApplicationManager.getApplication().invokeLater(() -> {
TextEditor queryResultEditor = GraphQLToolWindow.getQueryResultEditor(myProject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<grid id="27dc6" binding="rootPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="707" height="400"/>
<xy x="20" y="20" width="864" height="400"/>
</constraints>
<properties/>
<border type="none"/>
Expand All @@ -13,7 +13,7 @@
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="5f9ac" binding="introspectionPanel" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="5f9ac" binding="introspectionPanel" layout-manager="GridLayoutManager" row-count="6" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
Expand All @@ -23,7 +23,7 @@
<children>
<component id="f15eb" class="com.intellij.ui.components.fields.ExpandableTextField" binding="introspectionQueryTextField">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="0" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
Expand All @@ -33,15 +33,15 @@
</component>
<component id="f9589" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/GraphQLMessages" key="graphql.settings.introspection.query.label"/>
</properties>
</component>
<component id="86c0c" class="javax.swing.JCheckBox" binding="enableIntrospectionDefaultValues">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<enabled value="true"/>
Expand All @@ -51,21 +51,48 @@
</component>
<component id="880f9" class="javax.swing.JCheckBox" binding="openEditorWithIntrospectionResult">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/GraphQLMessages" key="graphql.settings.introspection.open.editor.label"/>
</properties>
</component>
<component id="f9e30" class="javax.swing.JCheckBox" binding="enableIntrospectionRepeatableDirectives">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="4" column="0" row-span="1" col-span="4" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/GraphQLMessages" key="graphql.settings.introspection.repeatable.directives.label"/>
<toolTipText resource-bundle="messages/GraphQLMessages" key="graphql.settings.introspection.repeatable.directives.tooltip"/>
</properties>
</component>
<component id="5c908" class="javax.swing.JCheckBox" binding="enableOperationName">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Include operation name in requests"/>
<toolTipText value="e.g. { operationName: MyQuery }"/>
</properties>
</component>
<component id="95028" class="javax.swing.JLabel" binding="operationNameLabel">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="keyname (leave blank to use operationName)"/>
</properties>
</component>
<component id="ee1f0" class="javax.swing.JTextField" binding="operationNameTextField">
<constraints>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value=""/>
</properties>
</component>
</children>
</grid>
<grid id="ed16d" binding="frameworksPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class GraphQLProjectSettingsForm {
private JCheckBox enableFederation;
private JCheckBox openEditorWithIntrospectionResult;
private JCheckBox enableIntrospectionRepeatableDirectives;
private JTextField operationNameTextField;
private JLabel operationNameLabel;
private JCheckBox enableOperationName;

private GraphQLSettings mySettings;

Expand Down Expand Up @@ -60,16 +63,20 @@ void apply() {

mySettings.setRelaySupportEnabled(enableRelay.isSelected());
mySettings.setFederationSupportEnabled(enableFederation.isSelected());
mySettings.setOperationNameEnabled(enableOperationName.isSelected());
mySettings.setOperationName(operationNameTextField.getText());
}

void reset() {
introspectionQueryTextField.setText(mySettings.getIntrospectionQuery());
operationNameTextField.setText(mySettings.getOperationName());
enableIntrospectionDefaultValues.setSelected(mySettings.isEnableIntrospectionDefaultValues());
enableIntrospectionRepeatableDirectives.setSelected(mySettings.isEnableIntrospectionRepeatableDirectives());
openEditorWithIntrospectionResult.setSelected(mySettings.isOpenEditorWithIntrospectionResult());

enableRelay.setSelected(mySettings.isRelaySupportEnabled());
enableFederation.setSelected(mySettings.isFederationSupportEnabled());
enableOperationName.setSelected(mySettings.isOperationNameEnabled());
}

boolean isModified() {
Expand All @@ -78,9 +85,11 @@ boolean isModified() {

boolean introspectionSettingsChanged() {
return !Objects.equals(mySettings.getIntrospectionQuery(), introspectionQueryTextField.getText()) ||
!Objects.equals(mySettings.getOperationName(), operationNameTextField.getText()) ||
mySettings.isEnableIntrospectionDefaultValues() != enableIntrospectionDefaultValues.isSelected() ||
mySettings.isEnableIntrospectionRepeatableDirectives() != enableIntrospectionRepeatableDirectives.isSelected() ||
mySettings.isOpenEditorWithIntrospectionResult() != openEditorWithIntrospectionResult.isSelected();
mySettings.isOpenEditorWithIntrospectionResult() != openEditorWithIntrospectionResult.isSelected() ||
mySettings.isOperationNameEnabled() != enableOperationName.isSelected();
}

boolean librarySettingsChanged() {
Expand Down