Skip to content

Commit 6f4a655

Browse files
authored
Merge pull request #381 from jimkyndemeyer/rework-introspection-errors
Reworked introspection query handling
2 parents 4e44695 + e9cec66 commit 6f4a655

File tree

7 files changed

+235
-181
lines changed

7 files changed

+235
-181
lines changed

resources/messages/GraphQLMessages.properties

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# Notifications
2-
graphql.notification.introspection.error.title=GraphQL introspection error
2+
graphql.notification.introspection.error.title=GraphQL introspection
33
graphql.notification.introspection.error.body=A valid schema could not be built using the introspection result.<br/>Error: {0}
44
graphql.notification.introspection.spec.error.body=A valid schema could not be built using the introspection result. The endpoint may not follow the GraphQL Specification.<br/>Error: {0}
5+
graphql.notification.introspection.parse.error=The server introspection response cannot be parsed as a valid JSON object.
6+
graphql.notification.introspection.empty.errors=Encountered empty error array, which does not conform to the GraphQL spec.
57
graphql.notification.error.title=GraphQL error
6-
graphql.notification.response=Response
78
graphql.notification.stack.trace=Stack trace
89
graphql.notification.retry.without.defaults=Retry (skip default values from now on)
910
graphql.notification.retry=Retry
10-
graphql.notification.unable.to.open.editor=Unable to open an editor for '{0}'
11-
graphql.notification.unable.to.create.file=Unable to create file '{0}' in directory '{1}'.<br/>Error: {2}
11+
graphql.notification.unable.to.open.editor=Unable to open an editor for ''{0}''
12+
graphql.notification.unable.to.create.file=Unable to create file ''{0}'' in directory ''{1}''.<br/>Error: {2}
1213
graphql.notification.invalid.config.file=Invalid config file
1314
graphql.notification.empty.schema.path=Please set a non-empty `schemaPath` field in the config file.
1415
graphql.notification.empty.endpoint.url=Please set a non-empty endpoint `url` field in the config file.
1516
graphql.notification.unable.to.parse.file=Unable to parse {0}
1617
graphql.notification.load.schema.from.endpoint.title=Get GraphQL schema from endpoint now?
17-
graphql.notification.load.schema.from.endpoint.body=Introspect '{0}' to update the local schema file.
18-
graphql.notification.load.schema.from.endpoint.action=Introspect '{0}'
18+
graphql.notification.load.schema.from.endpoint.body=Introspect ''{0}'' to update the local schema file.
19+
graphql.notification.load.schema.from.endpoint.action=Introspect ''{0}''
1920

2021
# Introspection
2122
graphql.introspection.missing.data=Expected `data` key to be present in query result.

src/main/com/intellij/lang/jsgraphql/ide/editor/GraphQLIntrospectionJsonToSDLLineMarkerProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
6565
generateAction.set(() -> {
6666
try {
6767
final String introspectionJson = element.getContainingFile().getText();
68-
final String schemaAsSDL = graphQLIntrospectionService.printIntrospectionJsonAsGraphQL(introspectionJson);
68+
final String schemaAsSDL = graphQLIntrospectionService.printIntrospectionAsGraphQL(introspectionJson);
6969

7070
final VirtualFile jsonFile = element.getContainingFile().getVirtualFile();
7171
final String outputFileName = jsonFile.getName() + ".graphql";

src/main/com/intellij/lang/jsgraphql/ide/editor/GraphQLIntrospectionService.java

Lines changed: 199 additions & 138 deletions
Large diffs are not rendered by default.

src/main/com/intellij/lang/jsgraphql/ide/notifications/GraphQLNotificationUtil.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@ public void actionPerformed(@NotNull AnActionEvent e, @NotNull Notification noti
4242
Notifications.Bus.notify(notification);
4343
}
4444

45-
public static void showRequestExceptionNotification(@NotNull NotificationAction retry,
46-
@NotNull String url,
47-
@NotNull String error,
48-
@NotNull NotificationType notificationType,
49-
@NotNull Project project) {
50-
Notifications.Bus.notify(
51-
new Notification(
52-
NOTIFICATION_GROUP_ID,
53-
GraphQLBundle.message("graphql.notification.error.title"),
54-
url + ": " + error,
55-
notificationType
56-
).addAction(retry),
57-
project
58-
);
45+
public static void showGraphQLRequestErrorNotification(@NotNull NotificationAction retry,
46+
@NotNull String url,
47+
@NotNull Exception error,
48+
@NotNull NotificationType notificationType,
49+
@NotNull Project project) {
50+
Notification notification = new Notification(
51+
NOTIFICATION_GROUP_ID,
52+
GraphQLBundle.message("graphql.notification.error.title"),
53+
url + ": " + GraphQLNotificationUtil.formatExceptionMessage(error),
54+
notificationType
55+
).addAction(retry);
56+
57+
Notifications.Bus.notify(notification, project);
5958
}
6059

6160
public static void addRetryFailedSchemaIntrospectionAction(@NotNull Notification notification,

src/main/com/intellij/lang/jsgraphql/schema/GraphQLRegistryProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ private GraphQLValidatedTypeDefinitionRegistry getRegistry(@NotNull PsiElement s
162162
if (psiFile != null) {
163163
try {
164164
synchronized (GRAPHQL_INTROSPECTION_JSON_TO_SDL) {
165-
final String introspectionJsonAsGraphQL = GraphQLIntrospectionService.getInstance(project).printIntrospectionJsonAsGraphQL(psiFile.getText());
165+
final String introspectionJsonAsGraphQL = GraphQLIntrospectionService.getInstance(project).printIntrospectionAsGraphQL(psiFile.getText());
166166
final GraphQLFile currentSDLPsiFile = psiFile.getUserData(GRAPHQL_INTROSPECTION_JSON_TO_SDL);
167167
if (currentSDLPsiFile != null && currentSDLPsiFile.getText().equals(introspectionJsonAsGraphQL)) {
168168
// already have a PSI file that matches the introspection SDL

src/main/com/intellij/lang/jsgraphql/v1/ide/project/JSGraphQLLanguageUIProjectService.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -473,24 +473,16 @@ private static Gson createQueryJsonSerializer() {
473473
.create();
474474
}
475475

476-
public enum QueryResultDisplay {
477-
ALWAYS,
478-
ON_ERRORS_ONLY
479-
}
480-
481-
public void showQueryResult(String jsonResponse, QueryResultDisplay display) {
482-
if (fileEditor instanceof TextEditor) {
483-
final TextEditor fileEditor = (TextEditor) this.fileEditor;
484-
updateQueryResultEditor(jsonResponse, fileEditor, true);
485-
if (display == QueryResultDisplay.ON_ERRORS_ONLY) {
486-
final Integer errorCount = getErrorCount(jsonResponse);
487-
if (errorCount == null || errorCount > 0) {
488-
showQueryResultEditor(fileEditor);
489-
}
490-
} else {
491-
showQueryResultEditor(fileEditor);
492-
}
476+
public void showQueryResult(@NotNull String jsonResponse) {
477+
if (!(fileEditor instanceof TextEditor)) {
478+
return;
493479
}
480+
481+
ApplicationManager.getApplication().invokeLater(() -> {
482+
TextEditor textEditor = (TextEditor) fileEditor;
483+
updateQueryResultEditor(jsonResponse, textEditor, true);
484+
showQueryResultEditor(textEditor);
485+
});
494486
}
495487

496488
private void showQueryResultEditor(TextEditor textEditor) {

src/test/com/intellij/lang/jsgraphql/ide/editor/GraphQLIntrospectionServiceTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.intellij.lang.jsgraphql.ide.editor;
22

3-
import com.intellij.openapi.vfs.VfsUtil;
3+
import com.intellij.openapi.vfs.VfsUtilCore;
44
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Nullable;
77
import org.junit.Assert;
88

99
import java.io.IOException;
10+
import java.util.Objects;
1011

1112
public class GraphQLIntrospectionServiceTest extends BasePlatformTestCase {
1213

@@ -40,16 +41,16 @@ public void testPrintIntrospectionWithNullFields() {
4041

4142
private void doTest(@NotNull String source, @NotNull String expected) {
4243
myFixture.configureByText(
43-
"result.graphql",
44-
new GraphQLIntrospectionService(getProject()).printIntrospectionJsonAsGraphQL(readSchemaJson(source))
44+
"result.graphql",
45+
new GraphQLIntrospectionService(getProject()).printIntrospectionAsGraphQL(Objects.requireNonNull(readSchemaJson(source)))
4546
);
4647
myFixture.checkResultByFile(expected);
4748
}
4849

4950
@Nullable
5051
private String readSchemaJson(@NotNull String path) {
5152
try {
52-
return VfsUtil.loadText(myFixture.copyFileToProject(path));
53+
return VfsUtilCore.loadText(myFixture.copyFileToProject(path));
5354
} catch (IOException e) {
5455
return null;
5556
}

0 commit comments

Comments
 (0)