@graphql-mesh/http 1.0.0-alpha-20230524142207-e9be9c327 → 1.0.0-alpha-20230524151151-439b8293d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ exports.graphqlHandler = void 0;
4
4
  const graphql_yoga_1 = require("graphql-yoga");
5
5
  const graphqlHandler = (getBuiltMesh, playgroundTitle, playgroundEnabled, graphqlEndpoint, corsConfig) => {
6
6
  let yoga$;
7
- return (request, ...args) => {
7
+ return (request, ctx) => {
8
8
  if (!yoga$) {
9
9
  yoga$ = getBuiltMesh().then(mesh => (0, graphql_yoga_1.createYoga)({
10
10
  plugins: [
@@ -28,7 +28,7 @@ const graphqlHandler = (getBuiltMesh, playgroundTitle, playgroundEnabled, graphq
28
28
  landingPage: false,
29
29
  }));
30
30
  }
31
- return yoga$.then(yoga => yoga(request, ...args));
31
+ return yoga$.then(yoga => yoga.handleRequest(request, ctx));
32
32
  };
33
33
  };
34
34
  exports.graphqlHandler = graphqlHandler;
package/cjs/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createMeshHTTPHandler = void 0;
4
- const fets_1 = require("fets");
5
4
  const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
6
5
  const utils_1 = require("@graphql-mesh/utils");
6
+ const server_1 = require("@whatwg-node/server");
7
7
  const graphqlHandler_js_1 = require("./graphqlHandler.js");
8
8
  function createMeshHTTPHandler({ baseDir, getBuiltMesh, rawServeConfig = {}, playgroundTitle, }) {
9
9
  let readyFlag = false;
@@ -13,100 +13,78 @@ function createMeshHTTPHandler({ baseDir, getBuiltMesh, rawServeConfig = {}, pla
13
13
  // TODO
14
14
  // trustProxy = 'loopback',
15
15
  } = rawServeConfig;
16
- const router = (0, fets_1.createRouter)();
17
- router
18
- .route({
19
- path: '*',
20
- handler() {
21
- if (!mesh$) {
22
- mesh$ = getBuiltMesh().then(mesh => {
23
- readyFlag = true;
24
- logger = mesh.logger.child('HTTP');
25
- return mesh;
26
- });
27
- }
28
- },
29
- })
30
- .route({
31
- path: '/healthcheck',
32
- handler() {
33
- return new fets_1.Response(null, {
34
- status: 200,
35
- });
36
- },
37
- })
38
- .route({
39
- path: '/readiness',
40
- handler() {
41
- return new fets_1.Response(null, {
42
- status: readyFlag ? 204 : 503,
43
- });
44
- },
45
- })
46
- .route({
47
- path: '*',
48
- method: 'POST',
49
- async handler(request) {
50
- if (readyFlag) {
51
- const { pubsub } = await mesh$;
52
- for (const eventName of pubsub.getEventNames()) {
53
- const { pathname } = new URL(request.url);
54
- if (eventName === `webhook:${request.method.toLowerCase()}:${pathname}`) {
55
- const body = await request.text();
56
- logger.debug(`Received webhook request for ${pathname}`, body);
57
- pubsub.publish(eventName, request.headers.get('content-type') === 'application/json'
58
- ? JSON.parse(body)
59
- : body);
60
- return new fets_1.Response(null, {
61
- status: 204,
62
- statusText: 'OK',
16
+ return (0, server_1.createServerAdapter)((0, graphqlHandler_js_1.graphqlHandler)(() => mesh$, playgroundTitle, playgroundEnabled, graphqlPath, corsConfig), {
17
+ plugins: [
18
+ {
19
+ onRequest({ request, url, endResponse }) {
20
+ if (!mesh$) {
21
+ mesh$ = getBuiltMesh().then(mesh => {
22
+ readyFlag = true;
23
+ logger = mesh.logger.child('HTTP');
24
+ return mesh;
63
25
  });
64
26
  }
65
- }
66
- }
67
- return undefined;
68
- },
69
- });
70
- if (staticFiles) {
71
- router.route({
72
- path: '/:relativePath+',
73
- method: 'GET',
74
- async handler(request) {
75
- let { relativePath } = request.params;
76
- if (!relativePath) {
77
- relativePath = 'index.html';
78
- }
79
- const absoluteStaticFilesPath = cross_helpers_1.path.join(baseDir, staticFiles);
80
- const absolutePath = cross_helpers_1.path.join(absoluteStaticFilesPath, relativePath);
81
- if (absolutePath.startsWith(absoluteStaticFilesPath) && (await (0, utils_1.pathExists)(absolutePath))) {
82
- const readStream = cross_helpers_1.fs.createReadStream(absolutePath);
83
- return new fets_1.Response(readStream, {
84
- status: 200,
85
- });
86
- }
87
- return undefined;
88
- },
89
- });
90
- }
91
- else if (graphqlPath !== '/') {
92
- router.route({
93
- path: '/',
94
- method: 'GET',
95
- handler() {
96
- return new fets_1.Response(null, {
97
- status: 302,
98
- headers: {
99
- Location: graphqlPath,
100
- },
101
- });
27
+ switch (url.pathname) {
28
+ case '/healthcheck':
29
+ endResponse(new server_1.Response(null, {
30
+ status: 200,
31
+ }));
32
+ return;
33
+ case '/readiness':
34
+ endResponse(new server_1.Response(null, {
35
+ status: readyFlag ? 204 : 503,
36
+ }));
37
+ return;
38
+ }
39
+ if (readyFlag) {
40
+ return mesh$.then(async (mesh) => {
41
+ for (const eventName of mesh.pubsub.getEventNames()) {
42
+ if (eventName === `webhook:${request.method.toLowerCase()}:${url.pathname}`) {
43
+ const body = await request.text();
44
+ logger.debug(`Received webhook request for ${url.pathname}`, body);
45
+ mesh.pubsub.publish(eventName, request.headers.get('content-type') === 'application/json'
46
+ ? JSON.parse(body)
47
+ : body);
48
+ endResponse(new server_1.Response(null, {
49
+ status: 204,
50
+ statusText: 'OK',
51
+ }));
52
+ return;
53
+ }
54
+ }
55
+ });
56
+ }
57
+ if (staticFiles && request.method === 'GET') {
58
+ let relativePath = url.pathname;
59
+ if (relativePath === '/' || !relativePath) {
60
+ relativePath = 'index.html';
61
+ }
62
+ const absoluteStaticFilesPath = cross_helpers_1.path.join(baseDir, staticFiles);
63
+ const absolutePath = cross_helpers_1.path.join(absoluteStaticFilesPath, relativePath);
64
+ if (absolutePath.startsWith(absoluteStaticFilesPath)) {
65
+ return (0, utils_1.pathExists)(absolutePath).then(exists => {
66
+ if (exists) {
67
+ const readStream = cross_helpers_1.fs.createReadStream(absolutePath);
68
+ endResponse(new server_1.Response(readStream, {
69
+ status: 200,
70
+ }));
71
+ }
72
+ });
73
+ }
74
+ }
75
+ else if (graphqlPath !== '/' && url.pathname === '/') {
76
+ endResponse(new server_1.Response(null, {
77
+ status: 302,
78
+ headers: {
79
+ Location: graphqlPath,
80
+ },
81
+ }));
82
+ // eslint-disable-next-line no-useless-return
83
+ return;
84
+ }
85
+ (0, utils_1.withCookies)(request);
86
+ },
102
87
  },
103
- });
104
- }
105
- return router.route({
106
- path: '*',
107
- handler: [
108
- utils_1.withCookies,
109
- (0, graphqlHandler_js_1.graphqlHandler)(() => mesh$, playgroundTitle, playgroundEnabled, graphqlPath, corsConfig),
110
88
  ],
111
89
  });
112
90
  }
@@ -1,7 +1,7 @@
1
1
  import { createYoga, useLogger } from 'graphql-yoga';
2
2
  export const graphqlHandler = (getBuiltMesh, playgroundTitle, playgroundEnabled, graphqlEndpoint, corsConfig) => {
3
3
  let yoga$;
4
- return (request, ...args) => {
4
+ return (request, ctx) => {
5
5
  if (!yoga$) {
6
6
  yoga$ = getBuiltMesh().then(mesh => createYoga({
7
7
  plugins: [
@@ -25,6 +25,6 @@ export const graphqlHandler = (getBuiltMesh, playgroundTitle, playgroundEnabled,
25
25
  landingPage: false,
26
26
  }));
27
27
  }
28
- return yoga$.then(yoga => yoga(request, ...args));
28
+ return yoga$.then(yoga => yoga.handleRequest(request, ctx));
29
29
  };
30
30
  };
package/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { createRouter, Response } from 'fets';
2
1
  import { fs, path } from '@graphql-mesh/cross-helpers';
3
2
  import { DefaultLogger, pathExists, withCookies } from '@graphql-mesh/utils';
3
+ import { createServerAdapter, Response } from '@whatwg-node/server';
4
4
  import { graphqlHandler } from './graphqlHandler.js';
5
5
  export function createMeshHTTPHandler({ baseDir, getBuiltMesh, rawServeConfig = {}, playgroundTitle, }) {
6
6
  let readyFlag = false;
@@ -10,100 +10,78 @@ export function createMeshHTTPHandler({ baseDir, getBuiltMesh, rawServeConfig =
10
10
  // TODO
11
11
  // trustProxy = 'loopback',
12
12
  } = rawServeConfig;
13
- const router = createRouter();
14
- router
15
- .route({
16
- path: '*',
17
- handler() {
18
- if (!mesh$) {
19
- mesh$ = getBuiltMesh().then(mesh => {
20
- readyFlag = true;
21
- logger = mesh.logger.child('HTTP');
22
- return mesh;
23
- });
24
- }
25
- },
26
- })
27
- .route({
28
- path: '/healthcheck',
29
- handler() {
30
- return new Response(null, {
31
- status: 200,
32
- });
33
- },
34
- })
35
- .route({
36
- path: '/readiness',
37
- handler() {
38
- return new Response(null, {
39
- status: readyFlag ? 204 : 503,
40
- });
41
- },
42
- })
43
- .route({
44
- path: '*',
45
- method: 'POST',
46
- async handler(request) {
47
- if (readyFlag) {
48
- const { pubsub } = await mesh$;
49
- for (const eventName of pubsub.getEventNames()) {
50
- const { pathname } = new URL(request.url);
51
- if (eventName === `webhook:${request.method.toLowerCase()}:${pathname}`) {
52
- const body = await request.text();
53
- logger.debug(`Received webhook request for ${pathname}`, body);
54
- pubsub.publish(eventName, request.headers.get('content-type') === 'application/json'
55
- ? JSON.parse(body)
56
- : body);
57
- return new Response(null, {
58
- status: 204,
59
- statusText: 'OK',
13
+ return createServerAdapter(graphqlHandler(() => mesh$, playgroundTitle, playgroundEnabled, graphqlPath, corsConfig), {
14
+ plugins: [
15
+ {
16
+ onRequest({ request, url, endResponse }) {
17
+ if (!mesh$) {
18
+ mesh$ = getBuiltMesh().then(mesh => {
19
+ readyFlag = true;
20
+ logger = mesh.logger.child('HTTP');
21
+ return mesh;
60
22
  });
61
23
  }
62
- }
63
- }
64
- return undefined;
65
- },
66
- });
67
- if (staticFiles) {
68
- router.route({
69
- path: '/:relativePath+',
70
- method: 'GET',
71
- async handler(request) {
72
- let { relativePath } = request.params;
73
- if (!relativePath) {
74
- relativePath = 'index.html';
75
- }
76
- const absoluteStaticFilesPath = path.join(baseDir, staticFiles);
77
- const absolutePath = path.join(absoluteStaticFilesPath, relativePath);
78
- if (absolutePath.startsWith(absoluteStaticFilesPath) && (await pathExists(absolutePath))) {
79
- const readStream = fs.createReadStream(absolutePath);
80
- return new Response(readStream, {
81
- status: 200,
82
- });
83
- }
84
- return undefined;
85
- },
86
- });
87
- }
88
- else if (graphqlPath !== '/') {
89
- router.route({
90
- path: '/',
91
- method: 'GET',
92
- handler() {
93
- return new Response(null, {
94
- status: 302,
95
- headers: {
96
- Location: graphqlPath,
97
- },
98
- });
24
+ switch (url.pathname) {
25
+ case '/healthcheck':
26
+ endResponse(new Response(null, {
27
+ status: 200,
28
+ }));
29
+ return;
30
+ case '/readiness':
31
+ endResponse(new Response(null, {
32
+ status: readyFlag ? 204 : 503,
33
+ }));
34
+ return;
35
+ }
36
+ if (readyFlag) {
37
+ return mesh$.then(async (mesh) => {
38
+ for (const eventName of mesh.pubsub.getEventNames()) {
39
+ if (eventName === `webhook:${request.method.toLowerCase()}:${url.pathname}`) {
40
+ const body = await request.text();
41
+ logger.debug(`Received webhook request for ${url.pathname}`, body);
42
+ mesh.pubsub.publish(eventName, request.headers.get('content-type') === 'application/json'
43
+ ? JSON.parse(body)
44
+ : body);
45
+ endResponse(new Response(null, {
46
+ status: 204,
47
+ statusText: 'OK',
48
+ }));
49
+ return;
50
+ }
51
+ }
52
+ });
53
+ }
54
+ if (staticFiles && request.method === 'GET') {
55
+ let relativePath = url.pathname;
56
+ if (relativePath === '/' || !relativePath) {
57
+ relativePath = 'index.html';
58
+ }
59
+ const absoluteStaticFilesPath = path.join(baseDir, staticFiles);
60
+ const absolutePath = path.join(absoluteStaticFilesPath, relativePath);
61
+ if (absolutePath.startsWith(absoluteStaticFilesPath)) {
62
+ return pathExists(absolutePath).then(exists => {
63
+ if (exists) {
64
+ const readStream = fs.createReadStream(absolutePath);
65
+ endResponse(new Response(readStream, {
66
+ status: 200,
67
+ }));
68
+ }
69
+ });
70
+ }
71
+ }
72
+ else if (graphqlPath !== '/' && url.pathname === '/') {
73
+ endResponse(new Response(null, {
74
+ status: 302,
75
+ headers: {
76
+ Location: graphqlPath,
77
+ },
78
+ }));
79
+ // eslint-disable-next-line no-useless-return
80
+ return;
81
+ }
82
+ withCookies(request);
83
+ },
99
84
  },
100
- });
101
- }
102
- return router.route({
103
- path: '*',
104
- handler: [
105
- withCookies,
106
- graphqlHandler(() => mesh$, playgroundTitle, playgroundEnabled, graphqlPath, corsConfig),
107
85
  ],
108
86
  });
109
87
  }
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@graphql-mesh/http",
3
- "version": "1.0.0-alpha-20230524142207-e9be9c327",
3
+ "version": "1.0.0-alpha-20230524151151-439b8293d",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@graphql-mesh/cross-helpers": "0.4.0-alpha-20230524142207-e9be9c327",
7
- "@graphql-mesh/runtime": "1.0.0-alpha-20230524142207-e9be9c327",
8
- "@graphql-mesh/types": "1.0.0-alpha-20230524142207-e9be9c327",
9
- "@graphql-mesh/utils": "1.0.0-alpha-20230524142207-e9be9c327",
6
+ "@graphql-mesh/cross-helpers": "0.4.0-alpha-20230524151151-439b8293d",
7
+ "@graphql-mesh/runtime": "1.0.0-alpha-20230524151151-439b8293d",
8
+ "@graphql-mesh/types": "1.0.0-alpha-20230524151151-439b8293d",
9
+ "@graphql-mesh/utils": "1.0.0-alpha-20230524151151-439b8293d",
10
10
  "graphql": "*",
11
11
  "tslib": "^2.4.0"
12
12
  },
13
13
  "dependencies": {
14
- "fets": "^0.2.0",
14
+ "@whatwg-node/server": "^0.8.0",
15
15
  "graphql-yoga": "^3.9.1"
16
16
  },
17
17
  "repository": {
@@ -1,3 +1,3 @@
1
1
  import { CORSOptions } from 'graphql-yoga';
2
2
  import { MeshInstance } from '@graphql-mesh/runtime';
3
- export declare const graphqlHandler: (getBuiltMesh: () => Promise<MeshInstance>, playgroundTitle: string, playgroundEnabled: boolean, graphqlEndpoint: string, corsConfig: CORSOptions) => (request: Request, ...args: any[]) => Promise<Response>;
3
+ export declare const graphqlHandler: (getBuiltMesh: () => Promise<MeshInstance>, playgroundTitle: string, playgroundEnabled: boolean, graphqlEndpoint: string, corsConfig: CORSOptions) => (request: Request, ctx: any) => Promise<Response>;
@@ -1,3 +1,3 @@
1
1
  import { CORSOptions } from 'graphql-yoga';
2
2
  import { MeshInstance } from '@graphql-mesh/runtime';
3
- export declare const graphqlHandler: (getBuiltMesh: () => Promise<MeshInstance>, playgroundTitle: string, playgroundEnabled: boolean, graphqlEndpoint: string, corsConfig: CORSOptions) => (request: Request, ...args: any[]) => Promise<Response>;
3
+ export declare const graphqlHandler: (getBuiltMesh: () => Promise<MeshInstance>, playgroundTitle: string, playgroundEnabled: boolean, graphqlEndpoint: string, corsConfig: CORSOptions) => (request: Request, ctx: any) => Promise<Response>;
@@ -1,10 +1,9 @@
1
- import { Router } from 'fets';
2
1
  import type { MeshInstance } from '@graphql-mesh/runtime';
3
2
  import { YamlConfig } from '@graphql-mesh/types';
4
- export type MeshHTTPHandler<TServerContext> = Router<TServerContext, {}, {}>;
3
+ export type MeshHTTPHandler = ReturnType<typeof createMeshHTTPHandler>;
5
4
  export declare function createMeshHTTPHandler<TServerContext>({ baseDir, getBuiltMesh, rawServeConfig, playgroundTitle, }: {
6
5
  baseDir: string;
7
6
  getBuiltMesh: () => Promise<MeshInstance>;
8
7
  rawServeConfig?: YamlConfig.Config['serve'];
9
8
  playgroundTitle?: string;
10
- }): MeshHTTPHandler<TServerContext>;
9
+ }): import("@whatwg-node/server").ServerAdapter<TServerContext, import("@whatwg-node/server").ServerAdapterBaseObject<TServerContext, import("@whatwg-node/server").ServerAdapterRequestHandler<TServerContext>>>;
@@ -1,10 +1,9 @@
1
- import { Router } from 'fets';
2
1
  import type { MeshInstance } from '@graphql-mesh/runtime';
3
2
  import { YamlConfig } from '@graphql-mesh/types';
4
- export type MeshHTTPHandler<TServerContext> = Router<TServerContext, {}, {}>;
3
+ export type MeshHTTPHandler = ReturnType<typeof createMeshHTTPHandler>;
5
4
  export declare function createMeshHTTPHandler<TServerContext>({ baseDir, getBuiltMesh, rawServeConfig, playgroundTitle, }: {
6
5
  baseDir: string;
7
6
  getBuiltMesh: () => Promise<MeshInstance>;
8
7
  rawServeConfig?: YamlConfig.Config['serve'];
9
8
  playgroundTitle?: string;
10
- }): MeshHTTPHandler<TServerContext>;
9
+ }): import("@whatwg-node/server").ServerAdapter<TServerContext, import("@whatwg-node/server").ServerAdapterBaseObject<TServerContext, import("@whatwg-node/server").ServerAdapterRequestHandler<TServerContext>>>;