@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 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 on the server and urql as a client.
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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lewebsimple/nuxt-graphql",
3
3
  "configKey": "graphql",
4
- "version": "0.1.4",
4
+ "version": "0.1.6",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
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
- throw new Error(`Could not find required server file ${cyan}${relativePath}${reset} in layers:
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"] = schemaPath;
41
- nitroConfig.alias["#graphql/context"] = contextPath;
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
- addPlugin(resolve("./runtime/plugin"));
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,4 @@
1
+ import type { RequestDocument, Variables } from "graphql-request";
2
+ export declare function useGraphQL(): {
3
+ request: <T>(document: RequestDocument, variables?: Variables) => any;
4
+ };
@@ -0,0 +1,7 @@
1
+ import { useNuxtApp } from "#imports";
2
+ export function useGraphQL() {
3
+ const { $graphql } = useNuxtApp();
4
+ return {
5
+ request: (document, variables) => $graphql.request(document, variables)
6
+ };
7
+ }
@@ -0,0 +1,7 @@
1
+ import { GraphQLClient } from "graphql-request";
2
+ declare const _default: import("nuxt/app").Plugin<{
3
+ graphql: GraphQLClient;
4
+ }> & import("nuxt/app").ObjectPlugin<{
5
+ graphql: GraphQLClient;
6
+ }>;
7
+ export default _default;
@@ -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,7 @@
1
+ import type { H3Event } from "h3";
2
+ export declare const createContext: (event: H3Event) => {
3
+ event: H3Event<import("h3").EventHandlerRequest>;
4
+ };
5
+ export type GraphQLContext = {
6
+ event: H3Event;
7
+ };
@@ -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.4",
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": {
@@ -1,2 +0,0 @@
1
- declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
2
- export default _default;
@@ -1,4 +0,0 @@
1
- import { defineNuxtPlugin } from "#app";
2
- export default defineNuxtPlugin((_nuxtApp) => {
3
- console.log("\u2728 @lewebsimple/nuxt-graphql plugin loaded");
4
- });