@lewebsimple/nuxt-graphql 0.1.1
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 +89 -0
- package/dist/module.d.mts +12 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +55 -0
- package/dist/runtime/plugin.d.ts +2 -0
- package/dist/runtime/plugin.js +4 -0
- package/dist/runtime/server/handler.d.ts +2 -0
- package/dist/runtime/server/handler.js +10 -0
- package/dist/runtime/server/tsconfig.json +3 -0
- package/dist/runtime/types/graphql-schema.d.ts +5 -0
- package/dist/types.d.mts +3 -0
- package/package.json +59 -0
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# Nuxt GraphQL
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![License][license-src]][license-href]
|
|
6
|
+
[![Nuxt][nuxt-src]][nuxt-href]
|
|
7
|
+
|
|
8
|
+
Opinionated Nuxt module for using GraphQL Yoga on the server and urql as a client.
|
|
9
|
+
|
|
10
|
+
- ✨ [Release Notes](/CHANGELOG.md)
|
|
11
|
+
- 🏀 [Online playground](https://stackblitz.com/github/lewebsimple/nuxt-graphql?file=playground%2Fapp.vue)
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
- 🧘♂️ GraphQL Yoga server handler with user-provided schema
|
|
15
|
+
|
|
16
|
+
## Quick Setup
|
|
17
|
+
|
|
18
|
+
Install the module to your Nuxt application with one command:
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npx nuxi module add @lewebsimple/nuxt-graphql
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Define your GraphQL schema in `server/graphql/schema.ts`:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { createSchema } from "graphql-yoga";
|
|
28
|
+
|
|
29
|
+
export const schema = createSchema({
|
|
30
|
+
typeDefs: /* GraphQL */ `
|
|
31
|
+
type Query {
|
|
32
|
+
hello: String!
|
|
33
|
+
}
|
|
34
|
+
`,
|
|
35
|
+
resolvers: {
|
|
36
|
+
Query: {
|
|
37
|
+
hello: () => "Hello world!",
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
That's it! You can now use Nuxt GraphQL in your Nuxt app ✨
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
## Contribution
|
|
47
|
+
|
|
48
|
+
<details>
|
|
49
|
+
<summary>Local development</summary>
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Install dependencies
|
|
53
|
+
pnpm install
|
|
54
|
+
|
|
55
|
+
# Generate type stubs
|
|
56
|
+
pnpm run dev:prepare
|
|
57
|
+
|
|
58
|
+
# Develop with the playground
|
|
59
|
+
pnpm run dev
|
|
60
|
+
|
|
61
|
+
# Build the playground
|
|
62
|
+
pnpm run dev:build
|
|
63
|
+
|
|
64
|
+
# Run ESLint
|
|
65
|
+
pnpm run lint
|
|
66
|
+
|
|
67
|
+
# Run Vitest
|
|
68
|
+
pnpm run test
|
|
69
|
+
pnpm run test:watch
|
|
70
|
+
|
|
71
|
+
# Release new version
|
|
72
|
+
pnpm run release
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
</details>
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
<!-- Badges -->
|
|
79
|
+
[npm-version-src]: https://img.shields.io/npm/v/@lewebsimple/nuxt-graphql/latest.svg?style=flat&colorA=020420&colorB=00DC82
|
|
80
|
+
[npm-version-href]: https://npmjs.com/package/@lewebsimple/nuxt-graphql
|
|
81
|
+
|
|
82
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@lewebsimple/nuxt-graphql.svg?style=flat&colorA=020420&colorB=00DC82
|
|
83
|
+
[npm-downloads-href]: https://npm.chart.dev/@lewebsimple/nuxt-graphql
|
|
84
|
+
|
|
85
|
+
[license-src]: https://img.shields.io/npm/l/@lewebsimple/nuxt-graphql.svg?style=flat&colorA=020420&colorB=00DC82
|
|
86
|
+
[license-href]: https://npmjs.com/package/@lewebsimple/nuxt-graphql
|
|
87
|
+
|
|
88
|
+
[nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
|
|
89
|
+
[nuxt-href]: https://nuxt.com
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
interface GraphQLYogaConfig {
|
|
4
|
+
endpoint?: string;
|
|
5
|
+
}
|
|
6
|
+
interface ModuleOptions {
|
|
7
|
+
yoga?: GraphQLYogaConfig;
|
|
8
|
+
}
|
|
9
|
+
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
10
|
+
|
|
11
|
+
export { _default as default };
|
|
12
|
+
export type { GraphQLYogaConfig, ModuleOptions };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useLogger, defineNuxtModule, createResolver, getLayerDirectories, addTemplate, addServerHandler, addPlugin } from '@nuxt/kit';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const logger = useLogger("@lewebsimple/nuxt-graphql");
|
|
6
|
+
const cyan = "\x1B[36m";
|
|
7
|
+
const reset = "\x1B[0m";
|
|
8
|
+
|
|
9
|
+
function findServerFile(layerDirs, relativePath) {
|
|
10
|
+
const extensions = ["ts", "mjs"];
|
|
11
|
+
for (const dir of layerDirs) {
|
|
12
|
+
const candidates = extensions.map((ext) => join(dir.server, `${relativePath}.${ext}`));
|
|
13
|
+
const fullPath = candidates.find(existsSync);
|
|
14
|
+
if (fullPath) {
|
|
15
|
+
return fullPath;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
throw new Error(`Could not find required server file ${cyan}${relativePath}${reset} in layers:
|
|
19
|
+
${layerDirs.map(({ server }) => server).join("\n")}`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const module$1 = defineNuxtModule({
|
|
23
|
+
meta: {
|
|
24
|
+
name: "@lewebsimple/nuxt-graphql",
|
|
25
|
+
configKey: "graphql"
|
|
26
|
+
},
|
|
27
|
+
defaults: {
|
|
28
|
+
yoga: {
|
|
29
|
+
endpoint: "/api/graphql"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
async setup(options, nuxt) {
|
|
33
|
+
const resolver = createResolver(import.meta.url);
|
|
34
|
+
const { rootDir, serverDir } = nuxt.options;
|
|
35
|
+
const layerDirs = [...getLayerDirectories(nuxt), { server: serverDir.replace(rootDir, `${rootDir}/playground`) }];
|
|
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") });
|
|
40
|
+
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
|
+
}
|
|
48
|
+
});
|
|
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"));
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
export { module$1 as default };
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
});
|
package/dist/types.d.mts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lewebsimple/nuxt-graphql",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Opinionated Nuxt module for using GraphQL",
|
|
5
|
+
"repository": "lewebsimple/nuxt-graphql",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/types.d.mts",
|
|
11
|
+
"import": "./dist/module.mjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"main": "./dist/module.mjs",
|
|
15
|
+
"typesVersions": {
|
|
16
|
+
"*": {
|
|
17
|
+
".": [
|
|
18
|
+
"./dist/types.d.mts"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"scripts": {
|
|
26
|
+
"prepack": "nuxt-module-build build",
|
|
27
|
+
"dev": "npm run dev:prepare && nuxi dev playground",
|
|
28
|
+
"dev:build": "nuxi build playground",
|
|
29
|
+
"dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground",
|
|
30
|
+
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
|
|
31
|
+
"lint": "eslint .",
|
|
32
|
+
"test": "vitest run",
|
|
33
|
+
"test:watch": "vitest watch",
|
|
34
|
+
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@nuxt/kit": "^4.2.2",
|
|
38
|
+
"graphql": "^16.12.0",
|
|
39
|
+
"graphql-yoga": "^5.17.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@nuxt/devtools": "^3.1.1",
|
|
43
|
+
"@nuxt/eslint": "1.12.1",
|
|
44
|
+
"@nuxt/eslint-config": "^1.12.1",
|
|
45
|
+
"@nuxt/module-builder": "^1.0.2",
|
|
46
|
+
"@nuxt/schema": "^4.2.2",
|
|
47
|
+
"@nuxt/test-utils": "^3.21.0",
|
|
48
|
+
"@types/node": "latest",
|
|
49
|
+
"changelogen": "^0.6.2",
|
|
50
|
+
"eslint": "^9.39.2",
|
|
51
|
+
"nuxt": "^4.2.2",
|
|
52
|
+
"typescript": "~5.9.3",
|
|
53
|
+
"vitest": "^4.0.15",
|
|
54
|
+
"vue-tsc": "^3.1.8"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
}
|
|
59
|
+
}
|