1 | 'use strict';
|
2 |
|
3 | const fetchache = require('fetchache');
|
4 | const urlLoader = require('@graphql-tools/url-loader');
|
5 | const graphql = require('graphql');
|
6 | const wrap = require('@graphql-tools/wrap');
|
7 | const utils = require('@graphql-mesh/utils');
|
8 |
|
9 | class GraphQLHandler {
|
10 | constructor({ name, config, cache }) {
|
11 | this.name = name;
|
12 | this.config = config;
|
13 | this.cache = cache;
|
14 | }
|
15 | async getMeshSource() {
|
16 | const urlLoader$1 = new urlLoader.UrlLoader();
|
17 | const customFetch = (...args) => fetchache.fetchache(args[0] instanceof fetchache.Request ? args[0] : new fetchache.Request(...args), this.cache);
|
18 | const getExecutorAndSubscriberForParams = (params, headersFactory) => {
|
19 | const resolverData = {
|
20 | root: {},
|
21 | args: params.variables,
|
22 | context: params.context,
|
23 | };
|
24 | const headers = utils.getHeadersObject(headersFactory(resolverData));
|
25 | return urlLoader$1.getExecutorAndSubscriber(this.config.endpoint, {
|
26 | customFetch: customFetch,
|
27 | ...this.config,
|
28 | headers,
|
29 | });
|
30 | };
|
31 | let schema;
|
32 | const schemaHeadersFactory = utils.getInterpolatedHeadersFactory(this.config.schemaHeaders);
|
33 | if (this.config.cacheIntrospection) {
|
34 | const cacheKey = this.name + '_introspection';
|
35 | const introspectionData = await this.cache.get(cacheKey);
|
36 | if (introspectionData) {
|
37 | schema = graphql.buildClientSchema(introspectionData);
|
38 | }
|
39 | else {
|
40 | schema = await wrap.introspectSchema(async (params) => {
|
41 | const { executor } = await getExecutorAndSubscriberForParams(params, schemaHeadersFactory);
|
42 | return executor(params);
|
43 | });
|
44 | await this.cache.set(cacheKey, graphql.introspectionFromSchema(schema));
|
45 | }
|
46 | }
|
47 | else {
|
48 | schema = await wrap.introspectSchema(async (params) => {
|
49 | const { executor } = await getExecutorAndSubscriberForParams(params, schemaHeadersFactory);
|
50 | return executor(params);
|
51 | });
|
52 | }
|
53 | const operationHeadersFactory = utils.getInterpolatedHeadersFactory(this.config.operationHeaders);
|
54 | return {
|
55 | schema,
|
56 | executor: async (params) => {
|
57 | const { executor } = await getExecutorAndSubscriberForParams(params, operationHeadersFactory);
|
58 | return executor(params);
|
59 | },
|
60 | subscriber: async (params) => {
|
61 | const { subscriber } = await getExecutorAndSubscriberForParams(params, operationHeadersFactory);
|
62 | return subscriber(params);
|
63 | },
|
64 | };
|
65 | }
|
66 | }
|
67 |
|
68 | module.exports = GraphQLHandler;
|
69 |
|