1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const fs = require("fs-extra");
|
19 | const path = require("path");
|
20 | const log_1 = require("./log");
|
21 | const util_1 = require("./util");
|
22 | const GraphQLCache = new Map();
|
23 | class 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 |
|
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 |
|
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 | }
|
107 | function 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 | }
|
113 | exports.createGraphQLClient = createGraphQLClient;
|
114 |
|
\ | No newline at end of file |