create-joist-app 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 (119) hide show
  1. package/build/create-app.d.ts +18 -0
  2. package/build/create-app.d.ts.map +1 -0
  3. package/build/create-app.js +73 -0
  4. package/build/create-app.js.map +1 -0
  5. package/build/create-app.test.d.ts +2 -0
  6. package/build/create-app.test.d.ts.map +1 -0
  7. package/build/create-app.test.js +176 -0
  8. package/build/create-app.test.js.map +1 -0
  9. package/build/create-db-dockerfile.d.ts +2 -0
  10. package/build/create-db-dockerfile.d.ts.map +1 -0
  11. package/build/create-db-dockerfile.js +43 -0
  12. package/build/create-db-dockerfile.js.map +1 -0
  13. package/build/create-docker-compose.d.ts +3 -0
  14. package/build/create-docker-compose.d.ts.map +1 -0
  15. package/build/create-docker-compose.js +35 -0
  16. package/build/create-docker-compose.js.map +1 -0
  17. package/build/create-env.d.ts +3 -0
  18. package/build/create-env.d.ts.map +1 -0
  19. package/build/create-env.js +16 -0
  20. package/build/create-env.js.map +1 -0
  21. package/build/helpers/copy.d.ts +3 -0
  22. package/build/helpers/copy.d.ts.map +1 -0
  23. package/build/helpers/copy.js +33 -0
  24. package/build/helpers/copy.js.map +1 -0
  25. package/build/helpers/get-package-manager.d.ts +3 -0
  26. package/build/helpers/get-package-manager.d.ts.map +1 -0
  27. package/build/helpers/get-package-manager.js +17 -0
  28. package/build/helpers/get-package-manager.js.map +1 -0
  29. package/build/helpers/install.d.ts +3 -0
  30. package/build/helpers/install.d.ts.map +1 -0
  31. package/build/helpers/install.js +29 -0
  32. package/build/helpers/install.js.map +1 -0
  33. package/build/helpers/is-folder-empty.d.ts +2 -0
  34. package/build/helpers/is-folder-empty.d.ts.map +1 -0
  35. package/build/helpers/is-folder-empty.js +14 -0
  36. package/build/helpers/is-folder-empty.js.map +1 -0
  37. package/build/helpers/is-folder-empty.test.d.ts +2 -0
  38. package/build/helpers/is-folder-empty.test.d.ts.map +1 -0
  39. package/build/helpers/is-folder-empty.test.js +38 -0
  40. package/build/helpers/is-folder-empty.test.js.map +1 -0
  41. package/build/helpers/is-writeable.d.ts +2 -0
  42. package/build/helpers/is-writeable.d.ts.map +1 -0
  43. package/build/helpers/is-writeable.js +17 -0
  44. package/build/helpers/is-writeable.js.map +1 -0
  45. package/build/index.d.ts +3 -0
  46. package/build/index.d.ts.map +1 -0
  47. package/build/index.js +213 -0
  48. package/build/index.js.map +1 -0
  49. package/build/replace-sentinels.d.ts +2 -0
  50. package/build/replace-sentinels.d.ts.map +1 -0
  51. package/build/replace-sentinels.js +59 -0
  52. package/build/replace-sentinels.js.map +1 -0
  53. package/build/update-package-json.d.ts +2 -0
  54. package/build/update-package-json.d.ts.map +1 -0
  55. package/build/update-package-json.js +25 -0
  56. package/build/update-package-json.js.map +1 -0
  57. package/package.json +55 -0
  58. package/templates/basic/README.md +70 -0
  59. package/templates/basic/gitignore +28 -0
  60. package/templates/basic/jest.config.js +14 -0
  61. package/templates/basic/joist-config.json +7 -0
  62. package/templates/basic/migrations/1580658856631_initial.ts +22 -0
  63. package/templates/basic/package.json +28 -0
  64. package/templates/basic/src/context.ts +14 -0
  65. package/templates/basic/src/entities/Author.test.ts +17 -0
  66. package/templates/basic/src/entities/Author.ts +7 -0
  67. package/templates/basic/src/entities/Book.ts +3 -0
  68. package/templates/basic/src/entities/entities.ts +14 -0
  69. package/templates/basic/src/entities/factories/index.ts +2 -0
  70. package/templates/basic/src/entities/factories/newAuthor.ts +6 -0
  71. package/templates/basic/src/entities/factories/newBook.ts +6 -0
  72. package/templates/basic/src/entities/index.ts +3 -0
  73. package/templates/basic/src/setupTestEnv.ts +5 -0
  74. package/templates/basic/src/setupTests.ts +29 -0
  75. package/templates/basic/tsconfig.json +23 -0
  76. package/templates/graphql/README.md +84 -0
  77. package/templates/graphql/codegen.yml +11 -0
  78. package/templates/graphql/gitignore +28 -0
  79. package/templates/graphql/graphql-codegen-joist.js +5 -0
  80. package/templates/graphql/graphql-codegen.js +26 -0
  81. package/templates/graphql/jest.config.js +14 -0
  82. package/templates/graphql/joist-config.json +8 -0
  83. package/templates/graphql/migrations/1580658856631_initial.ts +38 -0
  84. package/templates/graphql/package.json +41 -0
  85. package/templates/graphql/schema/.history.json +9 -0
  86. package/templates/graphql/schema/author.graphql +20 -0
  87. package/templates/graphql/schema/book.graphql +19 -0
  88. package/templates/graphql/schema/enums.graphql +0 -0
  89. package/templates/graphql/schema/root.graphql +0 -0
  90. package/templates/graphql/src/.history.json +12 -0
  91. package/templates/graphql/src/context.ts +14 -0
  92. package/templates/graphql/src/entities/Author.test.ts +36 -0
  93. package/templates/graphql/src/entities/Author.ts +7 -0
  94. package/templates/graphql/src/entities/Book.ts +3 -0
  95. package/templates/graphql/src/entities/entities.ts +14 -0
  96. package/templates/graphql/src/entities/factories/index.ts +2 -0
  97. package/templates/graphql/src/entities/factories/newAuthor.ts +6 -0
  98. package/templates/graphql/src/entities/factories/newBook.ts +6 -0
  99. package/templates/graphql/src/entities/index.ts +3 -0
  100. package/templates/graphql/src/jest.d.ts +9 -0
  101. package/templates/graphql/src/resolvers/author/authorResolvers.test.ts +17 -0
  102. package/templates/graphql/src/resolvers/author/authorResolvers.ts +5 -0
  103. package/templates/graphql/src/resolvers/author/saveAuthorMutation.test.ts +13 -0
  104. package/templates/graphql/src/resolvers/author/saveAuthorMutation.ts +9 -0
  105. package/templates/graphql/src/resolvers/book/bookResolvers.test.ts +17 -0
  106. package/templates/graphql/src/resolvers/book/bookResolvers.ts +5 -0
  107. package/templates/graphql/src/resolvers/book/saveBookMutation.test.ts +16 -0
  108. package/templates/graphql/src/resolvers/book/saveBookMutation.ts +9 -0
  109. package/templates/graphql/src/resolvers/enumResolvers.ts +5 -0
  110. package/templates/graphql/src/resolvers/index.ts +15 -0
  111. package/templates/graphql/src/resolvers/mutations/index.ts +7 -0
  112. package/templates/graphql/src/resolvers/objects/index.ts +6 -0
  113. package/templates/graphql/src/resolvers/testUtils.ts +10 -0
  114. package/templates/graphql/src/resolvers/utils.ts +1 -0
  115. package/templates/graphql/src/server.ts +37 -0
  116. package/templates/graphql/src/setupIt.ts +17 -0
  117. package/templates/graphql/src/setupTestEnv.ts +5 -0
  118. package/templates/graphql/src/setupTests.ts +37 -0
  119. package/templates/graphql/tsconfig.json +23 -0
@@ -0,0 +1,14 @@
1
+ import { EntityManager, newPgConnectionConfig } from "joist-orm";
2
+ import { PostgresDriver } from "joist-orm/pg";
3
+ import { Pool } from "pg";
4
+
5
+ export interface Context {
6
+ pool: Pool;
7
+ em: EntityManager;
8
+ }
9
+
10
+ export function newContext(): Context {
11
+ const pool = new Pool(newPgConnectionConfig());
12
+ const em = new EntityManager({}, new PostgresDriver(pool));
13
+ return { em, pool };
14
+ }
@@ -0,0 +1,17 @@
1
+ import { newEm } from "../setupTests";
2
+ import { newAuthor, newBook } from "./factories";
3
+
4
+ describe("Author", () => {
5
+ it("can create an author", async () => {
6
+ const em = newEm();
7
+ newAuthor(em, { firstName: "John", lastName: "Doe" });
8
+ await em.flush();
9
+ });
10
+
11
+ it("can have books", async () => {
12
+ const em = newEm();
13
+ const author = newAuthor(em, { firstName: "Test", lastName: "Author" });
14
+ newBook(em, { title: "Test Book", author });
15
+ await em.flush();
16
+ });
17
+ });
@@ -0,0 +1,7 @@
1
+ import { AuthorCodegen } from "./codegen/AuthorCodegen";
2
+
3
+ export class Author extends AuthorCodegen {
4
+ get fullName(): string {
5
+ return `${this.firstName} ${this.lastName}`;
6
+ }
7
+ }
@@ -0,0 +1,3 @@
1
+ import { BookCodegen } from "./codegen/BookCodegen";
2
+
3
+ export class Book extends BookCodegen {}
@@ -0,0 +1,14 @@
1
+ // organize-imports-ignore
2
+
3
+ // This file drives our import order to avoid undefined errors
4
+ // when the subclasses extend the base classes, see:
5
+ // https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de
6
+
7
+ export * from "./codegen/AuthorCodegen";
8
+ export * from "./codegen/BookCodegen";
9
+ export * from "./Author";
10
+ export * from "./Book";
11
+
12
+ export * from "./factories/newAuthor";
13
+ export * from "./factories/newBook";
14
+ export * from "./codegen/metadata";
@@ -0,0 +1,2 @@
1
+ export * from "./newAuthor";
2
+ export * from "./newBook";
@@ -0,0 +1,6 @@
1
+ import { EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
2
+ import { Author } from "../entities";
3
+
4
+ export function newAuthor(em: EntityManager, opts?: FactoryOpts<Author>): Author {
5
+ return newTestInstance(em, Author, opts);
6
+ }
@@ -0,0 +1,6 @@
1
+ import { EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
2
+ import { Book } from "../entities";
3
+
4
+ export function newBook(em: EntityManager, opts?: FactoryOpts<Book>): Book {
5
+ return newTestInstance(em, Book, opts);
6
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./entities";
2
+ export * from "./Author";
3
+ export * from "./Book";
@@ -0,0 +1,5 @@
1
+ import { GetEnvVars } from "env-cmd";
2
+
3
+ export default async function globalSetup() {
4
+ Object.entries(await GetEnvVars()).forEach(([key, value]) => (process.env[key] = value));
5
+ }
@@ -0,0 +1,29 @@
1
+ import expect from "expect";
2
+ import { newPgConnectionConfig } from "joist-orm";
3
+ import { PostgresDriver } from "joist-orm/pg";
4
+ import { toMatchEntity } from "joist-orm/tests";
5
+ import pg from "pg";
6
+ import { Context } from "./context";
7
+ import { EntityManager } from "./entities";
8
+
9
+ expect.extend({ toMatchEntity });
10
+
11
+ let pool: pg.Pool;
12
+
13
+ beforeAll(async () => {
14
+ pool = new pg.Pool(newPgConnectionConfig());
15
+ });
16
+
17
+ beforeEach(async () => {
18
+ await pool.query("select flush_database()");
19
+ });
20
+
21
+ afterAll(async () => {
22
+ await pool.end();
23
+ });
24
+
25
+ export function newEm(): EntityManager {
26
+ const driver = new PostgresDriver(pool);
27
+ const ctx = { pool, em: null as any } satisfies Context;
28
+ return new EntityManager(ctx, driver);
29
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "nodenext",
5
+ "lib": ["ES2022"],
6
+ "moduleResolution": "nodenext",
7
+ "esModuleInterop": true,
8
+ "strict": true,
9
+ "skipLibCheck": true,
10
+ "forceConsistentCasingInFileNames": true,
11
+ "declaration": true,
12
+ "declarationMap": true,
13
+ "sourceMap": true,
14
+ "outDir": "./build",
15
+ "rootDir": "./src",
16
+ "baseUrl": "./src",
17
+ "paths": {
18
+ "src/*": ["./*"]
19
+ }
20
+ },
21
+ "include": ["src"],
22
+ "exclude": ["node_modules", "build"]
23
+ }
@@ -0,0 +1,84 @@
1
+ # {{PROJECT_NAME}}
2
+
3
+ A [Joist ORM](https://joist-orm.io/) project with GraphQL.
4
+
5
+ ## Getting Started
6
+
7
+ ### Prerequisites
8
+
9
+ - Node.js 18+
10
+ - Docker (for PostgreSQL)
11
+
12
+ ### Setup
13
+
14
+ 1. Start the database and run initial setup:
15
+
16
+ ```bash
17
+ yarn db
18
+ ```
19
+
20
+ This command will:
21
+ - Start a PostgreSQL container
22
+ - Run database migrations
23
+ - Generate Joist entity code and GraphQL types
24
+
25
+ ### Development
26
+
27
+ #### Starting the Server
28
+
29
+ ```bash
30
+ yarn dev
31
+ ```
32
+
33
+ The GraphQL server will start at http://localhost:4000
34
+
35
+ #### Running Tests
36
+
37
+ ```bash
38
+ yarn test
39
+ ```
40
+
41
+ #### Creating a New Migration
42
+
43
+ ```bash
44
+ yarn migrate:new my-migration-name
45
+ ```
46
+
47
+ Then edit the generated file in `migrations/` and run:
48
+
49
+ ```bash
50
+ yarn migrate
51
+ yarn codegen
52
+ ```
53
+
54
+ #### Resetting the Database
55
+
56
+ ```bash
57
+ yarn redb
58
+ ```
59
+
60
+ ### Project Structure
61
+
62
+ ```
63
+ ├── migrations/ # Database migrations
64
+ ├── src/
65
+ │ ├── entities/ # Joist entities
66
+ │ │ ├── codegen/ # Generated entity code (do not edit)
67
+ │ │ └── factories/ # Test factories
68
+ │ ├── resolvers/ # GraphQL resolvers
69
+ │ ├── generated/ # Generated GraphQL types (do not edit)
70
+ │ ├── context.ts # Request context
71
+ │ ├── schema.graphql # GraphQL schema
72
+ │ ├── server.ts # Apollo Server setup
73
+ │ ├── setupTestEnv.ts # Global test setup
74
+ │ └── setupTests.ts # Per-suite test setup
75
+ ├── docker-compose.yml # PostgreSQL service
76
+ ├── joist-config.json # Joist configuration
77
+ └── codegen.yml # GraphQL codegen configuration
78
+ ```
79
+
80
+ ## Learn More
81
+
82
+ - [Joist Documentation](https://joist-orm.io/)
83
+ - [Apollo Server](https://www.apollographql.com/docs/apollo-server/)
84
+ - [GitHub Repository](https://github.com/joist-orm/joist-orm)
@@ -0,0 +1,11 @@
1
+ schema: "./src/**/*.graphql"
2
+ generates:
3
+ ./src/generated/graphql-types.ts:
4
+ plugins:
5
+ - typescript
6
+ - typescript-resolvers
7
+ config:
8
+ contextType: "../context#Context"
9
+ mappers:
10
+ Author: ../entities#Author
11
+ Book: ../entities#Book
@@ -0,0 +1,28 @@
1
+ # Dependencies
2
+ node_modules/
3
+
4
+ # Build output
5
+ build/
6
+ dist/
7
+
8
+ # IDE
9
+ .idea/
10
+ .vscode/
11
+ *.swp
12
+ *.swo
13
+
14
+ # OS
15
+ .DS_Store
16
+ Thumbs.db
17
+
18
+ # Test output
19
+ coverage/
20
+
21
+ # Logs
22
+ *.log
23
+ npm-debug.log*
24
+ yarn-debug.log*
25
+ yarn-error.log*
26
+
27
+ # TypeScript
28
+ *.tsbuildinfo
@@ -0,0 +1,5 @@
1
+ const mappers = { Author: "src/entities#Author", Book: "src/entities#Book" };
2
+
3
+ const enumValues = {};
4
+
5
+ module.exports = { mappers, enumValues };
@@ -0,0 +1,26 @@
1
+ const { mappers, enumValues } = require("./graphql-codegen-joist");
2
+
3
+ module.exports = {
4
+ overwrite: true,
5
+ schema: "./schema/**/*.graphql",
6
+ documents: null,
7
+ generates: {
8
+ "src/generated/graphql-types.ts": {
9
+ config: {
10
+ contextType: "src/context#Context",
11
+ noSchemaStitching: true,
12
+ avoidOptionals: true,
13
+ scaffolding: {
14
+ ignoreObjectsPattern: "Detail$",
15
+ },
16
+ mappers,
17
+ enumValues,
18
+ },
19
+ plugins: [
20
+ "@homebound/graphql-typescript-simple-resolvers",
21
+ "@homebound/graphql-typescript-resolver-scaffolding",
22
+ "@homebound/graphql-typescript-possible-types",
23
+ ],
24
+ },
25
+ },
26
+ };
@@ -0,0 +1,14 @@
1
+ /** @type {import('jest').Config} */
2
+ module.exports = {
3
+ transform: {
4
+ "^.+\\.tsx?$": "@swc/jest",
5
+ },
6
+ moduleNameMapper: {
7
+ "^src/(.*)$": "<rootDir>/src/$1",
8
+ },
9
+ testMatch: ["<rootDir>/src/**/*.test.{ts,tsx}"],
10
+ testEnvironment: "node",
11
+ globalSetup: "./src/setupTestEnv.ts",
12
+ setupFilesAfterEnv: ["./src/setupTests.ts"],
13
+ maxWorkers: 1,
14
+ };
@@ -0,0 +1,8 @@
1
+ {
2
+ "codegenPlugins": ["joist-graphql-codegen"],
3
+ "contextType": "Context@src/context",
4
+ "entities": { "Author": { "tag": "a" }, "Book": { "tag": "b" } },
5
+ "entitiesDirectory": "./src/entities",
6
+ "idType": "tagged-string",
7
+ "version": "0.0.0"
8
+ }
@@ -0,0 +1,38 @@
1
+ import {
2
+ createCreatedAtFunction,
3
+ createEntityTable,
4
+ createUpdatedAtFunction,
5
+ foreignKey,
6
+ } from "joist-migration-utils";
7
+ import { MigrationBuilder } from "node-pg-migrate";
8
+
9
+ export function up(b: MigrationBuilder): void {
10
+ createUpdatedAtFunction(b);
11
+ createCreatedAtFunction(b);
12
+
13
+ // Create flush_database function for test cleanup
14
+ b.sql(`
15
+ CREATE OR REPLACE FUNCTION flush_database() RETURNS void AS $$
16
+ DECLARE
17
+ tables CURSOR FOR
18
+ SELECT tablename FROM pg_tables
19
+ WHERE schemaname = 'public'
20
+ AND tablename != 'pgmigrations';
21
+ BEGIN
22
+ FOR t IN tables LOOP
23
+ EXECUTE 'TRUNCATE TABLE ' || quote_ident(t.tablename) || ' CASCADE';
24
+ END LOOP;
25
+ END;
26
+ $$ LANGUAGE plpgsql;
27
+ `);
28
+
29
+ createEntityTable(b, "authors", {
30
+ first_name: { type: "varchar(255)", notNull: true },
31
+ last_name: { type: "varchar(255)", notNull: false },
32
+ });
33
+
34
+ createEntityTable(b, "books", {
35
+ title: { type: "varchar(255)", notNull: true },
36
+ author_id: foreignKey("authors", { notNull: true }),
37
+ });
38
+ }
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "graphql-template",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "scripts": {
6
+ "build": "tsc",
7
+ "start": "tsx src/server.ts",
8
+ "dev": "tsx watch src/server.ts",
9
+ "test": "env-cmd jest --runInBand",
10
+ "codegen": "env-cmd yarn joist-codegen && yarn graphql-codegen",
11
+ "graphql-codegen": "graphql-codegen --config graphql-codegen.js",
12
+ "format": "prettier --write \"src/**/*.{ts,tsx,graphql}\""
13
+ },
14
+ "dependencies": {
15
+ "@apollo/server": "^4.11.0",
16
+ "@graphql-tools/graphql-file-loader": "^8.0.0",
17
+ "@graphql-tools/load": "^8.0.0",
18
+ "@graphql-tools/merge": "^9.0.0",
19
+ "graphql": "^16.9.0",
20
+ "joist-orm": "2.0.3-next.36",
21
+ "knex": "^3.1.0",
22
+ "pg": "^8.16.3"
23
+ },
24
+ "devDependencies": {
25
+ "@graphql-codegen/cli": "^5.0.0",
26
+ "@graphql-codegen/typescript": "^4.0.0",
27
+ "@graphql-codegen/typescript-resolvers": "^4.0.0",
28
+ "@homebound/graphql-typescript-possible-types": "^2.19.0",
29
+ "@homebound/graphql-typescript-resolver-scaffolding": "^2.49.0",
30
+ "@homebound/graphql-typescript-simple-resolvers": "^1.58.0",
31
+ "@swc/core": "^1.13.0",
32
+ "@swc/jest": "^0.2.39",
33
+ "@types/jest": "^30.0.0",
34
+ "@types/node": "^24.0.0",
35
+ "env-cmd": "^11.0.0",
36
+ "jest": "^30.0.0",
37
+ "prettier": "^3.6.0",
38
+ "tsx": "^4.20.0",
39
+ "typescript": "^5.9.0"
40
+ }
41
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "Author": ["books", "createdAt", "firstName", "id", "lastName", "updatedAt"],
3
+ "Book": ["author", "createdAt", "id", "title", "updatedAt"],
4
+ "Mutation": ["saveAuthor", "saveBook"],
5
+ "SaveAuthorInput": ["firstName", "id", "lastName"],
6
+ "SaveAuthorResult": ["author"],
7
+ "SaveBookInput": ["authorId", "id", "title"],
8
+ "SaveBookResult": ["book"]
9
+ }
@@ -0,0 +1,20 @@
1
+ extend type Mutation {
2
+ saveAuthor(input: SaveAuthorInput!): SaveAuthorResult!
3
+ }
4
+
5
+ type Author {
6
+ id: ID!
7
+ firstName: String!
8
+ lastName: String
9
+ books: [Book!]!
10
+ }
11
+
12
+ input SaveAuthorInput {
13
+ id: ID
14
+ firstName: String
15
+ lastName: String
16
+ }
17
+
18
+ type SaveAuthorResult {
19
+ author: Author!
20
+ }
@@ -0,0 +1,19 @@
1
+ extend type Mutation {
2
+ saveBook(input: SaveBookInput!): SaveBookResult!
3
+ }
4
+
5
+ type Book {
6
+ id: ID!
7
+ title: String!
8
+ author: Author!
9
+ }
10
+
11
+ input SaveBookInput {
12
+ id: ID
13
+ title: String
14
+ authorId: ID
15
+ }
16
+
17
+ type SaveBookResult {
18
+ book: Book!
19
+ }
File without changes
File without changes
@@ -0,0 +1,12 @@
1
+ {
2
+ "files": [
3
+ "resolvers/author/authorResolvers.test.ts",
4
+ "resolvers/author/authorResolvers.ts",
5
+ "resolvers/author/saveAuthorMutation.test.ts",
6
+ "resolvers/author/saveAuthorMutation.ts",
7
+ "resolvers/book/bookResolvers.test.ts",
8
+ "resolvers/book/bookResolvers.ts",
9
+ "resolvers/book/saveBookMutation.test.ts",
10
+ "resolvers/book/saveBookMutation.ts"
11
+ ]
12
+ }
@@ -0,0 +1,14 @@
1
+ import { EntityManager, newPgConnectionConfig } from "joist-orm";
2
+ import { PostgresDriver } from "joist-orm/pg";
3
+ import { Pool } from "pg";
4
+
5
+ export interface Context {
6
+ pool: Pool;
7
+ em: EntityManager;
8
+ }
9
+
10
+ export function newContext(): Context {
11
+ const pool = new Pool(newPgConnectionConfig());
12
+ const em = new EntityManager({}, new PostgresDriver(pool));
13
+ return { em, pool };
14
+ }
@@ -0,0 +1,36 @@
1
+ import { newEm } from "../setupTests";
2
+ import { newAuthor, newBook } from "./factories";
3
+
4
+ describe("Author", () => {
5
+ it("can create an author", async () => {
6
+ const em = newEm();
7
+ const author = newAuthor(em, { firstName: "John", lastName: "Doe" });
8
+ await em.flush();
9
+
10
+ const loaded = await em.load(author.constructor, author.id);
11
+ expect(loaded).toMatchEntity({ firstName: "John", lastName: "Doe" });
12
+ });
13
+
14
+ it("has a full name", async () => {
15
+ const em = newEm();
16
+ const author = newAuthor(em, { firstName: "Jane", lastName: "Smith" });
17
+ expect(author.fullName).toBe("Jane Smith");
18
+ });
19
+
20
+ it("handles missing last name in fullName", async () => {
21
+ const em = newEm();
22
+ const author = newAuthor(em, { firstName: "Jane" });
23
+ expect(author.fullName).toBe("Jane");
24
+ });
25
+
26
+ it("can have books", async () => {
27
+ const em = newEm();
28
+ const author = newAuthor(em, { firstName: "Test", lastName: "Author" });
29
+ const book = newBook(em, { title: "Test Book", author });
30
+ await em.flush();
31
+
32
+ const books = await author.books.load();
33
+ expect(books).toHaveLength(1);
34
+ expect(books[0]).toMatchEntity({ title: "Test Book" });
35
+ });
36
+ });
@@ -0,0 +1,7 @@
1
+ import { AuthorCodegen } from "./codegen/AuthorCodegen";
2
+
3
+ export class Author extends AuthorCodegen {
4
+ get fullName(): string {
5
+ return `${this.firstName} ${this.lastName || ""}`.trim();
6
+ }
7
+ }
@@ -0,0 +1,3 @@
1
+ import { BookCodegen } from "./codegen/BookCodegen";
2
+
3
+ export class Book extends BookCodegen {}
@@ -0,0 +1,14 @@
1
+ // organize-imports-ignore
2
+
3
+ // This file drives our import order to avoid undefined errors
4
+ // when the subclasses extend the base classes, see:
5
+ // https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de
6
+
7
+ export * from "./codegen/AuthorCodegen";
8
+ export * from "./codegen/BookCodegen";
9
+ export * from "./Author";
10
+ export * from "./Book";
11
+
12
+ export * from "./factories/newAuthor";
13
+ export * from "./factories/newBook";
14
+ export * from "./codegen/metadata";
@@ -0,0 +1,2 @@
1
+ export * from "./newAuthor";
2
+ export * from "./newBook";
@@ -0,0 +1,6 @@
1
+ import { EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
2
+ import { Author } from "../Author";
3
+
4
+ export function newAuthor(em: EntityManager, opts?: FactoryOpts<Author>): Author {
5
+ return newTestInstance(em, Author, opts);
6
+ }
@@ -0,0 +1,6 @@
1
+ import { EntityManager, FactoryOpts, newTestInstance } from "joist-orm";
2
+ import { Book } from "../Book";
3
+
4
+ export function newBook(em: EntityManager, opts?: FactoryOpts<Book>): Book {
5
+ return newTestInstance(em, Book, opts);
6
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./entities";
2
+ export * from "./Author";
3
+ export * from "./Book";
@@ -0,0 +1,9 @@
1
+ declare namespace jest {
2
+ type ContextOpts = Partial<import("./context").Context & import("./context").AppContext>;
3
+ type itWithCtxFn = (ctx: import("./context").Context) => Promise<void>;
4
+
5
+ interface It {
6
+ withCtx(name: string, fn: itWithCtxFn): void;
7
+ withCtx(name: string, opts: ContextOpts, fn: itWithCtxFn): void;
8
+ }
9
+ }
@@ -0,0 +1,17 @@
1
+ import { newAuthor } from "src/entities";
2
+ import { authorResolvers } from "src/resolvers/author/authorResolvers";
3
+ import { makeRunObjectField, makeRunObjectFields } from "src/resolvers/testUtils";
4
+
5
+ describe("authorResolvers", () => {
6
+ it.withCtx("can return", async (ctx) => {
7
+ const { em } = ctx;
8
+ // Given a Author
9
+ const a = newAuthor(em);
10
+ // Then we can query it
11
+ const result = await runFields(ctx, a, ["firstName", "lastName", "createdAt", "updatedAt"]);
12
+ expect(result).toMatchEntity({});
13
+ });
14
+ });
15
+
16
+ const runFields = makeRunObjectFields(authorResolvers);
17
+ const runField = makeRunObjectField(authorResolvers);
@@ -0,0 +1,5 @@
1
+ import { Author } from "src/entities";
2
+ import type { AuthorResolvers } from "src/generated/graphql-types";
3
+ import { entityResolver } from "src/resolvers/utils";
4
+
5
+ export const authorResolvers: AuthorResolvers = { ...entityResolver(Author) };
@@ -0,0 +1,13 @@
1
+ import { saveAuthor } from "src/resolvers/author/saveAuthorMutation";
2
+ import { makeRunInputMutation } from "src/resolvers/testUtils";
3
+
4
+ describe("saveAuthor", () => {
5
+ it.withCtx("can create", async (ctx) => {
6
+ const result = await runSave(ctx, () => ({
7
+ firstName: "Test",
8
+ }));
9
+ expect(result).toBeDefined();
10
+ });
11
+ });
12
+
13
+ const runSave = makeRunInputMutation(saveAuthor);
@@ -0,0 +1,9 @@
1
+ import { Author } from "src/entities";
2
+ import type { MutationResolvers } from "src/generated/graphql-types";
3
+ import { saveEntity } from "src/resolvers/utils";
4
+
5
+ export const saveAuthor: Pick<MutationResolvers, "saveAuthor"> = {
6
+ async saveAuthor(_, args, ctx) {
7
+ return { author: await saveEntity(ctx, Author, args.input) };
8
+ },
9
+ };