1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const apollo_cache_inmemory_1 = require("apollo-cache-inmemory");
|
4 | const apollo_client_1 = require("apollo-client");
|
5 | const apollo_link_1 = require("apollo-link");
|
6 | const apollo_link_http_1 = require("apollo-link-http");
|
7 | const axios_1 = require("axios");
|
8 | const axios_fetch_1 = require("axios-fetch");
|
9 | const graphql_tag_1 = require("graphql-tag");
|
10 | const stringify = require("json-stringify-safe");
|
11 | const trace = require("stack-trace");
|
12 | const internalGraphql = require("../internal/graph/graphQL");
|
13 | const namespace = require("../internal/util/cls");
|
14 | const http_1 = require("../internal/util/http");
|
15 | const logger_1 = require("../internal/util/logger");
|
16 |
|
17 |
|
18 |
|
19 | class ApolloGraphClient {
|
20 | |
21 |
|
22 |
|
23 |
|
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 |
|
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: options.moduleDir ? options.moduleDir : trace.get()[1].getFileName(),
|
66 | });
|
67 | return this.executeQuery(q, options.variables, options.options);
|
68 | }
|
69 | mutate(options) {
|
70 | if (typeof options === "string") {
|
71 | options = {
|
72 | name: options,
|
73 | };
|
74 | }
|
75 | const m = internalGraphql.mutate({
|
76 | mutation: options.mutation,
|
77 | path: options.path,
|
78 | name: options.name,
|
79 | moduleDir: options.moduleDir ? options.moduleDir : trace.get()[1].getFileName(),
|
80 | });
|
81 | return this.executeMutation(m, options.variables, options.options);
|
82 | }
|
83 | executeQuery(q, variables, queryOptions) {
|
84 | logger_1.logger.debug(`Querying '%s' with variables '%s' and query: %s`, this.endpoint, stringify(variables), internalGraphql.inlineQuery(q));
|
85 | const query = graphql_tag_1.default(q);
|
86 | const callback = namespace.init().bind(response => {
|
87 |
|
88 | logger_1.logger.debug("Query returned data: %s", stringify(response.data));
|
89 | return response.data;
|
90 | });
|
91 | return this.client.query(Object.assign({ query,
|
92 | variables, errorPolicy: "all" }, queryOptions))
|
93 | .then(result => callback(result));
|
94 | }
|
95 | executeMutation(m, variables, mutationOptions) {
|
96 | logger_1.logger.debug(`Mutating '%s' with variables '%s' and mutation: %s`, this.endpoint, stringify(variables), internalGraphql.inlineQuery(m));
|
97 | const mutation = graphql_tag_1.default(m);
|
98 | const callback = namespace.init().bind(response => {
|
99 |
|
100 | logger_1.logger.debug("Mutation returned data: %s", stringify(response.data));
|
101 | return response.data;
|
102 | });
|
103 | return this.client.mutate(Object.assign({ mutation,
|
104 | variables, errorPolicy: "all" }, mutationOptions))
|
105 | .then(response => callback(response));
|
106 | }
|
107 | }
|
108 | exports.ApolloGraphClient = ApolloGraphClient;
|
109 |
|
\ | No newline at end of file |