UNPKG

4.27 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2020 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18const fs = require("fs-extra");
19const path = require("path");
20const log_1 = require("./log");
21const util_1 = require("./util");
22const GraphQLCache = new Map();
23class NodeFetchGraphQLClient {
24 constructor(apiKey, url) {
25 this.apiKey = apiKey;
26 this.url = url;
27 }
28 async query(query, variables) {
29 const f = (await Promise.resolve().then(() => require("node-fetch"))).default;
30 const body = JSON.stringify({
31 query: await this.graphql(query, "query"),
32 variables,
33 });
34 log_1.debug(`GraphQL query: ${body}`);
35 const result = await (await f(this.url, {
36 method: "post",
37 body,
38 headers: {
39 "authorization": `bearer ${this.apiKey}`,
40 "content-type": "application/json",
41 },
42 })).json();
43 log_1.debug(`GraphQL result: ${JSON.stringify(result, util_1.replacer)}`);
44 if (result.errors) {
45 throw new Error(JSON.stringify(result.errors, undefined, 2));
46 }
47 return result.data;
48 }
49 async mutate(mutation, variables) {
50 const f = (await Promise.resolve().then(() => require("node-fetch"))).default;
51 const body = JSON.stringify({
52 query: await this.graphql(mutation, "mutation"),
53 variables,
54 });
55 log_1.debug(`GraphQL mutation: ${body}`);
56 const result = await (await f(this.url, {
57 method: "post",
58 body,
59 headers: {
60 "authorization": `bearer ${this.apiKey}`,
61 "content-type": "application/json",
62 },
63 })).json();
64 log_1.debug(`GraphQL result: ${JSON.stringify(result, util_1.replacer)}`);
65 if (result.errors) {
66 throw new Error(JSON.stringify(result.errors, undefined, 2));
67 }
68 return result.data;
69 }
70 async graphql(query, prefix) {
71 if (typeof query === "string") {
72 let q = query === null || query === void 0 ? void 0 : query.trim();
73 if (GraphQLCache.has(query)) {
74 return GraphQLCache.get(query);
75 }
76 else if (q.endsWith(".graphql")) {
77 // Case for being installed into node_modules
78 const p = path.join(__dirname, "..", "..", "..", "..", "graphql", prefix, q);
79 if (await fs.pathExists(p)) {
80 q = (await fs.readFile(p)).toString();
81 }
82 else {
83 // Case for being bundled into one js file
84 const p = path.join(__dirname, "..", "graphql", prefix, q);
85 q = (await fs.readFile(p)).toString();
86 }
87 }
88 q = q.replace(/\n/g, "");
89 GraphQLCache.set(query, q);
90 return q;
91 }
92 else {
93 const l = query;
94 const p = path.resolve(l.root, l.path);
95 if (GraphQLCache.has(p)) {
96 return GraphQLCache.get(p);
97 }
98 else {
99 let q = (await fs.readFile(p)).toString();
100 q = q.replace(/\n/g, "");
101 GraphQLCache.set(p, q);
102 return q;
103 }
104 }
105 }
106}
107function createGraphQLClient(apiKey, wid) {
108 const url = `${process.env.ATOMIST_GRAPHQL_ENDPOINT
109 || process.env.GRAPHQL_ENDPOINT
110 || "https://automation.atomist.com/graphql"}/team/${wid}`;
111 return new NodeFetchGraphQLClient(apiKey, url);
112}
113exports.createGraphQLClient = createGraphQLClient;
114//# sourceMappingURL=graphql.js.map
\No newline at end of file