@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 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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lewebsimple/nuxt-graphql",
3
3
  "configKey": "graphql",
4
- "version": "0.1.1",
4
+ "version": "0.1.3",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addTemplate, addServerHandler, addPlugin } from '@nuxt/kit';
2
- import { existsSync, readFileSync } from 'node:fs';
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 resolver = createResolver(import.meta.url);
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
- nuxt.options.alias["#graphql/schema"] = schemaPath.replace(/\.(ts|mjs)$/i, "");
38
- logger.success(`GraphQL schema found at ${cyan}${schemaPath}${reset}`);
39
- addTemplate({ filename: "types/graphql-schema.d.ts", src: resolver.resolve("./runtime/types/graphql-schema.d.ts") });
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
- addTemplate({
42
- filename: "graphql/handler.ts",
43
- write: true,
44
- getContents: () => {
45
- const template = readFileSync(resolver.resolve("./runtime/server/handler.ts"), "utf-8");
46
- return template.replace("{{endpoint}}", endpoint);
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
- addServerHandler({ route: endpoint, handler: "#build/graphql/handler.ts" });
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
 
@@ -1,3 +1,3 @@
1
1
  {
2
2
  "extends": "../../../.nuxt/tsconfig.server.json",
3
- }
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lewebsimple/nuxt-graphql",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Opinionated Nuxt module for using GraphQL",
5
5
  "repository": "lewebsimple/nuxt-graphql",
6
6
  "license": "MIT",
@@ -1,2 +0,0 @@
1
- declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
2
- export default _default;
@@ -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
- });
@@ -1,5 +0,0 @@
1
- declare module "#graphql/schema" {
2
- import type { GraphQLSchema } from "graphql";
3
-
4
- export const schema: GraphQLSchema;
5
- }