|
14 | 14 | import com.intellij.json.psi.JsonObject;
|
15 | 15 | import com.intellij.json.psi.JsonProperty;
|
16 | 16 | import com.intellij.lang.jsgraphql.GraphQLBundle;
|
17 |
| -import com.intellij.lang.jsgraphql.GraphQLSettings; |
18 | 17 | import com.intellij.lang.jsgraphql.ide.notifications.GraphQLNotificationUtil;
|
19 | 18 | import com.intellij.notification.Notification;
|
20 | 19 | import com.intellij.notification.NotificationType;
|
21 | 20 | import com.intellij.notification.Notifications;
|
22 | 21 | import com.intellij.openapi.editor.markup.GutterIconRenderer;
|
23 |
| -import com.intellij.openapi.progress.ProcessCanceledException; |
24 | 22 | import com.intellij.openapi.project.Project;
|
25 | 23 | import com.intellij.openapi.util.Ref;
|
26 | 24 | import com.intellij.openapi.vfs.VirtualFile;
|
|
29 | 27 | import org.jetbrains.annotations.NotNull;
|
30 | 28 | import org.jetbrains.annotations.Nullable;
|
31 | 29 |
|
| 30 | +import java.util.concurrent.CancellationException; |
| 31 | + |
32 | 32 | /**
|
33 | 33 | * Line marker which shows an action to turn a GraphQL Introspection JSON result into a GraphQL schema expressed in GraphQL SDL.
|
34 | 34 | */
|
35 | 35 | public final class GraphQLIntrospectionJsonToSDLLineMarkerProvider implements LineMarkerProvider {
|
36 | 36 | @Override
|
37 | 37 | public @Nullable LineMarkerInfo<?> getLineMarkerInfo(@NotNull PsiElement element) {
|
38 |
| - final VirtualFile virtualFile = element.isValid() ? element.getContainingFile().getVirtualFile() : null; |
| 38 | + VirtualFile virtualFile = element.isValid() ? element.getContainingFile().getVirtualFile() : null; |
39 | 39 | if (virtualFile != null && !virtualFile.isInLocalFileSystem()) {
|
40 | 40 | // skip in-memory JSON files such as the query result viewer
|
41 | 41 | return null;
|
42 | 42 | }
|
43 | 43 | if (!(element instanceof JsonProperty jsonProperty)) {
|
44 | 44 | return null;
|
45 | 45 | }
|
46 |
| - final Project project = element.getProject(); |
47 |
| - final JsonProperty parentProperty = PsiTreeUtil.getParentOfType(element, JsonProperty.class); |
| 46 | + Project project = element.getProject(); |
| 47 | + JsonProperty parentProperty = PsiTreeUtil.getParentOfType(element, JsonProperty.class); |
48 | 48 | if (parentProperty != null && !"data".equals(parentProperty.getName())) {
|
49 | 49 | return null;
|
50 | 50 | }
|
51 | 51 |
|
52 | 52 | // top level property or inside data property
|
53 |
| - final String propertyName = jsonProperty.getName(); |
| 53 | + String propertyName = jsonProperty.getName(); |
54 | 54 | if (!"__schema".equals(propertyName) || !(jsonProperty.getValue() instanceof JsonObject)) {
|
55 | 55 | return null;
|
56 | 56 | }
|
57 | 57 |
|
58 | 58 | for (JsonProperty property : ((JsonObject)jsonProperty.getValue()).getPropertyList()) {
|
59 | 59 | if ("types".equals(property.getName()) && property.getValue() instanceof JsonArray) {
|
60 | 60 | // likely a GraphQL schema with a { __schema: { types: [] } }
|
61 |
| - final GraphQLIntrospectionService introspectionService = GraphQLIntrospectionService.getInstance(project); |
62 |
| - final Ref<Runnable> generateAction = Ref.create(); |
| 61 | + Ref<Runnable> generateAction = Ref.create(); |
63 | 62 | generateAction.set(() -> {
|
64 | 63 | try {
|
65 |
| - final String introspectionJson = element.getContainingFile().getText(); |
66 |
| - final String schemaAsSDL = introspectionService.printIntrospectionAsGraphQL(introspectionJson); |
| 64 | + String introspectionJson = element.getContainingFile().getText(); |
| 65 | + String schemaAsSDL = GraphQLIntrospectionService.printIntrospectionAsGraphQL(project, introspectionJson); |
67 | 66 |
|
68 |
| - final VirtualFile jsonFile = element.getContainingFile().getVirtualFile(); |
69 |
| - final String outputFileName = jsonFile.getName() + ".graphql"; |
| 67 | + VirtualFile jsonFile = element.getContainingFile().getVirtualFile(); |
| 68 | + String outputFileName = jsonFile.getNameWithoutExtension() + ".graphql"; |
70 | 69 |
|
71 |
| - introspectionService.createOrUpdateIntrospectionOutputFile( |
72 |
| - schemaAsSDL, |
73 |
| - GraphQLIntrospectionService.IntrospectionOutputFormat.SDL, |
| 70 | + GraphQLIntrospectionService.createOrUpdateIntrospectionOutputFile( |
| 71 | + project, |
| 72 | + new GraphQLIntrospectionService.IntrospectionOutput(schemaAsSDL, GraphQLIntrospectionService.IntrospectionOutputFormat.SDL), |
74 | 73 | outputFileName,
|
75 | 74 | jsonFile.getParent()
|
76 | 75 | );
|
77 | 76 | }
|
78 |
| - catch (ProcessCanceledException e) { |
| 77 | + catch (CancellationException e) { |
79 | 78 | throw e;
|
80 | 79 | }
|
81 | 80 | catch (Exception e) {
|
82 | 81 | Notification notification = new Notification(
|
83 | 82 | GraphQLNotificationUtil.GRAPHQL_NOTIFICATION_GROUP_ID,
|
84 | 83 | GraphQLBundle.message("graphql.notification.introspection.error.title"),
|
85 |
| - GraphQLNotificationUtil.formatExceptionMessage(e), |
| 84 | + GraphQLBundle.message("graphql.notification.introspection.error.body"), |
86 | 85 | NotificationType.ERROR
|
87 |
| - ); |
| 86 | + ).setImportant(true); |
88 | 87 |
|
89 |
| - GraphQLNotificationUtil.addRetryFailedSchemaIntrospectionAction( |
90 |
| - notification, GraphQLSettings.getSettings(project), e, generateAction.get()); |
91 |
| - introspectionService.addIntrospectionStackTraceAction(notification, e); |
92 |
| - Notifications.Bus.notify(notification.setImportant(true)); |
| 88 | + GraphQLNotificationUtil.addRetryQueryForPossiblyInvalidIntrospectionSchemaAction(project, notification, e, generateAction.get()); |
| 89 | + GraphQLNotificationUtil.addShowQueryErrorDetailsAction(project, notification, e); |
| 90 | + Notifications.Bus.notify(notification); |
93 | 91 | }
|
94 | 92 | });
|
95 | 93 |
|
|
0 commit comments