@fougere/adapter-graphql 0.2.0-alpha.2 → 0.4.0-alpha.0
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.
- package/README.md +1 -1
- package/dist/app.d.ts +43 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +50 -0
- package/dist/app.js.map +1 -0
- package/dist/auto-register.d.ts +22 -7
- package/dist/auto-register.d.ts.map +1 -1
- package/dist/auto-register.js +23 -45
- package/dist/auto-register.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/pothos.d.ts +43 -11
- package/dist/pothos.d.ts.map +1 -1
- package/dist/pothos.js +127 -52
- package/dist/pothos.js.map +1 -1
- package/package.json +5 -4
- package/src/app.ts +73 -0
- package/src/auto-register.ts +474 -0
- package/src/index.ts +14 -0
- package/src/pothos-entry.ts +9 -0
- package/src/pothos.ts +1011 -0
- package/src/serve.ts +133 -0
package/src/serve.ts
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Serve a GraphQL schema on an HttpRouter — no Apollo, no Yoga needed.
|
|
3
|
+
*
|
|
4
|
+
* Uses the `graphql` package directly for execution.
|
|
5
|
+
* GraphiQL is available as an explicit opt-in for trusted development environments.
|
|
6
|
+
*/
|
|
7
|
+
import type { HttpRouter, ResponseResult } from '@fougere/http';
|
|
8
|
+
import { getOperationAST, graphql, parse, type GraphQLSchema } from 'graphql';
|
|
9
|
+
|
|
10
|
+
export interface GraphQLServeOptions {
|
|
11
|
+
/** Route path. Default: '/graphql'. */
|
|
12
|
+
path?: string;
|
|
13
|
+
/** Enable GraphiQL playground on GET requests from browsers. Default: false. */
|
|
14
|
+
playground?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function graphiqlHtml(path: string): string {
|
|
18
|
+
return `<!DOCTYPE html>
|
|
19
|
+
<html lang="en">
|
|
20
|
+
<head>
|
|
21
|
+
<meta charset="utf-8" />
|
|
22
|
+
<title>GraphiQL — Fougere</title>
|
|
23
|
+
<link rel="stylesheet" href="https://unpkg.com/graphiql@3/graphiql.min.css" />
|
|
24
|
+
<style>body{margin:0;height:100vh}#graphiql{height:100vh}</style>
|
|
25
|
+
</head>
|
|
26
|
+
<body>
|
|
27
|
+
<div id="graphiql"></div>
|
|
28
|
+
<script src="https://unpkg.com/react@18/umd/react.production.min.js" crossorigin></script>
|
|
29
|
+
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js" crossorigin></script>
|
|
30
|
+
<script src="https://unpkg.com/graphiql@3/graphiql.min.js" crossorigin></script>
|
|
31
|
+
<script>
|
|
32
|
+
const fetcher = GraphiQL.createFetcher({ url: '${path}' });
|
|
33
|
+
const root = ReactDOM.createRoot(document.getElementById('graphiql'));
|
|
34
|
+
root.render(React.createElement(GraphiQL, { fetcher }));
|
|
35
|
+
</script>
|
|
36
|
+
</body>
|
|
37
|
+
</html>`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Register a GraphQL endpoint on an HttpRouter.
|
|
42
|
+
*
|
|
43
|
+
* - POST: execute queries/mutations
|
|
44
|
+
* - GET with `query` param: execute queries
|
|
45
|
+
* - GET from browser (no `query` param): serve GraphiQL only with `playground: true`
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* import { registerGraphQL } from '@fougere/adapter-graphql'
|
|
49
|
+
*
|
|
50
|
+
* registerGraphQL(router, schema, { playground: process.env.NODE_ENV === 'development' })
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function registerGraphQL(
|
|
54
|
+
router: HttpRouter,
|
|
55
|
+
schema: GraphQLSchema,
|
|
56
|
+
options?: GraphQLServeOptions,
|
|
57
|
+
): void {
|
|
58
|
+
const path = options?.path ?? '/graphql';
|
|
59
|
+
const playground = options?.playground ?? false;
|
|
60
|
+
|
|
61
|
+
router.on('POST', path, async (ctx) => {
|
|
62
|
+
const body = (await ctx.body()) as {
|
|
63
|
+
query?: string;
|
|
64
|
+
variables?: Record<string, unknown>;
|
|
65
|
+
operationName?: string;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
if (!body?.query) {
|
|
69
|
+
return { status: 400, data: { errors: [{ message: 'Missing query' }] } };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const result = await graphql({
|
|
73
|
+
schema,
|
|
74
|
+
source: body.query,
|
|
75
|
+
variableValues: body.variables,
|
|
76
|
+
operationName: body.operationName,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
status: 200,
|
|
81
|
+
data: result,
|
|
82
|
+
headers: { 'content-type': 'application/json' },
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
router.on('GET', path, async (ctx): Promise<ResponseResult> => {
|
|
87
|
+
// If there's a query param, execute it
|
|
88
|
+
if (ctx.query.query) {
|
|
89
|
+
let variables: Record<string, unknown> | undefined;
|
|
90
|
+
let document;
|
|
91
|
+
try {
|
|
92
|
+
variables = ctx.query.variables ? JSON.parse(ctx.query.variables) : undefined;
|
|
93
|
+
document = parse(ctx.query.query);
|
|
94
|
+
} catch (err) {
|
|
95
|
+
return { status: 400, data: { errors: [{ message: (err as Error).message }] } };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const operation = getOperationAST(document, ctx.query.operationName);
|
|
99
|
+
if (operation && operation.operation !== 'query') {
|
|
100
|
+
return {
|
|
101
|
+
status: 405,
|
|
102
|
+
data: { errors: [{ message: 'Only GraphQL queries may be executed over GET' }] },
|
|
103
|
+
headers: { allow: 'POST' },
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const result = await graphql({
|
|
108
|
+
schema,
|
|
109
|
+
source: ctx.query.query,
|
|
110
|
+
variableValues: variables,
|
|
111
|
+
operationName: ctx.query.operationName,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return {
|
|
115
|
+
status: 200,
|
|
116
|
+
data: result,
|
|
117
|
+
headers: { 'content-type': 'application/json' },
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// No query param → serve GraphiQL if enabled
|
|
122
|
+
if (playground) {
|
|
123
|
+
return {
|
|
124
|
+
status: 200,
|
|
125
|
+
data: graphiqlHtml(path),
|
|
126
|
+
headers: { 'content-type': 'text/html' },
|
|
127
|
+
raw: true,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return { status: 400, data: { errors: [{ message: 'Missing query parameter' }] } };
|
|
132
|
+
});
|
|
133
|
+
}
|