@lewebsimple/nuxt-graphql 0.1.1 → 0.1.3
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 +14 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +61 -16
- package/dist/runtime/server/tsconfig.json +1 -1
- package/package.json +1 -1
- package/dist/runtime/server/handler.d.ts +0 -2
- package/dist/runtime/server/handler.js +0 -10
- package/dist/runtime/types/graphql-schema.d.ts +0 -5
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@ Opinionated Nuxt module for using GraphQL Yoga on the server and urql as a clien
|
|
|
11
11
|
- 🏀 [Online playground](https://stackblitz.com/github/lewebsimple/nuxt-graphql?file=playground%2Fapp.vue)
|
|
12
12
|
|
|
13
13
|
## Features
|
|
14
|
-
- 🧘♂️ GraphQL Yoga server handler with user-provided schema
|
|
14
|
+
- 🧘♂️ GraphQL Yoga server handler with user-provided schema / context
|
|
15
15
|
|
|
16
16
|
## Quick Setup
|
|
17
17
|
|
|
@@ -40,6 +40,19 @@ export const schema = createSchema({
|
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
+
Define your GraphQL context in `server/graphql/context.ts`:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import type { H3Event } from "h3";
|
|
47
|
+
|
|
48
|
+
export async function createContext(_event: H3Event) {
|
|
49
|
+
return {
|
|
50
|
+
foo: "bar",
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
```
|
|
55
|
+
|
|
43
56
|
That's it! You can now use Nuxt GraphQL in your Nuxt app ✨
|
|
44
57
|
|
|
45
58
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useLogger, defineNuxtModule, createResolver, getLayerDirectories,
|
|
2
|
-
import { existsSync
|
|
1
|
+
import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addTypeTemplate, addServerTemplate, addServerHandler, addPlugin } from '@nuxt/kit';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
|
|
5
5
|
const logger = useLogger("@lewebsimple/nuxt-graphql");
|
|
@@ -30,25 +30,70 @@ const module$1 = defineNuxtModule({
|
|
|
30
30
|
}
|
|
31
31
|
},
|
|
32
32
|
async setup(options, nuxt) {
|
|
33
|
-
const
|
|
33
|
+
const { resolve } = createResolver(import.meta.url);
|
|
34
34
|
const { rootDir, serverDir } = nuxt.options;
|
|
35
35
|
const layerDirs = [...getLayerDirectories(nuxt), { server: serverDir.replace(rootDir, `${rootDir}/playground`) }];
|
|
36
36
|
const schemaPath = findServerFile(layerDirs, "graphql/schema");
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
const contextPath = findServerFile(layerDirs, "graphql/context");
|
|
38
|
+
addTypeTemplate({
|
|
39
|
+
filename: "types/graphql-runtime.d.ts",
|
|
40
|
+
getContents: () => `
|
|
41
|
+
import type { schema as userSchema } from "${schemaPath}";
|
|
42
|
+
import type { createContext as userCreateContext } from "${contextPath}";
|
|
43
|
+
|
|
44
|
+
type UserCreateContext = typeof userCreateContext;
|
|
45
|
+
|
|
46
|
+
export type GraphQLContext = Awaited<ReturnType<UserCreateContext>>;
|
|
47
|
+
|
|
48
|
+
declare module "#graphql/schema" {
|
|
49
|
+
export const schema: typeof userSchema;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
declare module "#graphql/context" {
|
|
53
|
+
export const createContext: UserCreateContext;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
declare module "#graphql/runtime" {
|
|
57
|
+
export type { GraphQLContext };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export {};
|
|
61
|
+
`.trim()
|
|
62
|
+
});
|
|
63
|
+
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
64
|
+
nitroConfig.alias ||= {};
|
|
65
|
+
nitroConfig.alias["#graphql/schema"] = schemaPath;
|
|
66
|
+
nitroConfig.alias["#graphql/context"] = contextPath;
|
|
67
|
+
nitroConfig.alias["#graphql/runtime"] = resolve(nuxt.options.buildDir, "types/graphql-runtime.d.ts");
|
|
68
|
+
nitroConfig.virtual ||= {};
|
|
69
|
+
nitroConfig.virtual["#graphql/runtime"] = "export {};";
|
|
70
|
+
});
|
|
40
71
|
const endpoint = options.yoga?.endpoint ?? "/api/graphql";
|
|
41
|
-
|
|
42
|
-
filename: "graphql/handler
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
72
|
+
addServerTemplate({
|
|
73
|
+
filename: "graphql/yoga-handler",
|
|
74
|
+
getContents: () => `
|
|
75
|
+
import { createYoga } from "graphql-yoga";
|
|
76
|
+
import { defineEventHandler, toWebRequest, sendWebResponse } from "h3";
|
|
77
|
+
import { schema } from "#graphql/schema";
|
|
78
|
+
import { createContext } from "#graphql/context";
|
|
79
|
+
|
|
80
|
+
const yoga = createYoga({
|
|
81
|
+
schema,
|
|
82
|
+
graphqlEndpoint: "${endpoint}",
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
export default defineEventHandler(async (event) => {
|
|
86
|
+
const context = await createContext(event);
|
|
87
|
+
const request = toWebRequest(event);
|
|
88
|
+
const response = await yoga.handleRequest(request, context);
|
|
89
|
+
return sendWebResponse(event, response);
|
|
90
|
+
});`.trim()
|
|
91
|
+
});
|
|
92
|
+
addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
|
|
93
|
+
nuxt.hook("listen", (_server, { url }) => {
|
|
94
|
+
logger.success(`GraphQL Yoga available at ${cyan}${url.replace(/\/$/, "") + endpoint}${reset}`);
|
|
48
95
|
});
|
|
49
|
-
|
|
50
|
-
logger.success(`GraphQL Yoga server handler added at ${cyan}${endpoint}${reset}`);
|
|
51
|
-
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
96
|
+
addPlugin(resolve("./runtime/plugin"));
|
|
52
97
|
}
|
|
53
98
|
});
|
|
54
99
|
|
package/package.json
CHANGED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { createYoga } from "graphql-yoga";
|
|
2
|
-
import { defineEventHandler } from "h3";
|
|
3
|
-
import { schema } from "#graphql/schema";
|
|
4
|
-
const yoga = createYoga({
|
|
5
|
-
schema,
|
|
6
|
-
graphqlEndpoint: "{{endpoint}}"
|
|
7
|
-
});
|
|
8
|
-
export default defineEventHandler(async (event) => {
|
|
9
|
-
return yoga.handle(event.node.req, event.node.res);
|
|
10
|
-
});
|