create-lykosjs 0.0.0-stage → 0.0.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.
Files changed (50) hide show
  1. package/README.md +163 -2
  2. package/bootstrap/.env.example +4 -0
  3. package/bootstrap/.github/workflows/ci.yml +1 -0
  4. package/bootstrap/.prettierrc +14 -0
  5. package/bootstrap/AGENTS.md +94 -0
  6. package/bootstrap/CLAUDE.md +46 -0
  7. package/bootstrap/README.md +73 -0
  8. package/bootstrap/gitignore.template +34 -0
  9. package/bootstrap/package.json +25 -0
  10. package/bootstrap/src/graphql/graphql.executor.ts +54 -0
  11. package/bootstrap/src/graphql/graphql.handler.ts +18 -0
  12. package/bootstrap/src/graphql/graphql.resolvers.ts +14 -0
  13. package/bootstrap/src/graphql/graphql.schema.ts +58 -0
  14. package/bootstrap/src/launch.ts +36 -0
  15. package/bootstrap/src/modules/users/graphql-schema/users.graphql +29 -0
  16. package/bootstrap/src/modules/users/tests/users.resolver.test.ts +161 -0
  17. package/bootstrap/src/modules/users/tests/users.service.test.ts +36 -0
  18. package/bootstrap/src/modules/users/users.module.ts +8 -0
  19. package/bootstrap/src/modules/users/users.resolver.ts +42 -0
  20. package/bootstrap/src/modules/users/users.service.ts +33 -0
  21. package/bootstrap/src/modules.ts +3 -0
  22. package/bootstrap/tsconfig.build.json +22 -0
  23. package/bootstrap/tsconfig.json +30 -0
  24. package/index.js +154 -0
  25. package/lib/args.js +55 -0
  26. package/lib/prompt.js +90 -0
  27. package/lib/scaffold.js +88 -0
  28. package/lib/templates.js +32 -0
  29. package/package.json +17 -4
  30. package/template/.env.example +4 -0
  31. package/template/.github/workflows/ci.yml +1 -0
  32. package/template/.prettierrc +14 -0
  33. package/template/AGENTS.md +81 -0
  34. package/template/CLAUDE.md +40 -0
  35. package/template/README.md +41 -0
  36. package/template/gitignore.template +34 -0
  37. package/template/package.json +21 -0
  38. package/template/src/launch.ts +17 -0
  39. package/template/src/modules/users/rules/create.ts +7 -0
  40. package/template/src/modules/users/rules/update.ts +3 -0
  41. package/template/src/modules/users/schemas/user.schema.ts +1 -0
  42. package/template/src/modules/users/tests/users.controller.test.ts +19 -0
  43. package/template/src/modules/users/tests/users.service.test.ts +18 -0
  44. package/template/src/modules/users/users.controller.ts +36 -0
  45. package/template/src/modules/users/users.module.ts +12 -0
  46. package/template/src/modules/users/users.route.ts +30 -0
  47. package/template/src/modules/users/users.service.ts +22 -0
  48. package/template/src/modules.ts +3 -0
  49. package/template/tsconfig.build.json +22 -0
  50. package/template/tsconfig.json +30 -0
package/README.md CHANGED
@@ -1,3 +1,164 @@
1
- # Temporary Holding Version
1
+ # Create LykosJS 🐺
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ <h1>
4
+ <img src="https://res.cloudinary.com/dgagbheuj/image/upload/v1795227129/koakwx1uyjf68edpilc8.png" alt="LykosJS Logo" width="48" height="48" align="absmiddle" />
5
+ LykosJS
6
+ </h1>
7
+
8
+ **Create your next backend application with LykosJS.**
9
+
10
+ `create-lykosjs` is the official CLI scaffolding tool for generating a new [LykosJS](https://github.com/lykosjs/lykos) application with a ready-to-use modular structure.
11
+
12
+ ## Features
13
+
14
+ - **Ready-to-use structure** — Start with an organized application layout.
15
+ - **Modular architecture** — Keep controllers, services, routes, modules, rules, and schemas organized.
16
+ - **TypeScript support** — Build your backend with TypeScript.
17
+ - **Preconfigured tooling** — Includes a TypeScript configuration and Prettier settings.
18
+ - **Quick setup** — Generate a project with a single command.
19
+
20
+ ## Getting Started
21
+
22
+ ### Using npm
23
+
24
+ ```bash
25
+ npx create-lykosjs my-app
26
+ ```
27
+
28
+ ### Using Bun
29
+
30
+ ```bash
31
+ bun create lykosjs my-app
32
+ ```
33
+
34
+ Replace `my-app` with your desired project name.
35
+
36
+ You will be asked which kind of API you want to start with:
37
+
38
+ ```text
39
+ Which API are you ready to build with LykosJS? 🐺
40
+ ❯ REST API — controllers, routes & validation rules
41
+ GraphQL — schema-first with resolvers
42
+ ```
43
+
44
+ Use the arrow keys (or `1` / `2`) and press Enter. Press Esc or Ctrl+C to cancel — nothing is created.
45
+
46
+ ### Skip the prompt
47
+
48
+ ```bash
49
+ bun create lykosjs my-app --template rest
50
+ bun create lykosjs my-app --template graphql
51
+ ```
52
+
53
+ `--rest` and `--graphql` work as shortcuts. In a non-interactive terminal (CI, pipes) the REST template is used unless you pass a template flag.
54
+
55
+ ## Project Structure
56
+
57
+ ### REST API
58
+
59
+ The generated project follows a modular structure similar to this:
60
+
61
+ ```text
62
+ my-app/
63
+ ├── src/
64
+ │ ├── launch.ts
65
+ |── modules.ts
66
+ │ └── modules/
67
+ │ └── users/
68
+ │ ├── users.controller.ts
69
+ │ ├── users.module.ts
70
+ │ ├── users.route.ts
71
+ │ ├── users.service.ts
72
+ │ ├── rules/
73
+ │ │ ├── create.ts
74
+ │ │ └── update.ts
75
+ │ ├── schemas/
76
+ │ │ └── user.schema.ts
77
+ │ └── tests/
78
+ │ └── users.controller.test.ts
79
+ └── users.service.test.ts
80
+ ├── package.json
81
+ ├── tsconfig.json
82
+ ├── .prettierrc
83
+ └── .gitignore
84
+ └── .tsconfig.json
85
+ └── .tsconfig.build.json
86
+ ```
87
+
88
+ ### GraphQL
89
+
90
+ ```text
91
+ my-app/
92
+ ├── src/
93
+ │ ├── launch.ts
94
+ │ ├── modules.ts
95
+ │ ├── graphql/
96
+ │ │ ├── graphql.executor.ts
97
+ │ │ ├── graphql.handler.ts
98
+ │ │ ├── graphql.resolvers.ts
99
+ │ │ └── graphql.schema.ts
100
+ │ └── modules/
101
+ │ └── users/
102
+ │ ├── users.module.ts
103
+ │ ├── users.resolver.ts
104
+ │ ├── users.service.ts
105
+ │ ├── graphql-schema/
106
+ │ │ └── users.graphql
107
+ │ └── tests/
108
+ │ ├── users.resolver.test.ts
109
+ │ └── users.service.test.ts
110
+ └── package.json
111
+ ```
112
+
113
+ The GraphQL endpoint is `POST /graphql` on port 4000.
114
+
115
+ ## Run Your Application
116
+
117
+ After generating your project, enter its directory:
118
+
119
+ ```bash
120
+ cd my-app
121
+ ```
122
+
123
+ Install the dependencies and follow the scripts provided in the generated `package.json`.
124
+
125
+ If you use Bun:
126
+
127
+ ```bash
128
+ bun install
129
+ ```
130
+
131
+ Then run the development command defined in your project's `package.json`.
132
+
133
+ ## Built with LykosJS
134
+
135
+ A lightweight, modular backend framework built for speed and DX.
136
+
137
+ - **Framework:** [LykosJS](https://github.com/lykosjs/lykos)
138
+ - **Runtime:** [Bun](https://bun.sh/)
139
+ - **Language:** TypeScript
140
+
141
+ ## Links
142
+
143
+ - [LykosJS on GitHub](https://github.com/lykosjs/lykos)
144
+ - [Bun Documentation](https://bun.sh/docs)
145
+
146
+ ## Development
147
+
148
+ ```bash
149
+ bun test
150
+ ```
151
+
152
+ The REST template lives in `template/` and the GraphQL template in `bootstrap/`; they are fully independent. The CLI logic is in `index.js` and `lib/`.
153
+
154
+ ## License
155
+
156
+ MIT
157
+
158
+ <p align="center">
159
+ <img
160
+ src="https://res.cloudinary.com/dgagbheuj/image/upload/v1795313052/muvd4yk1qmoa3kmdql43.png"
161
+ alt="🐺 LykosJS — Built for Speed. Designed for Scale.."
162
+ width="100%"
163
+ />
164
+ </p>
@@ -0,0 +1,4 @@
1
+ # Environment Variables Template
2
+ # Copy this file to `.env` and configure the values for your local environment.
3
+ # Never commit your actual `.env` file or expose sensitive credentials.
4
+ # Add any new environment variables here to document the required configuration.
@@ -0,0 +1 @@
1
+ # GitHub Actions workflow for running tests and building the project on every push and pull request.
@@ -0,0 +1,14 @@
1
+ {
2
+ "singleQuote": true,
3
+ "semi": false,
4
+ "trailingComma": "all",
5
+ "printWidth": 100,
6
+ "tabWidth": 2,
7
+ "useTabs": false,
8
+ "bracketSpacing": true,
9
+ "bracketSameLine": false,
10
+ "arrowParens": "always",
11
+ "endOfLine": "lf",
12
+ "quoteProps": "as-needed",
13
+ "proseWrap": "preserve"
14
+ }
@@ -0,0 +1,94 @@
1
+ # AGENTS.md
2
+
3
+ ## Project Overview
4
+
5
+ This project is built with LykosJS, a lightweight, modular backend framework built for speed and DX.
6
+
7
+ Follow the existing project structure and conventions. Prefer simple, maintainable solutions over unnecessary abstractions.
8
+
9
+ ## Technology Stack
10
+
11
+ - Runtime and package manager: Bun
12
+ - Language: TypeScript
13
+ - backend framework: LykosJS
14
+ - API: GraphQL (`graphql` package) at `POST /graphql`
15
+ - Tests: Bun's built-in test runner (`bun:test`)
16
+
17
+ Use Bun for installing dependencies, running scripts, and executing tests.
18
+
19
+ ## Project Structure
20
+
21
+ - `src/index.ts`: Main application entry point.
22
+ - `src/modules.ts`: Centralized module registration.
23
+ - `src/modules/`: Feature modules.
24
+ - `src/modules/users/`: Example Users module.
25
+ - `src/modules/users/users.resolver.ts`: GraphQL resolver logic.
26
+ - `src/modules/users/users.service.ts`: Service logic.
27
+ - `src/modules/users/users.module.ts`: Module configuration.
28
+ - `src/modules/users/graphql-schema/`: The module's GraphQL schema (`*.graphql`).
29
+ - `src/graphql/graphql.schema.ts`: Loads and merges every module schema.
30
+ - `src/graphql/graphql.resolvers.ts`: List of registered resolvers.
31
+ - `src/graphql/graphql.executor.ts`: Builds the schema and executes queries.
32
+ - `src/graphql/graphql.handler.ts`: Validates the request body.
33
+
34
+ Follow the actual files in the repository if the structure changes.
35
+
36
+ ## Adding a GraphQL Feature
37
+
38
+ 1. Create `src/modules/<feature>/` with a module, service and resolver, following the `users` module.
39
+ 2. Add `graphql-schema/<feature>.graphql`. `*.graphql` files under `src/modules` are loaded automatically, and `Query`/`Mutation` are merged across modules.
40
+ 3. Register the module in `src/modules.ts`.
41
+ 4. Add `app.resolve(<Feature>Module, <Feature>Resolver)` to `src/graphql/graphql.resolvers.ts`.
42
+ 5. Name resolver methods exactly like the Query/Mutation fields they implement.
43
+
44
+ Do not edit the files in `src/graphql/` just to add a feature, except `graphql.resolvers.ts`.
45
+
46
+ ## Development Rules
47
+
48
+ 1. Inspect relevant files before making changes.
49
+ 2. Follow existing LykosJS APIs and established project patterns.
50
+ 3. Do not invent framework methods, decorators, middleware, or configuration options.
51
+ 4. Use TypeScript types instead of `any` whenever possible.
52
+ 5. Keep modules focused and responsibilities clearly separated.
53
+ 6. Validate input at the appropriate application boundary.
54
+ 7. Handle errors consistently with the existing application.
55
+ 8. Avoid unrelated refactoring and unnecessary dependencies.
56
+ 9. Preserve existing behavior unless the requested change requires otherwise.
57
+ 10. Never claim that tests or builds passed unless they were actually executed.
58
+
59
+ ## Commands
60
+
61
+ Check `package.json` before assuming a script exists.
62
+
63
+ Common commands may include:
64
+
65
+ - `bun install` — install dependencies.
66
+ - `bun run dev` — start the development server.
67
+ - `bun run build` — build the project, if configured.
68
+ - `bun test` — run tests.
69
+
70
+ Use only commands supported by the current project configuration.
71
+
72
+ ## Testing
73
+
74
+ - Use `bun:test` for unit tests.
75
+ - Test observable behavior, not just whether methods exist.
76
+ - Include success cases, failure cases, and relevant edge cases.
77
+ - Run the relevant tests after modifying code when possible.
78
+ - Report any tests that could not be executed.
79
+
80
+ ## Dependencies and Security
81
+
82
+ - Avoid adding dependencies when existing tools are sufficient.
83
+ - Never hardcode secrets, tokens, or credentials.
84
+ - Validate untrusted input.
85
+ - Do not weaken authentication, authorization, or validation to make tests pass.
86
+
87
+ ## Final Response
88
+
89
+ When completing a task, summarize:
90
+
91
+ 1. What changed.
92
+ 2. Which files were affected.
93
+ 3. Which tests or build commands were actually run.
94
+ 4. Any remaining limitations or follow-up work.
@@ -0,0 +1,46 @@
1
+ # Claude Code Instructions
2
+
3
+ ## Start Here
4
+
5
+ Read `AGENTS.md` before making changes. Follow its project conventions and requirements throughout the task.
6
+
7
+ ## Working with LykosJS
8
+
9
+ This project uses LykosJS, Bun, and TypeScript.
10
+
11
+ - Inspect the existing implementation before using a framework API.
12
+ - Follow the APIs and patterns actually present in the project.
13
+ - Do not assume LykosJS behaves like Express, NestJS, or another framework.
14
+ - Do not invent methods, decorators, route registration patterns, or configuration options.
15
+ - Consult the installed package's types and the existing source code when API behavior is unclear.
16
+
17
+ ## Working with GraphQL
18
+
19
+ - Schema files live next to their module and are loaded automatically; resolvers are registered in `src/graphql/graphql.resolvers.ts`.
20
+ - Resolver methods are matched to Query/Mutation fields by name.
21
+ - See the "Adding a GraphQL Feature" section in `AGENTS.md`.
22
+
23
+ ## Editing Rules
24
+
25
+ - Keep changes focused on the user's request.
26
+ - Preserve the existing architecture and public interfaces.
27
+ - Avoid unrelated file changes and unnecessary rewrites.
28
+ - Prefer explicit TypeScript types and avoid `any`.
29
+ - Do not add dependencies without a clear need.
30
+ - Never overwrite user changes without inspecting them first.
31
+
32
+ ## Verification
33
+
34
+ Before finishing:
35
+
36
+ 1. Review the changes you made.
37
+ 2. Run relevant tests using the project's configured commands.
38
+ 3. Run the build if a build script exists and is relevant.
39
+ 4. Fix issues caused by your changes when possible.
40
+ 5. Report exactly which checks passed and which were not run.
41
+
42
+ Never claim successful verification without executing the corresponding command.
43
+
44
+ ## Communication
45
+
46
+ Explain important implementation decisions concisely. Mention affected files, verification results, and any unresolved issues.
@@ -0,0 +1,73 @@
1
+ <div align="center">
2
+
3
+ <img src="https://res.cloudinary.com/dgagbheuj/image/upload/v1795227129/koakwx1uyjf68edpilc8.png" alt="LykosJS Logo" width="110" />
4
+
5
+ # LykosJS
6
+
7
+ **Built for Speed. Designed for Scale.**
8
+
9
+ A lightweight, modular backend framework built for speed and DX.
10
+
11
+ [![Bun](https://img.shields.io/badge/Bun-1.3%2B-000000?logo=bun&logoColor=white)](https://bun.sh)
12
+ [![TypeScript](https://img.shields.io/badge/TypeScript-Powered-3178C6?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
13
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://lykosjs.com/docs/license)
14
+
15
+ [Documentation](https://lykosjs.com/docs) · [License](https://lykosjs.com/docs/license)
16
+
17
+ </div>
18
+
19
+ ## Installation
20
+
21
+ Requires [Bun](https://bun.sh) 1.4 or newer.
22
+
23
+ **Create a new project (recommended)**
24
+
25
+ ```bash
26
+ bun create lykosjs my-app
27
+ ```
28
+
29
+ **Or install the package**
30
+
31
+ ```bash
32
+ bun add lykosjs
33
+ ```
34
+
35
+ <p align="center">
36
+ <img
37
+ src="https://res.cloudinary.com/dgagbheuj/image/upload/v1795313052/muvd4yk1qmoa3kmdql43.png"
38
+ alt="LykosJS — Built for Speed. Designed for Scale."
39
+ width="100%"
40
+ />
41
+ </p>
42
+
43
+ ## GraphQL
44
+
45
+ This template serves GraphQL at `POST /graphql`.
46
+
47
+ ```bash
48
+ curl -X POST http://localhost:4000/graphql \
49
+ -H "content-type: application/json" \
50
+ -d "{\"query\":\"{ users { id name } }\"}"
51
+ ```
52
+
53
+ ### Add a feature
54
+
55
+ 1. Create a module in `src/modules/<feature>/` with a service and a resolver (see `users`).
56
+ 2. Add a schema file `src/modules/<feature>/graphql-schema/<feature>.graphql`. Every `*.graphql` file under `src/modules` is loaded automatically, and each module can declare its own `type Query` and `type Mutation`.
57
+ 3. Register the module in `src/modules.ts`.
58
+ 4. Add one line to `src/graphql/graphql.resolvers.ts`:
59
+
60
+ ```ts
61
+ app.resolve(PostsModule, PostsResolver),
62
+ ```
63
+
64
+ A resolver method is matched to the Query/Mutation field with the same name.
65
+
66
+ ### `src/graphql/`
67
+
68
+ | File | Purpose |
69
+ | ---------------------- | ------------------------------------- |
70
+ | `graphql.schema.ts` | Finds and merges every module schema |
71
+ | `graphql.resolvers.ts` | List of resolvers (the file you edit) |
72
+ | `graphql.executor.ts` | Builds the schema and runs queries |
73
+ | `graphql.handler.ts` | Validates the request body |
@@ -0,0 +1,34 @@
1
+ # dependencies (bun install)
2
+ node_modules
3
+
4
+ # output
5
+ out
6
+ dist
7
+ *.tgz
8
+
9
+ # code coverage
10
+ coverage
11
+ *.lcov
12
+
13
+ # logs
14
+ logs
15
+ _.log
16
+ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17
+
18
+ # dotenv environment variable files
19
+ .env
20
+ .env.development.local
21
+ .env.test.local
22
+ .env.production.local
23
+ .env.local
24
+
25
+ # caches
26
+ .eslintcache
27
+ .cache
28
+ *.tsbuildinfo
29
+
30
+ # IntelliJ based IDEs
31
+ .idea
32
+
33
+ # Finder (MacOS) folder config
34
+ .DS_Store
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "my-lykosjs-app",
3
+ "version": "1.0.0",
4
+ "private": true,
5
+ "type": "module",
6
+ "lykosjsAPI": {
7
+ "template": "graphql"
8
+ },
9
+ "scripts": {
10
+ "dev": "bun run --watch src/launch.ts",
11
+ "build": "bun build src/launch.ts --outdir dist --target=bun --minify",
12
+ "start": "bun run src/launch.ts",
13
+ "typecheck": "tsc --noEmit"
14
+ },
15
+ "dependencies": {
16
+ "graphql": "^17.1.0",
17
+ "lykosjs": "^0.0.1",
18
+ "@socket.io/bun-engine": "^0.1.2",
19
+ "socket.io": "^4.8.4"
20
+ },
21
+ "devDependencies": {
22
+ "@types/bun": "latest",
23
+ "typescript": "^5.9.3"
24
+ }
25
+ }
@@ -0,0 +1,54 @@
1
+ import { graphql, type ExecutionResult, type GraphQLSchema } from 'graphql'
2
+ import { buildSchema } from './graphql.schema'
3
+
4
+ type ResolverFn = (...args: unknown[]) => unknown
5
+
6
+ export class GraphQLExecutor {
7
+ private schema!: GraphQLSchema
8
+ private rootValue: Record<string, ResolverFn> = {}
9
+
10
+ constructor(private readonly resolvers: object[]) {}
11
+
12
+ async initialize() {
13
+ this.schema = await buildSchema()
14
+ this.rootValue = this.mergeResolvers()
15
+ }
16
+
17
+ execute(source: string, variables?: Record<string, unknown>): Promise<ExecutionResult> {
18
+ return graphql({
19
+ schema: this.schema,
20
+ source,
21
+ rootValue: this.rootValue,
22
+ variableValues: variables,
23
+ })
24
+ }
25
+
26
+ // Collects the methods of every resolver into one root value (field name -> function).
27
+ private mergeResolvers(): Record<string, ResolverFn> {
28
+ const root: Record<string, ResolverFn> = {}
29
+ const owners = new Map<string, object>()
30
+
31
+ for (const resolver of this.resolvers) {
32
+ let target: object | null = resolver
33
+
34
+ while (target && target !== Object.prototype) {
35
+ for (const name of Object.getOwnPropertyNames(target)) {
36
+ const descriptor = Object.getOwnPropertyDescriptor(target, name)
37
+ if (name === 'constructor' || typeof descriptor?.value !== 'function') continue
38
+
39
+ const owner = owners.get(name)
40
+ if (owner === resolver) continue // overridden in a subclass
41
+ if (owner !== undefined) {
42
+ throw new Error(`GraphQL resolver "${name}" is defined in more than one resolver`)
43
+ }
44
+
45
+ owners.set(name, resolver)
46
+ root[name] = (descriptor.value as ResolverFn).bind(resolver)
47
+ }
48
+ target = Object.getPrototypeOf(target)
49
+ }
50
+ }
51
+
52
+ return root
53
+ }
54
+ }
@@ -0,0 +1,18 @@
1
+ import type { GraphQLExecutor } from './graphql.executor'
2
+
3
+ export interface GraphQLBody {
4
+ query: string
5
+ variables?: Record<string, unknown>
6
+ }
7
+
8
+ export class GraphQLHandler {
9
+ constructor(private readonly executor: GraphQLExecutor) {}
10
+
11
+ async handle(body: GraphQLBody) {
12
+ if (typeof body?.query !== 'string' || body.query.trim() === '') {
13
+ return { errors: [{ message: 'GraphQL query is required' }] }
14
+ }
15
+
16
+ return this.executor.execute(body.query, body.variables)
17
+ }
18
+ }
@@ -0,0 +1,14 @@
1
+ import type { Lykos } from 'lykosjs'
2
+ import { UsersModule } from '../modules/users/users.module'
3
+ import { UsersResolver } from '../modules/users/users.resolver'
4
+
5
+ // Add a line here for every new module's resolver:
6
+ // app.resolve(PostsModule, PostsResolver)
7
+ // (the resolver method has to be named like the field in the .graphql file)
8
+
9
+ export function createResolvers(app: Lykos): object[] {
10
+ return [
11
+ app.resolve(UsersModule, UsersResolver),
12
+ // app.resolve(PostsModule, PostsResolver),
13
+ ]
14
+ }
@@ -0,0 +1,58 @@
1
+ import { readdir, readFile } from 'node:fs/promises'
2
+ import { join } from 'node:path'
3
+ import { fileURLToPath } from 'node:url'
4
+ import {
5
+ buildASTSchema,
6
+ Kind,
7
+ parse,
8
+ type DefinitionNode,
9
+ type FieldDefinitionNode,
10
+ type GraphQLSchema,
11
+ type ObjectTypeDefinitionNode,
12
+ } from 'graphql'
13
+
14
+ const MODULES_DIR = fileURLToPath(new URL('../modules', import.meta.url))
15
+ const ROOT_TYPES = ['Query', 'Mutation', 'Subscription']
16
+
17
+ // Reads all .graphql files inside src/modules and builds a single schema.
18
+ // Every module can have its own Query/Mutation, we merge them here.
19
+
20
+ export async function buildSchema(): Promise<GraphQLSchema> {
21
+ const files = (await readdir(MODULES_DIR, { recursive: true })).filter((file) =>
22
+ file.endsWith('.graphql'),
23
+ )
24
+
25
+ const definitions: DefinitionNode[] = []
26
+ const roots = new Map<string, { node: ObjectTypeDefinitionNode; fields: FieldDefinitionNode[] }>()
27
+
28
+ for (const file of files.sort()) {
29
+ const document = parse(await readFile(join(MODULES_DIR, file), 'utf-8'))
30
+
31
+ for (const definition of document.definitions) {
32
+ if (
33
+ definition.kind !== Kind.OBJECT_TYPE_DEFINITION ||
34
+ !ROOT_TYPES.includes(definition.name.value)
35
+ ) {
36
+ definitions.push(definition)
37
+ continue
38
+ }
39
+
40
+ const root = roots.get(definition.name.value) ?? { node: definition, fields: [] }
41
+ for (const field of definition.fields ?? []) {
42
+ if (root.fields.some((existing) => existing.name.value === field.name.value)) {
43
+ throw new Error(
44
+ `GraphQL field "${definition.name.value}.${field.name.value}" is defined twice (${file})`,
45
+ )
46
+ }
47
+ root.fields.push(field)
48
+ }
49
+ roots.set(definition.name.value, root)
50
+ }
51
+ }
52
+
53
+ for (const { node, fields } of roots.values()) {
54
+ definitions.push({ ...node, fields })
55
+ }
56
+
57
+ return buildASTSchema({ kind: Kind.DOCUMENT, definitions })
58
+ }
@@ -0,0 +1,36 @@
1
+ import { Lykos } from 'lykosjs'
2
+ import { Modules } from './modules'
3
+ import { GraphQLExecutor } from './graphql/graphql.executor'
4
+ import { GraphQLHandler, type GraphQLBody } from './graphql/graphql.handler'
5
+ import { createResolvers } from './graphql/graphql.resolvers'
6
+
7
+ async function launch() {
8
+ const app = new Lykos()
9
+
10
+ app.modules(Modules)
11
+
12
+ // Resolvers are created through DI (see src/graphql/graphql.resolvers.ts)
13
+ const executor = new GraphQLExecutor(createResolvers(app))
14
+
15
+ // Builds the schema from every src/modules/**/*.graphql file
16
+ await executor.initialize()
17
+
18
+ const graphqlHandler = new GraphQLHandler(executor)
19
+
20
+ app.GET('/', async (ctx, reply) => reply.send('Welcome to LykosJS 🐺'))
21
+
22
+ app.POST('/graphql', async (ctx, reply) => {
23
+ const body = await ctx.json<GraphQLBody>()
24
+
25
+ const result = await graphqlHandler.handle(body)
26
+
27
+ return ctx.json(result)
28
+ })
29
+
30
+ app.listen(4000, () => console.log('🐺 LykosJS is running at http://localhost:4000'))
31
+ }
32
+
33
+ launch().catch((error: unknown) => {
34
+ console.error('[LykosJS] Failed to start application:', error)
35
+ process.exit(1)
36
+ })
@@ -0,0 +1,29 @@
1
+ type User {
2
+ id: ID!
3
+ name: String!
4
+ email: String!
5
+ createdAt: String!
6
+ }
7
+
8
+ input CreateUserInput {
9
+ name: String!
10
+ email: String!
11
+ password: String!
12
+ }
13
+
14
+ input UpdateUserInput {
15
+ name: String
16
+ email: String
17
+ password: String
18
+ }
19
+
20
+ type Query {
21
+ users: [User!]!
22
+ user(id: ID!): User
23
+ }
24
+
25
+ type Mutation {
26
+ createUser(input: CreateUserInput!): User!
27
+ updateUser(id: ID!, input: UpdateUserInput!): User
28
+ deleteUser(id: ID!): User
29
+ }