@lewebsimple/nuxt-graphql 0.1.4 → 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 +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +15 -24
- 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)
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addServerTemplate, addServerHandler, addPlugin } from '@nuxt/kit';
|
|
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,39 +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
36
|
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
39
37
|
nitroConfig.alias ||= {};
|
|
40
|
-
nitroConfig.alias["#graphql/schema"] =
|
|
41
|
-
nitroConfig.alias["#graphql/context"] =
|
|
38
|
+
nitroConfig.alias["#graphql/schema"] = findServerFile(layerDirs, "graphql/schema");
|
|
39
|
+
nitroConfig.alias["#graphql/context"] = findServerFile(layerDirs, "graphql/context");
|
|
42
40
|
});
|
|
43
41
|
const endpoint = options.yoga?.endpoint ?? "/api/graphql";
|
|
44
42
|
addServerTemplate({
|
|
45
43
|
filename: "graphql/yoga-handler",
|
|
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()
|
|
44
|
+
getContents: () => readFileSync(resolve("./templates/yoga-handler.ts"), "utf-8").replace("{{endpoint}}", endpoint)
|
|
63
45
|
});
|
|
64
46
|
addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
|
|
65
47
|
nuxt.hook("listen", (_server, { url }) => {
|
|
66
48
|
logger.success(`GraphQL Yoga available at ${cyan}${url.replace(/\/$/, "") + endpoint}${reset}`);
|
|
67
49
|
});
|
|
68
|
-
|
|
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
|
+
});
|
|
69
60
|
}
|
|
70
61
|
});
|
|
71
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