-
Notifications
You must be signed in to change notification settings - Fork 95
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 { | ||
|
@@ -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) { | ||
|
@@ -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()); | ||
|
@@ -359,6 +369,22 @@ public void run(@NotNull ProgressIndicator indicator) { | |
} | ||
} | ||
|
||
private void handleOperationName(GraphQLSettings graphQLSettings, GraphQLQueryContext context, Map<String, Object> requestData) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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": {}}
There was a problem hiding this comment.
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