@lewebsimple/nuxt-graphql 0.1.2 → 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.2",
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
1
  import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addTypeTemplate, addServerTemplate, addServerHandler, addPlugin } from '@nuxt/kit';
2
- import { existsSync, readFileSync } from 'node:fs';
2
+ import { existsSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
 
5
5
  const logger = useLogger("@lewebsimple/nuxt-graphql");
@@ -37,10 +37,28 @@ const module$1 = defineNuxtModule({
37
37
  const contextPath = findServerFile(layerDirs, "graphql/context");
38
38
  addTypeTemplate({
39
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") });
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()
44
62
  });
45
63
  nuxt.hook("nitro:config", (nitroConfig) => {
46
64
  nitroConfig.alias ||= {};
@@ -48,12 +66,28 @@ const module$1 = defineNuxtModule({
48
66
  nitroConfig.alias["#graphql/context"] = contextPath;
49
67
  nitroConfig.alias["#graphql/runtime"] = resolve(nuxt.options.buildDir, "types/graphql-runtime.d.ts");
50
68
  nitroConfig.virtual ||= {};
51
- nitroConfig.virtual["#graphql/runtime"] = () => "export {};";
69
+ nitroConfig.virtual["#graphql/runtime"] = "export {};";
52
70
  });
53
71
  const endpoint = options.yoga?.endpoint ?? "/api/graphql";
54
72
  addServerTemplate({
55
73
  filename: "graphql/yoga-handler",
56
- getContents: () => readFileSync(resolve("./runtime/server/yoga-handler.mjs"), "utf-8").replace("{{endpoint}}", endpoint)
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()
57
91
  });
58
92
  addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
59
93
  nuxt.hook("listen", (_server, { url }) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lewebsimple/nuxt-graphql",
3
- "version": "0.1.2",
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,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,2 +0,0 @@
1
- declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
2
- export default _default;
@@ -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
- }