-
Notifications
You must be signed in to change notification settings - Fork 559
/
use-graphiql.ts
131 lines (120 loc) 路 4.33 KB
/
use-graphiql.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { PromiseOrValue } from '@envelop/core';
import { YogaLogger } from '@graphql-yoga/logger';
import graphiqlHTML from '../graphiql-html.js';
import { FetchAPI } from '../types.js';
import { Plugin } from './types.js';
export function shouldRenderGraphiQL({ headers, method }: Request): boolean {
return method === 'GET' && !!headers?.get('accept')?.includes('text/html');
}
export type GraphiQLOptions = {
/**
* An optional GraphQL string to use when no query is provided and no stored
* query exists from a previous session. If undefined is provided, GraphiQL
* will use its own default query.
*/
defaultQuery?: string;
/**
* The initial headers to render inside the header editor. Defaults to `"{}"`.
* The value should be a JSON encoded string, for example:
* `headers: JSON.stringify({Authorization: "Bearer your-auth-key"})`
*/
headers?: string;
/**
* More info there: https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
*/
credentials?: RequestCredentials;
/**
* The title to display at the top of the page. Defaults to `"Yoga GraphiQL"`.
*/
title?: string;
/**
* Protocol for subscriptions
*/
subscriptionsProtocol?: 'SSE' | 'GRAPHQL_SSE' | 'WS' | 'LEGACY_WS';
/**
* Extra headers you always want to pass with users' headers input
*/
additionalHeaders?: Record<string, string>;
};
export type GraphiQLRendererOptions = {
/**
* The endpoint requests should be sent. Defaults to `"/graphql"`.
*/
endpoint?: string;
} & GraphiQLOptions;
export const renderGraphiQL = (opts: GraphiQLRendererOptions) =>
graphiqlHTML
.replace('__TITLE__', opts?.title || 'Yoga GraphiQL')
.replace('__OPTS__', JSON.stringify(opts ?? {}));
export type GraphiQLOptionsFactory<TServerContext> = (
request: Request,
// eslint-disable-next-line @typescript-eslint/ban-types
...args: {} extends TServerContext
? [serverContext?: TServerContext | undefined]
: [serverContext: TServerContext]
) => PromiseOrValue<GraphiQLOptions | boolean>;
export type GraphiQLOptionsOrFactory<TServerContext> =
| GraphiQLOptions
| GraphiQLOptionsFactory<TServerContext>
| boolean;
export interface GraphiQLPluginConfig<TServerContext> {
graphqlEndpoint: string;
options?: GraphiQLOptionsOrFactory<TServerContext>;
render?(options: GraphiQLRendererOptions): PromiseOrValue<BodyInit>;
logger?: YogaLogger;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function useGraphiQL<TServerContext extends Record<string, any>>(
config: GraphiQLPluginConfig<TServerContext>,
// eslint-disable-next-line @typescript-eslint/ban-types
): Plugin<{}, TServerContext> {
const logger = config.logger ?? console;
let graphiqlOptionsFactory: GraphiQLOptionsFactory<TServerContext>;
if (typeof config?.options === 'function') {
graphiqlOptionsFactory = config?.options;
} else if (typeof config?.options === 'object') {
graphiqlOptionsFactory = () => config?.options as GraphiQLOptions;
} else if (config?.options === false) {
graphiqlOptionsFactory = () => false;
} else {
graphiqlOptionsFactory = () => ({});
}
const renderer = config?.render ?? renderGraphiQL;
let urlPattern: URLPattern;
const getUrlPattern = ({ URLPattern }: FetchAPI) => {
urlPattern ||= new URLPattern({
pathname: config.graphqlEndpoint,
});
return urlPattern;
};
return {
async onRequest({ request, serverContext, fetchAPI, endResponse, url }) {
if (
shouldRenderGraphiQL(request) &&
(request.url.endsWith(config.graphqlEndpoint) ||
request.url.endsWith(`${config.graphqlEndpoint}/`) ||
url.pathname === config.graphqlEndpoint ||
url.pathname === `${config.graphqlEndpoint}/` ||
getUrlPattern(fetchAPI).test(url))
) {
logger.debug(`Rendering GraphiQL`);
const graphiqlOptions = await graphiqlOptionsFactory(
request,
serverContext as TServerContext,
);
if (graphiqlOptions) {
const graphiQLBody = await renderer({
...(graphiqlOptions === true ? {} : graphiqlOptions),
});
const response = new fetchAPI.Response(graphiQLBody, {
headers: {
'Content-Type': 'text/html',
},
status: 200,
});
endResponse(response);
}
}
},
};
}