UNPKG

4.92 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const apollo_cache_inmemory_1 = require("apollo-cache-inmemory");
4const apollo_client_1 = require("apollo-client");
5const apollo_link_1 = require("apollo-link");
6const apollo_link_http_1 = require("apollo-link-http");
7const axios_1 = require("axios");
8const axios_fetch_1 = require("axios-fetch");
9const graphql_tag_1 = require("graphql-tag");
10const stringify = require("json-stringify-safe");
11const trace = require("stack-trace");
12const internalGraphql = require("../internal/graph/graphQL");
13const namespace = require("../internal/util/cls");
14const http_1 = require("../internal/util/http");
15const logger_1 = require("../internal/util/logger");
16/**
17 * Implementation of GraphClient using Apollo Client.
18 */
19class ApolloGraphClient {
20 /**
21 * Create a new GraphClient
22 * @param endpoint GraphQL endpoint
23 * @param headers any special headers to use
24 */
25 constructor(endpoint, headers = {}) {
26 this.endpoint = endpoint;
27 const cache = new apollo_cache_inmemory_1.InMemoryCache({
28 addTypename: false,
29 });
30 const httpLink = apollo_link_http_1.createHttpLink({
31 uri: endpoint,
32 fetch: axios_fetch_1.buildAxiosFetch(axios_1.default.create(http_1.configureProxy({}))),
33 });
34 const middlewareLink = new apollo_link_1.ApolloLink((operation, forward) => {
35 // attach the correlation-id to the request
36 const correlationId = namespace.get() ? namespace.get().correlationId : undefined;
37 if (correlationId) {
38 operation.setContext({
39 headers: Object.assign({}, headers, { "correlation-id": correlationId }),
40 });
41 }
42 else {
43 operation.setContext({
44 headers: Object.assign({}, headers),
45 });
46 }
47 return forward(operation);
48 });
49 const link = middlewareLink.concat(httpLink);
50 this.client = new apollo_client_1.default({
51 link,
52 cache,
53 });
54 }
55 query(options) {
56 if (typeof options === "string") {
57 options = {
58 name: options,
59 };
60 }
61 const q = internalGraphql.query({
62 query: options.query,
63 path: options.path,
64 name: options.name,
65 moduleDir: trace.get()[1].getFileName(),
66 });
67 return this.executeQuery(q, options.variables, options.options);
68 }
69 executeQueryFromFile(queryFile, variables, queryOptions, current) {
70 return this.executeQuery(internalGraphql.resolveAndReadFileSync(queryFile, current, {}), variables, queryOptions);
71 }
72 executeQuery(q, variables, queryOptions) {
73 logger_1.logger.debug(`Querying '%s' with variables '%s' and query: %s`, this.endpoint, stringify(variables), internalGraphql.inlineQuery(q));
74 const query = graphql_tag_1.default(q);
75 const callback = namespace.init().bind(response => {
76 // The following statement is needed for debugging; we can always disable that later
77 logger_1.logger.debug("Query returned data: %s", stringify(response.data));
78 return response.data;
79 });
80 return this.client.query(Object.assign({ query,
81 variables }, queryOptions))
82 .then(result => callback(result));
83 }
84 mutate(options) {
85 if (typeof options === "string") {
86 options = {
87 name: options,
88 };
89 }
90 const m = internalGraphql.mutate({
91 mutation: options.mutation,
92 path: options.path,
93 name: options.name,
94 moduleDir: trace.get()[1].getFileName(),
95 });
96 return this.executeMutation(m, options.variables, options.options);
97 }
98 executeMutationFromFile(mutationFile, variables, mutationOptions, current) {
99 return this.executeMutation(internalGraphql.resolveAndReadFileSync(mutationFile, current, {}), variables, mutationOptions);
100 }
101 executeMutation(m, variables, mutationOptions) {
102 logger_1.logger.debug(`Mutating '%s' with variables '%s' and mutation: %s`, this.endpoint, stringify(variables), internalGraphql.inlineQuery(m));
103 const mutation = graphql_tag_1.default(m);
104 const callback = namespace.init().bind(response => {
105 // The following statement is needed for debugging; we can always disable that later
106 logger_1.logger.debug("Mutation returned data: %s", stringify(response.data));
107 return response.data;
108 });
109 return this.client.mutate(Object.assign({ mutation,
110 variables }, mutationOptions))
111 .then(response => callback(response));
112 }
113}
114exports.ApolloGraphClient = ApolloGraphClient;
115//# sourceMappingURL=ApolloGraphClient.js.map
\No newline at end of file