@lewebsimple/nuxt-graphql 0.1.4 → 0.1.5

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.5",
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, 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");
@@ -33,39 +33,30 @@ const module$1 = defineNuxtModule({
33
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
- const schemaPath = findServerFile(layerDirs, "graphql/schema");
37
- const contextPath = findServerFile(layerDirs, "graphql/context");
38
36
  nuxt.hook("nitro:config", (nitroConfig) => {
39
37
  nitroConfig.alias ||= {};
40
- nitroConfig.alias["#graphql/schema"] = schemaPath;
41
- nitroConfig.alias["#graphql/context"] = contextPath;
38
+ nitroConfig.alias["#graphql/schema"] = findServerFile(layerDirs, "graphql/schema");
39
+ nitroConfig.alias["#graphql/context"] = findServerFile(layerDirs, "graphql/context");
42
40
  });
43
41
  const endpoint = options.yoga?.endpoint ?? "/api/graphql";
44
42
  addServerTemplate({
45
43
  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()
44
+ getContents: () => readFileSync(resolve("./templates/yoga-handler.ts"), "utf-8").replace("{{endpoint}}", endpoint)
63
45
  });
64
46
  addServerHandler({ route: endpoint, handler: "graphql/yoga-handler" });
65
47
  nuxt.hook("listen", (_server, { url }) => {
66
48
  logger.success(`GraphQL Yoga available at ${cyan}${url.replace(/\/$/, "") + endpoint}${reset}`);
67
49
  });
68
- addPlugin(resolve("./runtime/plugin"));
50
+ nuxt.options.runtimeConfig.public.graphql = { endpoint };
51
+ addImportsDir(resolve("./runtime/composables"));
52
+ addPlugin(resolve("./runtime/plugins/graphql"));
53
+ addTypeTemplate({
54
+ filename: "types/graphql-client.d.ts",
55
+ getContents: () => readFileSync(resolve("./runtime/types/graphql-client.d.ts"), "utf-8")
56
+ });
57
+ nuxt.hook("prepare:types", ({ references }) => {
58
+ references.push({ path: "./types/graphql-client.d.ts" });
59
+ });
69
60
  }
70
61
  });
71
62
 
@@ -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,13 @@
1
+ import { useNuxtApp, useRequestHeaders } from "#imports";
2
+ export function useGraphQL() {
3
+ const { $graphql } = useNuxtApp();
4
+ if (import.meta.server) {
5
+ const headers = useRequestHeaders(["cookie"]);
6
+ if (headers.cookie) {
7
+ $graphql.setHeader("cookie", headers.cookie);
8
+ }
9
+ }
10
+ return {
11
+ request: (document, variables) => $graphql.request(document, variables)
12
+ };
13
+ }
@@ -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,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,18 @@
1
+ import { createYoga } from "graphql-yoga";
2
+ import { defineEventHandler, toWebRequest, sendWebResponse } from "h3";
3
+ /** @ts-expect-error - No type declarations in module context */
4
+ import { schema } from "#graphql/schema";
5
+ /** @ts-expect-error - No type declarations in module context */
6
+ import { createContext } from "#graphql/context";
7
+
8
+ const yoga = createYoga({
9
+ schema,
10
+ graphqlEndpoint: "{{endpoint}}",
11
+ });
12
+
13
+ export default defineEventHandler(async (event) => {
14
+ const context = await createContext(event);
15
+ const request = toWebRequest(event);
16
+ const response = await yoga.handleRequest(request, context);
17
+ return sendWebResponse(event, response);
18
+ });
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.5",
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
- });