UNPKG

@atomist/automation-client

Version:
109 lines 4.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const apollo_cache_inmemory_1 = require("apollo-cache-inmemory"); const apollo_client_1 = require("apollo-client"); const apollo_link_1 = require("apollo-link"); const apollo_link_http_1 = require("apollo-link-http"); const axios_1 = require("axios"); const axios_fetch_1 = require("axios-fetch"); const graphql_tag_1 = require("graphql-tag"); const stringify = require("json-stringify-safe"); const trace = require("stack-trace"); const internalGraphql = require("../internal/graph/graphQL"); const namespace = require("../internal/util/cls"); const http_1 = require("../internal/util/http"); const logger_1 = require("../internal/util/logger"); /** * Implementation of GraphClient using Apollo Client. */ class ApolloGraphClient { /** * Create a new GraphClient * @param endpoint GraphQL endpoint * @param headers any special headers to use */ constructor(endpoint, headers = {}) { this.endpoint = endpoint; const cache = new apollo_cache_inmemory_1.InMemoryCache({ addTypename: false, }); const httpLink = apollo_link_http_1.createHttpLink({ uri: endpoint, fetch: axios_fetch_1.buildAxiosFetch(axios_1.default.create(http_1.configureProxy({}))), }); const middlewareLink = new apollo_link_1.ApolloLink((operation, forward) => { // attach the correlation-id to the request const correlationId = namespace.get() ? namespace.get().correlationId : undefined; if (correlationId) { operation.setContext({ headers: Object.assign({}, headers, { "correlation-id": correlationId }), }); } else { operation.setContext({ headers: Object.assign({}, headers), }); } return forward(operation); }); const link = middlewareLink.concat(httpLink); this.client = new apollo_client_1.default({ link, cache, }); } query(options) { if (typeof options === "string") { options = { name: options, }; } const q = internalGraphql.query({ query: options.query, path: options.path, name: options.name, moduleDir: trace.get()[1].getFileName(), }); return this.executeQuery(q, options.variables, options.options); } mutate(options) { if (typeof options === "string") { options = { name: options, }; } const m = internalGraphql.mutate({ mutation: options.mutation, path: options.path, name: options.name, moduleDir: trace.get()[1].getFileName(), }); return this.executeMutation(m, options.variables, options.options); } executeQuery(q, variables, queryOptions) { logger_1.logger.debug(`Querying '%s' with variables '%s' and query: %s`, this.endpoint, stringify(variables), internalGraphql.inlineQuery(q)); const query = graphql_tag_1.default(q); const callback = namespace.init().bind(response => { // The following statement is needed for debugging; we can always disable that later logger_1.logger.debug("Query returned data: %s", stringify(response.data)); return response.data; }); return this.client.query(Object.assign({ query, variables }, queryOptions)) .then(result => callback(result)); } executeMutation(m, variables, mutationOptions) { logger_1.logger.debug(`Mutating '%s' with variables '%s' and mutation: %s`, this.endpoint, stringify(variables), internalGraphql.inlineQuery(m)); const mutation = graphql_tag_1.default(m); const callback = namespace.init().bind(response => { // The following statement is needed for debugging; we can always disable that later logger_1.logger.debug("Mutation returned data: %s", stringify(response.data)); return response.data; }); return this.client.mutate(Object.assign({ mutation, variables }, mutationOptions)) .then(response => callback(response)); } } exports.ApolloGraphClient = ApolloGraphClient; //# sourceMappingURL=ApolloGraphClient.js.map