@lewebsimple/nuxt-graphql 0.1.1 → 0.1.2

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/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.2",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addTemplate, addServerHandler, addPlugin } from '@nuxt/kit';
1
+ import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addTypeTemplate, addServerTemplate, addServerHandler, addPlugin } from '@nuxt/kit';
2
2
  import { existsSync, readFileSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
 
@@ -30,25 +30,36 @@ 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: () => 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") });
44
+ });
45
+ nuxt.hook("nitro:config", (nitroConfig) => {
46
+ nitroConfig.alias ||= {};
47
+ nitroConfig.alias["#graphql/schema"] = schemaPath;
48
+ nitroConfig.alias["#graphql/context"] = contextPath;
49
+ nitroConfig.alias["#graphql/runtime"] = resolve(nuxt.options.buildDir, "types/graphql-runtime.d.ts");
50
+ nitroConfig.virtual ||= {};
51
+ nitroConfig.virtual["#graphql/runtime"] = () => "export {};";
52
+ });
40
53
  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
- }
54
+ addServerTemplate({
55
+ filename: "graphql/yoga-handler",
56
+ getContents: () => readFileSync(resolve("./runtime/server/yoga-handler.mjs"), "utf-8").replace("{{endpoint}}", endpoint)
57
+ });
58
+ addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
59
+ nuxt.hook("listen", (_server, { url }) => {
60
+ logger.success(`GraphQL Yoga available at ${cyan}${url.replace(/\/$/, "") + endpoint}${reset}`);
48
61
  });
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"));
62
+ addPlugin(resolve("./runtime/plugin"));
52
63
  }
53
64
  });
54
65
 
@@ -0,0 +1,13 @@
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,3 +1,3 @@
1
1
  {
2
2
  "extends": "../../../.nuxt/tsconfig.server.json",
3
- }
3
+ }
@@ -0,0 +1,16 @@
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
+ });
@@ -0,0 +1,18 @@
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
+ }
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.2",
4
4
  "description": "Opinionated Nuxt module for using GraphQL",
5
5
  "repository": "lewebsimple/nuxt-graphql",
6
6
  "license": "MIT",
@@ -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
- }