|
| 1 | +/* |
| 2 | + * Copyright (c) 2018-present, Jim Kynde Meyer |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +package com.intellij.lang.jsgraphql.ide.editor; |
| 9 | + |
| 10 | +import com.google.gson.Gson; |
| 11 | +import com.intellij.codeHighlighting.Pass; |
| 12 | +import com.intellij.codeInsight.daemon.LineMarkerInfo; |
| 13 | +import com.intellij.codeInsight.daemon.LineMarkerProvider; |
| 14 | +import com.intellij.icons.AllIcons; |
| 15 | +import com.intellij.json.psi.JsonObject; |
| 16 | +import com.intellij.json.psi.JsonProperty; |
| 17 | +import com.intellij.json.psi.JsonStringLiteral; |
| 18 | +import com.intellij.json.psi.JsonValue; |
| 19 | +import com.intellij.lang.jsgraphql.GraphQLSettings; |
| 20 | +import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.GraphQLConfigManager; |
| 21 | +import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.model.GraphQLConfigEndpoint; |
| 22 | +import com.intellij.lang.jsgraphql.ide.project.graphqlconfig.model.GraphQLConfigVariableAwareEndpoint; |
| 23 | +import com.intellij.lang.jsgraphql.v1.ide.project.JSGraphQLLanguageUIProjectService; |
| 24 | +import com.intellij.notification.Notification; |
| 25 | +import com.intellij.notification.NotificationType; |
| 26 | +import com.intellij.notification.Notifications; |
| 27 | +import com.intellij.openapi.application.ApplicationManager; |
| 28 | +import com.intellij.openapi.editor.markup.GutterIconRenderer; |
| 29 | +import com.intellij.openapi.progress.ProgressManager; |
| 30 | +import com.intellij.openapi.vfs.VirtualFile; |
| 31 | +import com.intellij.psi.PsiElement; |
| 32 | +import com.intellij.psi.util.PsiTreeUtil; |
| 33 | +import graphql.introspection.IntrospectionQuery; |
| 34 | +import org.apache.commons.httpclient.HttpClient; |
| 35 | +import org.apache.commons.httpclient.methods.PostMethod; |
| 36 | +import org.apache.commons.httpclient.methods.StringRequestEntity; |
| 37 | +import org.apache.commons.httpclient.params.HttpClientParams; |
| 38 | +import org.apache.commons.lang.StringEscapeUtils; |
| 39 | +import org.apache.commons.lang.StringUtils; |
| 40 | +import org.jetbrains.annotations.NotNull; |
| 41 | +import org.jetbrains.annotations.Nullable; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.io.UnsupportedEncodingException; |
| 45 | +import java.util.Collection; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
| 48 | +import java.util.Optional; |
| 49 | +import java.util.stream.Stream; |
| 50 | + |
| 51 | +import static com.intellij.lang.jsgraphql.ide.editor.GraphQLIntrospectionHelper.printIntrospectionJsonAsGraphQL; |
| 52 | +import static com.intellij.lang.jsgraphql.v1.ide.project.JSGraphQLLanguageUIProjectService.setHeadersFromOptions; |
| 53 | + |
| 54 | +/** |
| 55 | + * Line marker for running an introspection against a configured endpoint url in a .graphqlconfig file |
| 56 | + */ |
| 57 | +public class GraphQLIntrospectEndpointUrlLineMarkerProvider implements LineMarkerProvider { |
| 58 | + @Nullable |
| 59 | + @Override |
| 60 | + public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) { |
| 61 | + if (!GraphQLConfigManager.GRAPHQLCONFIG.equals(element.getContainingFile().getName())) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + if (element instanceof JsonProperty) { |
| 65 | + final JsonProperty jsonProperty = (JsonProperty) element; |
| 66 | + if ("url".equals(jsonProperty.getName()) && jsonProperty.getValue() instanceof JsonStringLiteral) { |
| 67 | + |
| 68 | + return new LineMarkerInfo<>((JsonStringLiteral) jsonProperty.getValue(), jsonProperty.getValue().getTextRange(), AllIcons.General.Run, Pass.UPDATE_ALL, o -> "Run introspection query to generate GraphQL SDL schema file", (evt, jsonUrl) -> { |
| 69 | + |
| 70 | + final String url = jsonUrl.getValue(); |
| 71 | + |
| 72 | + final GraphQLConfigVariableAwareEndpoint endpoint = getEndpoint(url, jsonProperty); |
| 73 | + if (endpoint == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + String schemaPath = getSchemaPath(jsonProperty); |
| 78 | + if (schemaPath == null || schemaPath.trim().isEmpty()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + final HttpClient httpClient = new HttpClient(new HttpClientParams()); |
| 83 | + |
| 84 | + try { |
| 85 | + |
| 86 | + String query = GraphQLSettings.getSettings(element.getProject()).getIntrospectionQuery(); |
| 87 | + if (StringUtils.isBlank(query)) { |
| 88 | + query = IntrospectionQuery.INTROSPECTION_QUERY; |
| 89 | + } |
| 90 | + |
| 91 | + final String requestJson = "{\"query\":\"" + StringEscapeUtils.escapeJavaScript(query) + "\"}"; |
| 92 | + |
| 93 | + final PostMethod method = new PostMethod(endpoint.getUrl()); |
| 94 | + method.setRequestEntity(new StringRequestEntity(requestJson, "application/json", "UTF-8")); |
| 95 | + |
| 96 | + setHeadersFromOptions(endpoint, method); |
| 97 | + |
| 98 | + ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> { |
| 99 | + ProgressManager.getInstance().getProgressIndicator().setIndeterminate(true); |
| 100 | + try { |
| 101 | + httpClient.executeMethod(method); |
| 102 | + final String responseJson = Optional.ofNullable(method.getResponseBodyAsString()).orElse(""); |
| 103 | + ApplicationManager.getApplication().invokeLater(() -> { |
| 104 | + try { |
| 105 | + JSGraphQLLanguageUIProjectService.getService(jsonProperty.getProject()).showQueryResult(responseJson); |
| 106 | + final String schemaAsSDL = printIntrospectionJsonAsGraphQL(responseJson); |
| 107 | + VirtualFile virtualFile = element.getContainingFile().getVirtualFile(); |
| 108 | + GraphQLIntrospectionHelper.createOrUpdateIntrospectionSDLFile(schemaAsSDL, virtualFile, schemaPath, element.getProject()); |
| 109 | + } catch (Exception e) { |
| 110 | + Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Introspection Error", e.getMessage(), NotificationType.WARNING), element.getProject()); |
| 111 | + } |
| 112 | + }); |
| 113 | + } catch (IOException e) { |
| 114 | + Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Query Error", url + ": " + e.getMessage(), NotificationType.WARNING), element.getProject()); |
| 115 | + } |
| 116 | + |
| 117 | + }, "Executing GraphQL Introspection Query", false, jsonProperty.getProject()); |
| 118 | + |
| 119 | + |
| 120 | + } catch (UnsupportedEncodingException | IllegalStateException | IllegalArgumentException e) { |
| 121 | + Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Query Error", url + ": " + e.getMessage(), NotificationType.ERROR), element.getProject()); |
| 122 | + } |
| 123 | + |
| 124 | + }, GutterIconRenderer.Alignment.CENTER); |
| 125 | + } |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + private String getSchemaPath(JsonProperty urlElement) { |
| 131 | + JsonObject jsonObject = PsiTreeUtil.getParentOfType(urlElement, JsonObject.class); |
| 132 | + String url = urlElement.getValue() instanceof JsonStringLiteral ? ((JsonStringLiteral) urlElement.getValue()).getValue() : ""; |
| 133 | + while (jsonObject != null) { |
| 134 | + JsonProperty schemaPathElement = jsonObject.findProperty("schemaPath"); |
| 135 | + if (schemaPathElement != null) { |
| 136 | + if (schemaPathElement.getValue() instanceof JsonStringLiteral) { |
| 137 | + String schemaPath = ((JsonStringLiteral) schemaPathElement.getValue()).getValue(); |
| 138 | + if (schemaPath.trim().isEmpty()) { |
| 139 | + Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Configuration Error", "The schemaPath must be defined for url " + url, NotificationType.ERROR), urlElement.getProject()); |
| 140 | + } |
| 141 | + return schemaPath; |
| 142 | + } else { |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + jsonObject = PsiTreeUtil.getParentOfType(jsonObject, JsonObject.class); |
| 147 | + } |
| 148 | + Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Configuration Error", "No schemaPath found for url " + url, NotificationType.ERROR), urlElement.getProject()); |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + private GraphQLConfigVariableAwareEndpoint getEndpoint(String url, JsonProperty urlJsonProperty) { |
| 153 | + try { |
| 154 | + |
| 155 | + final GraphQLConfigEndpoint endpointConfig = new GraphQLConfigEndpoint("", "", url); |
| 156 | + |
| 157 | + final Stream<JsonProperty> jsonPropertyStream = PsiTreeUtil.getChildrenOfTypeAsList(urlJsonProperty.getParent(), JsonProperty.class).stream(); |
| 158 | + final Optional<JsonProperty> headers = jsonPropertyStream.filter(p -> "headers".equals(p.getName())).findFirst(); |
| 159 | + headers.ifPresent(headersProp -> { |
| 160 | + final JsonValue jsonValue = headersProp.getValue(); |
| 161 | + if (jsonValue != null) { |
| 162 | + endpointConfig.headers = new Gson().<Map<String, Object>>fromJson(jsonValue.getText(), Map.class); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + return new GraphQLConfigVariableAwareEndpoint(endpointConfig, urlJsonProperty.getProject()); |
| 167 | + |
| 168 | + } catch (Exception e) { |
| 169 | + Notifications.Bus.notify(new Notification("GraphQL", "GraphQL Configuration Error", e.getMessage(), NotificationType.ERROR), urlJsonProperty.getProject()); |
| 170 | + } |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) { |
| 176 | + |
| 177 | + } |
| 178 | +} |
0 commit comments