UNPKG

3.37 kBJavaScriptView Raw
1'use strict';
2
3const fetchache = require('fetchache');
4const urlLoader = require('@graphql-tools/url-loader');
5const graphql = require('graphql');
6const wrap = require('@graphql-tools/wrap');
7const utils = require('@graphql-mesh/utils');
8
9class 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.introspection) {
34 const result = await urlLoader$1.handleSDL(this.config.introspection, {
35 customFetch: customFetch,
36 ...this.config,
37 headers: this.config.schemaHeaders,
38 });
39 schema = result.schema;
40 }
41 else if (this.config.cacheIntrospection) {
42 const cacheKey = this.name + '_introspection';
43 const introspectionData = await this.cache.get(cacheKey);
44 if (introspectionData) {
45 schema = graphql.buildClientSchema(introspectionData);
46 }
47 else {
48 schema = await wrap.introspectSchema(async (params) => {
49 const { executor } = await getExecutorAndSubscriberForParams(params, schemaHeadersFactory);
50 return executor(params);
51 });
52 const ttl = typeof this.config.cacheIntrospection === 'object' && this.config.cacheIntrospection.ttl;
53 const introspection = graphql.introspectionFromSchema(schema);
54 this.cache.set(cacheKey, introspection, { ttl });
55 }
56 }
57 else {
58 schema = await wrap.introspectSchema(async (params) => {
59 const { executor } = await getExecutorAndSubscriberForParams(params, schemaHeadersFactory);
60 return executor(params);
61 });
62 }
63 const operationHeadersFactory = utils.getInterpolatedHeadersFactory(this.config.operationHeaders);
64 return {
65 schema,
66 executor: async (params) => {
67 const { executor } = await getExecutorAndSubscriberForParams(params, operationHeadersFactory);
68 return executor(params);
69 },
70 subscriber: async (params) => {
71 const { subscriber } = await getExecutorAndSubscriberForParams(params, operationHeadersFactory);
72 return subscriber(params);
73 },
74 };
75 }
76}
77
78module.exports = GraphQLHandler;
79//# sourceMappingURL=index.cjs.js.map