| 50 | /// generated impls call [#execute] / [#subscribe] and never touch |
| 51 | /// `ConnectionRequest` directly. |
| 52 | public final class GraphQL { |
| 53 | |
| 54 | /// Content type for GraphQL-over-HTTP requests. |
| 55 | public static final String CONTENT_TYPE = "application/json"; |
| 56 | |
| 57 | private GraphQL() { |
| 58 | } |
| 59 | |
| 60 | /// Sends a unary query or mutation and invokes `callback` with the |
| 61 | /// decoded [GraphQLResponse]. `variables` may be null or empty. |
| 62 | /// The `operationName` may be null when the document declares a |
| 63 | /// single operation. |
| 64 | public static <T> void execute( |
| 65 | String endpoint, |
| 66 | String bearerToken, |
| 67 | String operationName, |
| 68 | String document, |
| 69 | Map<String, Object> variables, |
| 70 | Class<T> dataType, |
| 71 | final OnComplete<GraphQLResponse<T>> callback) { |
| 72 | if (callback == null) { |
| 73 | throw new IllegalArgumentException("callback must not be null"); |
| 74 | } |
| 75 | String body = buildRequestBody(operationName, document, variables); |
| 76 | |
| 77 | GraphQLConnection<T> conn = new GraphQLConnection<T>(dataType, callback); |
| 78 | conn.setUrl(endpoint); |
| 79 | conn.setHttpMethod("POST"); |
| 80 | conn.setPost(true); |
| 81 | conn.setContentType(CONTENT_TYPE); |
| 82 | conn.addRequestHeader("Accept", CONTENT_TYPE); |
| 83 | if (bearerToken != null && bearerToken.length() > 0) { |
| 84 | conn.addRequestHeader("Authorization", bearerToken); |
| 85 | } |
| 86 | conn.setRequestBody(new Data.StringData(body)); // StringData defaults to UTF-8 |
| 87 | CN.addToQueue(conn); |
| 88 | } |
| 89 | |
| 90 | /// Opens a GraphQL subscription over a WebSocket and streams mapped |
| 91 | /// `data` payloads to `handler`. Returns a handle whose |
| 92 | /// [GraphQLSubscription#cancel()] tears the subscription down. |
| 93 | /// |
| 94 | /// The `endpoint` is the GraphQL HTTP endpoint; its scheme is |
| 95 | /// rewritten to `ws`/`wss` for the WebSocket connection. Pass a |
| 96 | /// `ws://`/`wss://` URL directly to override that heuristic. |
| 97 | public static <T> GraphQLSubscription subscribe( |
| 98 | String endpoint, |
| 99 | String bearerToken, |
| 100 | String operationName, |
| 101 | String document, |
| 102 | Map<String, Object> variables, |
| 103 | Class<T> dataType, |
| 104 | GraphQLSubscription.Handler<T> handler) { |
| 105 | return GraphQLSubscription.start( |
| 106 | toWebSocketUrl(endpoint), bearerToken, operationName, |
| 107 | document, variables, dataType, handler); |
| 108 | } |
| 109 |
nothing calls this directly
no outgoing calls
no test coverage detected