@lewebsimple/nuxt-graphql 0.1.3 → 0.1.5
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 +4 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +15 -52
- package/dist/runtime/composables/useGraphQL.d.ts +4 -0
- package/dist/runtime/composables/useGraphQL.js +13 -0
- package/dist/runtime/plugins/graphql.d.ts +7 -0
- package/dist/runtime/plugins/graphql.js +12 -0
- package/dist/runtime/types/graphql-client.d.ts +17 -0
- package/dist/templates/yoga-handler.ts +18 -0
- package/package.json +2 -1
- package/dist/runtime/plugin.d.ts +0 -2
- package/dist/runtime/plugin.js +0 -4
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[![License][license-src]][license-href]
|
|
6
6
|
[![Nuxt][nuxt-src]][nuxt-href]
|
|
7
7
|
|
|
8
|
-
Opinionated Nuxt module for using GraphQL Yoga
|
|
8
|
+
Opinionated Nuxt module for using GraphQL Yoga with graphql-request / graphql-sse.
|
|
9
9
|
|
|
10
10
|
- ✨ [Release Notes](/CHANGELOG.md)
|
|
11
11
|
- 🏀 [Online playground](https://stackblitz.com/github/lewebsimple/nuxt-graphql?file=playground%2Fapp.vue)
|
|
@@ -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!
|
|
@@ -51,6 +52,7 @@ export async function createContext(_event: H3Event) {
|
|
|
51
52
|
};
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
export type GraphQLContext = Awaited<ReturnType<typeof createContext>>;
|
|
54
56
|
```
|
|
55
57
|
|
|
56
58
|
That's it! You can now use Nuxt GraphQL in your Nuxt app ✨
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useLogger, defineNuxtModule, createResolver, getLayerDirectories,
|
|
2
|
-
import { existsSync } from 'node:fs';
|
|
1
|
+
import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addServerTemplate, addServerHandler, addImportsDir, addPlugin, addTypeTemplate } from '@nuxt/kit';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
|
|
5
5
|
const logger = useLogger("@lewebsimple/nuxt-graphql");
|
|
@@ -33,67 +33,30 @@ const module$1 = defineNuxtModule({
|
|
|
33
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
|
-
const schemaPath = findServerFile(layerDirs, "graphql/schema");
|
|
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
36
|
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
64
37
|
nitroConfig.alias ||= {};
|
|
65
|
-
nitroConfig.alias["#graphql/schema"] =
|
|
66
|
-
nitroConfig.alias["#graphql/context"] =
|
|
67
|
-
nitroConfig.alias["#graphql/runtime"] = resolve(nuxt.options.buildDir, "types/graphql-runtime.d.ts");
|
|
68
|
-
nitroConfig.virtual ||= {};
|
|
69
|
-
nitroConfig.virtual["#graphql/runtime"] = "export {};";
|
|
38
|
+
nitroConfig.alias["#graphql/schema"] = findServerFile(layerDirs, "graphql/schema");
|
|
39
|
+
nitroConfig.alias["#graphql/context"] = findServerFile(layerDirs, "graphql/context");
|
|
70
40
|
});
|
|
71
41
|
const endpoint = options.yoga?.endpoint ?? "/api/graphql";
|
|
72
42
|
addServerTemplate({
|
|
73
43
|
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()
|
|
44
|
+
getContents: () => readFileSync(resolve("./templates/yoga-handler.ts"), "utf-8").replace("{{endpoint}}", endpoint)
|
|
91
45
|
});
|
|
92
46
|
addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
|
|
93
47
|
nuxt.hook("listen", (_server, { url }) => {
|
|
94
48
|
logger.success(`GraphQL Yoga available at ${cyan}${url.replace(/\/$/, "") + endpoint}${reset}`);
|
|
95
49
|
});
|
|
96
|
-
|
|
50
|
+
nuxt.options.runtimeConfig.public.graphql = { endpoint };
|
|
51
|
+
addImportsDir(resolve("./runtime/composables"));
|
|
52
|
+
addPlugin(resolve("./runtime/plugins/graphql"));
|
|
53
|
+
addTypeTemplate({
|
|
54
|
+
filename: "types/graphql-client.d.ts",
|
|
55
|
+
getContents: () => readFileSync(resolve("./runtime/types/graphql-client.d.ts"), "utf-8")
|
|
56
|
+
});
|
|
57
|
+
nuxt.hook("prepare:types", ({ references }) => {
|
|
58
|
+
references.push({ path: "./types/graphql-client.d.ts" });
|
|
59
|
+
});
|
|
97
60
|
}
|
|
98
61
|
});
|
|
99
62
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useNuxtApp, useRequestHeaders } from "#imports";
|
|
2
|
+
export function useGraphQL() {
|
|
3
|
+
const { $graphql } = useNuxtApp();
|
|
4
|
+
if (import.meta.server) {
|
|
5
|
+
const headers = useRequestHeaders(["cookie"]);
|
|
6
|
+
if (headers.cookie) {
|
|
7
|
+
$graphql.setHeader("cookie", headers.cookie);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return {
|
|
11
|
+
request: (document, variables) => $graphql.request(document, variables)
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GraphQLClient } from "graphql-request";
|
|
2
|
+
import { defineNuxtPlugin, useRequestURL, useRuntimeConfig } from "#imports";
|
|
3
|
+
export default defineNuxtPlugin(() => {
|
|
4
|
+
const config = useRuntimeConfig();
|
|
5
|
+
const { origin } = useRequestURL();
|
|
6
|
+
const client = new GraphQLClient(`${origin}${config.public.graphql.endpoint}`);
|
|
7
|
+
return {
|
|
8
|
+
provide: {
|
|
9
|
+
graphql: client
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { GraphQLClient } from "graphql-request";
|
|
2
|
+
|
|
3
|
+
declare module "#app" {
|
|
4
|
+
interface NuxtApp {
|
|
5
|
+
$graphql: GraphQLClient;
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare module "nuxt/schema" {
|
|
10
|
+
interface PublicRuntimeConfig {
|
|
11
|
+
graphql: {
|
|
12
|
+
endpoint: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createYoga } from "graphql-yoga";
|
|
2
|
+
import { defineEventHandler, toWebRequest, sendWebResponse } from "h3";
|
|
3
|
+
/** @ts-expect-error - No type declarations in module context */
|
|
4
|
+
import { schema } from "#graphql/schema";
|
|
5
|
+
/** @ts-expect-error - No type declarations in module context */
|
|
6
|
+
import { createContext } from "#graphql/context";
|
|
7
|
+
|
|
8
|
+
const yoga = createYoga({
|
|
9
|
+
schema,
|
|
10
|
+
graphqlEndpoint: "{{endpoint}}",
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export default defineEventHandler(async (event) => {
|
|
14
|
+
const context = await createContext(event);
|
|
15
|
+
const request = toWebRequest(event);
|
|
16
|
+
const response = await yoga.handleRequest(request, context);
|
|
17
|
+
return sendWebResponse(event, response);
|
|
18
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lewebsimple/nuxt-graphql",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Opinionated Nuxt module for using GraphQL",
|
|
5
5
|
"repository": "lewebsimple/nuxt-graphql",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@nuxt/kit": "^4.2.2",
|
|
38
38
|
"graphql": "^16.12.0",
|
|
39
|
+
"graphql-request": "^7.4.0",
|
|
39
40
|
"graphql-yoga": "^5.17.1"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
package/dist/runtime/plugin.d.ts
DELETED
package/dist/runtime/plugin.js
DELETED