1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
7 | class BrowserEngine {
|
8 | constructor({ fetcher }) {
|
9 | this.defaultFetcher = async ({ query, typeName }) => {
|
10 | return cross_fetch_1.default(this.url, {
|
11 | method: 'POST',
|
12 | headers: {
|
13 | 'Content-Type': 'application/json',
|
14 | },
|
15 | body: JSON.stringify({ query, variables: {}, operationName: '' }),
|
16 | })
|
17 | .then(response => {
|
18 | if (!response.ok) {
|
19 | return response.text().then(body => {
|
20 | const { status, statusText } = response;
|
21 | this.handleErrors({
|
22 | errors: {
|
23 | status,
|
24 | statusText,
|
25 | body,
|
26 | },
|
27 | query,
|
28 | });
|
29 | });
|
30 | }
|
31 | else {
|
32 | return response.json();
|
33 | }
|
34 | })
|
35 | .catch(errors => {
|
36 | return this.handleErrors({ errors, query });
|
37 | });
|
38 | };
|
39 | this.fetcher = fetcher || this.defaultFetcher;
|
40 | }
|
41 | async start() { }
|
42 | async stop() { }
|
43 | async request(query, typeName) {
|
44 | return this.fetcher({ query, typeName }).then(result => {
|
45 | const { data } = result;
|
46 | const errors = result.error || result.errors;
|
47 | if (errors) {
|
48 | return this.handleErrors({
|
49 | errors,
|
50 | query,
|
51 | });
|
52 | }
|
53 | return data;
|
54 | });
|
55 | }
|
56 | handleErrors({ errors, query }) {
|
57 | const stringified = errors ? JSON.stringify(errors, null, 2) : null;
|
58 | const message = stringified.length > 0 ? stringified : `Error in prisma.\$\{rootField || 'query'}`;
|
59 | throw new Error(message);
|
60 | }
|
61 | }
|
62 | exports.BrowserEngine = BrowserEngine;
|
63 |
|
\ | No newline at end of file |