@lewebsimple/nuxt-graphql 0.1.2 → 0.1.4
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 +17 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +19 -13
- package/package.json +1 -1
- package/dist/runtime/server/shims.d.ts +0 -13
- package/dist/runtime/server/yoga-handler.d.mts +0 -2
- package/dist/runtime/server/yoga-handler.js +0 -16
- package/dist/runtime/types/graphql-runtime.d.ts +0 -18
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
|
|
|
@@ -25,8 +25,9 @@ Define your GraphQL schema in `server/graphql/schema.ts`:
|
|
|
25
25
|
|
|
26
26
|
```ts
|
|
27
27
|
import { createSchema } from "graphql-yoga";
|
|
28
|
+
import type { GraphQLContext } from "./context";
|
|
28
29
|
|
|
29
|
-
export const schema = createSchema({
|
|
30
|
+
export const schema = createSchema<GraphQLContext>({
|
|
30
31
|
typeDefs: /* GraphQL */ `
|
|
31
32
|
type Query {
|
|
32
33
|
hello: String!
|
|
@@ -40,6 +41,20 @@ export const schema = createSchema({
|
|
|
40
41
|
});
|
|
41
42
|
```
|
|
42
43
|
|
|
44
|
+
Define your GraphQL context in `server/graphql/context.ts`:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import type { H3Event } from "h3";
|
|
48
|
+
|
|
49
|
+
export async function createContext(_event: H3Event) {
|
|
50
|
+
return {
|
|
51
|
+
foo: "bar",
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type GraphQLContext = Awaited<ReturnType<typeof createContext>>;
|
|
56
|
+
```
|
|
57
|
+
|
|
43
58
|
That's it! You can now use Nuxt GraphQL in your Nuxt app ✨
|
|
44
59
|
|
|
45
60
|
|
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, 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");
|
|
@@ -35,25 +35,31 @@ const module$1 = defineNuxtModule({
|
|
|
35
35
|
const layerDirs = [...getLayerDirectories(nuxt), { server: serverDir.replace(rootDir, `${rootDir}/playground`) }];
|
|
36
36
|
const schemaPath = findServerFile(layerDirs, "graphql/schema");
|
|
37
37
|
const contextPath = findServerFile(layerDirs, "graphql/context");
|
|
38
|
-
addTypeTemplate({
|
|
39
|
-
filename: "types/graphql-runtime.d.ts",
|
|
40
|
-
getContents: () => readFileSync(resolve("./runtime/types/graphql-runtime.d.ts"), "utf-8").replace("{{schemaPath}}", schemaPath.replace(/\.ts$/, "")).replace("{{contextPath}}", contextPath.replace(/\.ts$/, ""))
|
|
41
|
-
});
|
|
42
|
-
nuxt.hook("nitro:prepare:types", ({ references }) => {
|
|
43
|
-
references.push({ path: resolve(nuxt.options.buildDir, "types/graphql-runtime.d.ts") });
|
|
44
|
-
});
|
|
45
38
|
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
46
39
|
nitroConfig.alias ||= {};
|
|
47
40
|
nitroConfig.alias["#graphql/schema"] = schemaPath;
|
|
48
41
|
nitroConfig.alias["#graphql/context"] = contextPath;
|
|
49
|
-
nitroConfig.alias["#graphql/runtime"] = resolve(nuxt.options.buildDir, "types/graphql-runtime.d.ts");
|
|
50
|
-
nitroConfig.virtual ||= {};
|
|
51
|
-
nitroConfig.virtual["#graphql/runtime"] = () => "export {};";
|
|
52
42
|
});
|
|
53
43
|
const endpoint = options.yoga?.endpoint ?? "/api/graphql";
|
|
54
44
|
addServerTemplate({
|
|
55
45
|
filename: "graphql/yoga-handler",
|
|
56
|
-
getContents: () =>
|
|
46
|
+
getContents: () => `
|
|
47
|
+
import { createYoga } from "graphql-yoga";
|
|
48
|
+
import { defineEventHandler, toWebRequest, sendWebResponse } from "h3";
|
|
49
|
+
import { schema } from "#graphql/schema";
|
|
50
|
+
import { createContext } from "#graphql/context";
|
|
51
|
+
|
|
52
|
+
const yoga = createYoga({
|
|
53
|
+
schema,
|
|
54
|
+
graphqlEndpoint: "${endpoint}",
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
export default defineEventHandler(async (event) => {
|
|
58
|
+
const context = await createContext(event);
|
|
59
|
+
const request = toWebRequest(event);
|
|
60
|
+
const response = await yoga.handleRequest(request, context);
|
|
61
|
+
return sendWebResponse(event, response);
|
|
62
|
+
});`.trim()
|
|
57
63
|
});
|
|
58
64
|
addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
|
|
59
65
|
nuxt.hook("listen", (_server, { url }) => {
|
package/package.json
CHANGED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
declare module "#graphql/schema" {
|
|
2
|
-
import type { GraphQLSchema } from "graphql";
|
|
3
|
-
|
|
4
|
-
const schema: GraphQLSchema;
|
|
5
|
-
export { schema };
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
declare module "#graphql/context" {
|
|
9
|
-
import type { H3Event } from "h3";
|
|
10
|
-
|
|
11
|
-
function createContext(event: H3Event): Promise<Record<string, unknown>>;
|
|
12
|
-
export { createContext };
|
|
13
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { createYoga } from "graphql-yoga";
|
|
2
|
-
import { defineEventHandler, toWebRequest, sendWebResponse } from "h3";
|
|
3
|
-
import { schema } from "#graphql/schema";
|
|
4
|
-
import { createContext } from "#graphql/context";
|
|
5
|
-
|
|
6
|
-
const yoga = createYoga({
|
|
7
|
-
schema,
|
|
8
|
-
graphqlEndpoint: "{{endpoint}}",
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
export default defineEventHandler(async (event) => {
|
|
12
|
-
const context = await createContext(event);
|
|
13
|
-
const request = toWebRequest(event);
|
|
14
|
-
const response = await yoga.handleRequest(request, context);
|
|
15
|
-
return sendWebResponse(event, response);
|
|
16
|
-
});
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type * as UserSchema from "{{schemaPath}}";
|
|
2
|
-
import type * as UserContext from "{{contextPath}}";
|
|
3
|
-
|
|
4
|
-
type UserCreateContext = typeof UserContext.createContext;
|
|
5
|
-
|
|
6
|
-
export type GraphQLContext = Awaited<ReturnType<UserCreateContext>>;
|
|
7
|
-
|
|
8
|
-
declare module "#graphql/schema" {
|
|
9
|
-
export const schema: UserSchema.schema;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
declare module "#graphql/context" {
|
|
13
|
-
export const createContext: UserCreateContext;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
declare module "#graphql/runtime" {
|
|
17
|
-
export type { GraphQLContext };
|
|
18
|
-
}
|