@lewebsimple/nuxt-graphql 0.1.4 → 0.1.6
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 +19 -26
- package/dist/runtime/composables/useGraphQL.d.ts +4 -0
- package/dist/runtime/composables/useGraphQL.js +7 -0
- package/dist/runtime/plugins/graphql.d.ts +7 -0
- package/dist/runtime/plugins/graphql.js +12 -0
- package/dist/runtime/server/default-context.d.ts +7 -0
- package/dist/runtime/server/default-context.js +1 -0
- package/dist/runtime/types/graphql-client.d.ts +17 -0
- package/dist/templates/yoga-handler.mjs +24 -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,12 +1,12 @@
|
|
|
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");
|
|
6
6
|
const cyan = "\x1B[36m";
|
|
7
7
|
const reset = "\x1B[0m";
|
|
8
8
|
|
|
9
|
-
function findServerFile(layerDirs, relativePath) {
|
|
9
|
+
function findServerFile(layerDirs, relativePath, isRequired = false) {
|
|
10
10
|
const extensions = ["ts", "mjs"];
|
|
11
11
|
for (const dir of layerDirs) {
|
|
12
12
|
const candidates = extensions.map((ext) => join(dir.server, `${relativePath}.${ext}`));
|
|
@@ -15,8 +15,10 @@ function findServerFile(layerDirs, relativePath) {
|
|
|
15
15
|
return fullPath;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
if (isRequired) {
|
|
19
|
+
throw new Error(`Could not find required server file ${cyan}${relativePath}.{${extensions.join(",")}}${reset} in the following layers:
|
|
19
20
|
${layerDirs.map(({ server }) => server).join("\n")}`);
|
|
21
|
+
}
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
const module$1 = defineNuxtModule({
|
|
@@ -33,39 +35,30 @@ const module$1 = defineNuxtModule({
|
|
|
33
35
|
const { resolve } = createResolver(import.meta.url);
|
|
34
36
|
const { rootDir, serverDir } = nuxt.options;
|
|
35
37
|
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
38
|
nuxt.hook("nitro:config", (nitroConfig) => {
|
|
39
39
|
nitroConfig.alias ||= {};
|
|
40
|
-
nitroConfig.alias["#graphql/schema"] =
|
|
41
|
-
nitroConfig.alias["#graphql/context"] =
|
|
40
|
+
nitroConfig.alias["#graphql/schema"] = findServerFile(layerDirs, "graphql/schema", true);
|
|
41
|
+
nitroConfig.alias["#graphql/context"] = findServerFile(layerDirs, "graphql/context") || resolve("./runtime/server/default-context.ts");
|
|
42
42
|
});
|
|
43
43
|
const endpoint = options.yoga?.endpoint ?? "/api/graphql";
|
|
44
44
|
addServerTemplate({
|
|
45
45
|
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()
|
|
46
|
+
getContents: () => readFileSync(resolve("./templates/yoga-handler.mjs"), "utf-8").replace("{{endpoint}}", endpoint)
|
|
63
47
|
});
|
|
64
48
|
addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
|
|
65
49
|
nuxt.hook("listen", (_server, { url }) => {
|
|
66
50
|
logger.success(`GraphQL Yoga available at ${cyan}${url.replace(/\/$/, "") + endpoint}${reset}`);
|
|
67
51
|
});
|
|
68
|
-
|
|
52
|
+
nuxt.options.runtimeConfig.public.graphql = { endpoint };
|
|
53
|
+
addImportsDir(resolve("./runtime/composables"));
|
|
54
|
+
addPlugin(resolve("./runtime/plugins/graphql"));
|
|
55
|
+
addTypeTemplate({
|
|
56
|
+
filename: "types/graphql-client.d.ts",
|
|
57
|
+
getContents: () => readFileSync(resolve("./runtime/types/graphql-client.d.ts"), "utf-8")
|
|
58
|
+
});
|
|
59
|
+
nuxt.hook("prepare:types", ({ references }) => {
|
|
60
|
+
references.push({ path: "./types/graphql-client.d.ts" });
|
|
61
|
+
});
|
|
69
62
|
}
|
|
70
63
|
});
|
|
71
64
|
|
|
@@ -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 @@
|
|
|
1
|
+
export const createContext = (event) => ({ event });
|
|
@@ -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,24 @@
|
|
|
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
|
+
let yoga = null;
|
|
7
|
+
|
|
8
|
+
function getYoga() {
|
|
9
|
+
if (!yoga) {
|
|
10
|
+
yoga = createYoga({
|
|
11
|
+
schema,
|
|
12
|
+
graphqlEndpoint: "{{endpoint}}",
|
|
13
|
+
fetchAPI: globalThis,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return yoga;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default defineEventHandler(async (event) => {
|
|
20
|
+
const request = toWebRequest(event);
|
|
21
|
+
const context = await createContext(event);
|
|
22
|
+
const response = await getYoga().handleRequest(request, context);
|
|
23
|
+
return sendWebResponse(event, response);
|
|
24
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lewebsimple/nuxt-graphql",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
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