@smartive/graphql-magic 16.3.0 → 16.3.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/.github/workflows/release.yml +2 -2
- package/CHANGELOG.md +3 -3
- package/README.md +23 -4
- package/docs/docs/2-models.md +11 -11
- package/docs/docs/3-fields.md +5 -5
- package/docs/docs/4-generation.md +6 -6
- package/docs/docs/6-graphql-server.md +3 -3
- package/docs/docs/7-graphql-client.md +3 -3
- package/package.json +5 -5
|
@@ -12,7 +12,7 @@ jobs:
|
|
|
12
12
|
- uses: actions/checkout@v3
|
|
13
13
|
with:
|
|
14
14
|
submodules: true
|
|
15
|
-
- uses: actions/setup-node@
|
|
15
|
+
- uses: actions/setup-node@v4
|
|
16
16
|
with:
|
|
17
17
|
node-version: 20
|
|
18
18
|
- run: npm ci
|
|
@@ -30,7 +30,7 @@ jobs:
|
|
|
30
30
|
persist-credentials: false
|
|
31
31
|
|
|
32
32
|
- name: Setup Node
|
|
33
|
-
uses: actions/setup-node@
|
|
33
|
+
uses: actions/setup-node@v4
|
|
34
34
|
with:
|
|
35
35
|
node-version: '20'
|
|
36
36
|
- name: Install and Build
|
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
## [16.3.1](https://github.com/smartive/graphql-magic/compare/v16.3.0...v16.3.1) (2025-02-25)
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
###
|
|
4
|
+
### Bug Fixes
|
|
5
5
|
|
|
6
|
-
*
|
|
6
|
+
* **deps:** update dependency code-block-writer to v13 ([#142](https://github.com/smartive/graphql-magic/issues/142)) ([5ac2426](https://github.com/smartive/graphql-magic/commit/5ac2426a4610e5df7b2a490f443423e9ad5cbf08))
|
package/README.md
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
# graphql-magic
|
|
2
2
|
|
|
3
|
-
Welcome to graphql-magic
|
|
3
|
+
Welcome to `graphql-magic`, a revolutionary library that transforms the way of working with GraphQL and databases in TypeScript projects. This library aims to streamline the development process, enhancing speed, efficiency, and type safety.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Key Features
|
|
6
|
+
|
|
7
|
+
- **Model First**: Define your models and their relations directly in TypeScript. `graphql-magic` seamlessly integrates with your TypeScript projects, ensuring type safety and reducing the need for redundant code.
|
|
8
|
+
|
|
9
|
+
- **Automatic GraphQL Schema Generation**: Forget about manually writing your GraphQL schemas. With `graphql-magic`, your TypeScript model definitions are automatically transformed into fully functional GraphQL schemas.
|
|
10
|
+
|
|
11
|
+
- **Database Management Made Easy**: Leveraging the power of Knex for migrations, `graphql-magic` not only generates the necessary database schemas based on your models but also handles all database migrations for you. This means your database is always in sync with your application's models without any extra effort.
|
|
12
|
+
|
|
13
|
+
- **Speed and Efficiency**: Designed with performance in mind, `graphql-magic` ensures that your applications run swiftly and efficiently, handling complex queries and mutations with ease.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Why graphql-magic?
|
|
17
|
+
|
|
18
|
+
`graphql-magic` was developed by smartive, representing the culmination of years of experience in creating robust, scalable, and efficient web applications. Suitable for both small projects and large-scale enterprise applications, `graphql-magic` offers the necessary tools for success.
|
|
19
|
+
|
|
20
|
+
The magic of `graphql-magic` takes TypeScript projects to the next level. To get started, visit the [Documentation](https://smartive.github.io/graphql-magic/).
|
|
6
21
|
|
|
7
22
|
## Development
|
|
8
23
|
|
|
24
|
+
Start the required dependencies such as a Postgres database:
|
|
25
|
+
|
|
9
26
|
```
|
|
10
27
|
npm run deps
|
|
11
28
|
```
|
|
@@ -14,10 +31,12 @@ npm run deps
|
|
|
14
31
|
npm bootstrap
|
|
15
32
|
```
|
|
16
33
|
|
|
34
|
+
Bootstrap the application by generating all the models, migrations and graphql schema:
|
|
17
35
|
```
|
|
18
|
-
npm
|
|
36
|
+
npm run bootstrap
|
|
19
37
|
```
|
|
20
38
|
|
|
39
|
+
Run the tests to see if everything works properly:
|
|
21
40
|
```
|
|
22
|
-
npm
|
|
41
|
+
npm test
|
|
23
42
|
```
|
package/docs/docs/2-models.md
CHANGED
|
@@ -35,7 +35,7 @@ These are the entity options:
|
|
|
35
35
|
|
|
36
36
|
### `description`
|
|
37
37
|
|
|
38
|
-
Will appear as description in the
|
|
38
|
+
Will appear as description in the GraphQL schema.
|
|
39
39
|
|
|
40
40
|
### `plural`
|
|
41
41
|
|
|
@@ -43,7 +43,7 @@ Will appear as description in the graphql schema.
|
|
|
43
43
|
|
|
44
44
|
### `creatable`
|
|
45
45
|
|
|
46
|
-
When `creatable` is `true`, the entity can be created using a dedicated
|
|
46
|
+
When `creatable` is `true`, the entity can be created using a dedicated GraphQL `create<ModelName>` mutation.
|
|
47
47
|
|
|
48
48
|
For this to work, at least one entity field needs to be marked as `creatable`.
|
|
49
49
|
|
|
@@ -51,7 +51,7 @@ For this to work, at least one entity field needs to be marked as `creatable`.
|
|
|
51
51
|
|
|
52
52
|
### `updatable`
|
|
53
53
|
|
|
54
|
-
When `updatable` is `true`, the entity can be created using a dedicated
|
|
54
|
+
When `updatable` is `true`, the entity can be created using a dedicated GraphQL `delete<ModelName>` mutation.
|
|
55
55
|
|
|
56
56
|
For this to work, at least one entity field needs to be marked as `updatable`.
|
|
57
57
|
|
|
@@ -61,15 +61,15 @@ If a field is updatable, a `<ModelName>Revisions` table is created (containing o
|
|
|
61
61
|
|
|
62
62
|
### `deletable`
|
|
63
63
|
|
|
64
|
-
When `deletable` is `true`, the entity can be created using a dedicated
|
|
64
|
+
When `deletable` is `true`, the entity can be created using a dedicated GraphQL `delete<ModelName>` mutation.
|
|
65
65
|
|
|
66
|
-
This is a soft delete (the `deleted` field is set to `true`), and the entity can be restored with the
|
|
66
|
+
This is a soft delete (the `deleted` field is set to `true`), and the entity can be restored with the GraphQL `restore<ModelName>` mutation.
|
|
67
67
|
|
|
68
68
|
`deletable` also accepts an object to override properties of the implicitly generated `deleted`, `deletedBy` and `deletedAt` fields.
|
|
69
69
|
|
|
70
70
|
### `queriable`
|
|
71
71
|
|
|
72
|
-
When `queriable` is `true` a
|
|
72
|
+
When `queriable` is `true` a GraphQL `Query` becomes available to fetch exactly one element by id.
|
|
73
73
|
|
|
74
74
|
For example, with
|
|
75
75
|
|
|
@@ -82,7 +82,7 @@ For example, with
|
|
|
82
82
|
}
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
-
the following
|
|
85
|
+
the following GraphQL query becomes possible
|
|
86
86
|
|
|
87
87
|
```graphql
|
|
88
88
|
query {
|
|
@@ -94,7 +94,7 @@ query {
|
|
|
94
94
|
|
|
95
95
|
### `listQueriable`
|
|
96
96
|
|
|
97
|
-
When `listQueriable` is `true` a
|
|
97
|
+
When `listQueriable` is `true` a GraphQL `Query` becomes available to fetch a list of elements of this model.
|
|
98
98
|
|
|
99
99
|
For example, with
|
|
100
100
|
|
|
@@ -107,7 +107,7 @@ For example, with
|
|
|
107
107
|
}
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
-
the following
|
|
110
|
+
the following GraphQL query becomes possible
|
|
111
111
|
|
|
112
112
|
```graphql
|
|
113
113
|
query {
|
|
@@ -123,7 +123,7 @@ The name of the field that ought to be used as display value, e.g. a `Post`'s `t
|
|
|
123
123
|
|
|
124
124
|
### `defaultOrderBy`
|
|
125
125
|
|
|
126
|
-
An array of orders with the same structure as the `orderBy` parameters in
|
|
126
|
+
An array of orders with the same structure as the `orderBy` parameters in GraphQL queries. The implicit default order by is `[{ createdAt: 'DESC }]`.
|
|
127
127
|
|
|
128
128
|
### `fields`
|
|
129
129
|
|
|
@@ -131,7 +131,7 @@ An array of fields. See [fields](./fields)
|
|
|
131
131
|
|
|
132
132
|
## Scalars
|
|
133
133
|
|
|
134
|
-
Used for
|
|
134
|
+
Used for GraphQL scalars, e.g.
|
|
135
135
|
|
|
136
136
|
```ts
|
|
137
137
|
{
|
package/docs/docs/3-fields.md
CHANGED
|
@@ -85,11 +85,11 @@ For more details, see section on [kinds](#kinds) below.
|
|
|
85
85
|
|
|
86
86
|
### `type`
|
|
87
87
|
|
|
88
|
-
This represents the
|
|
88
|
+
This represents the GraphQL "return type", which can be a primitive or a separate model (depending on the [kind](#kinds)).
|
|
89
89
|
|
|
90
90
|
### `description`
|
|
91
91
|
|
|
92
|
-
Will appear as description in the
|
|
92
|
+
Will appear as description in the GraphQL schema.
|
|
93
93
|
|
|
94
94
|
### `list`
|
|
95
95
|
|
|
@@ -97,11 +97,11 @@ If `list` is `true` the result is an array.
|
|
|
97
97
|
|
|
98
98
|
### `nonNull`
|
|
99
99
|
|
|
100
|
-
Will make the field required both in the
|
|
100
|
+
Will make the field required both in the GraphQL schema and in the database.
|
|
101
101
|
|
|
102
102
|
### `defaultValue`
|
|
103
103
|
|
|
104
|
-
Will set this as default value in
|
|
104
|
+
Will set this as default value in GraphQL mutations and in the database.
|
|
105
105
|
|
|
106
106
|
### `args`
|
|
107
107
|
|
|
@@ -309,7 +309,7 @@ Available postfixes are:
|
|
|
309
309
|
|
|
310
310
|
### `queriable`
|
|
311
311
|
|
|
312
|
-
`true` by default. If explicitly set to `false`, the field won't be queriable via
|
|
312
|
+
`true` by default. If explicitly set to `false`, the field won't be queriable via GraphQL.
|
|
313
313
|
|
|
314
314
|
Also accepts an object that defines a list of `roles` to restrict access to specific roles.
|
|
315
315
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Code generation
|
|
2
2
|
|
|
3
|
-
`graphql-magic` generates a lot of utility code for you based on the models, in particular
|
|
3
|
+
`graphql-magic` generates a lot of utility code for you based on the models, in particular TypeScript types.
|
|
4
4
|
|
|
5
5
|
This can be done directly with `npx gqm generate`.
|
|
6
6
|
|
|
@@ -10,20 +10,20 @@ During the first run, the tool applies the following changes to the repo:
|
|
|
10
10
|
* Add local database connection variables to `.env` file.
|
|
11
11
|
* Add generated folder to `.gitignore`
|
|
12
12
|
* Generate `models.ts` file (if not present).
|
|
13
|
-
* Generate a basic `get-me.ts` example
|
|
13
|
+
* Generate a basic `get-me.ts` example GraphQL query.
|
|
14
14
|
* Generate the `execute.ts` file for the execution
|
|
15
15
|
|
|
16
16
|
With each application, it generates the following files in the configured "generated" folder:
|
|
17
17
|
|
|
18
|
-
* `schema.graphql` - the schema of the
|
|
18
|
+
* `schema.graphql` - the schema of the API, for reference
|
|
19
19
|
* `models.json` - the final models array, including generated fields such as "id","createdBy"... for reference
|
|
20
|
-
* `api/index.ts` - the server-side model
|
|
21
|
-
* `client/index.ts` - the client-side
|
|
20
|
+
* `api/index.ts` - the server-side model TypeScript types
|
|
21
|
+
* `client/index.ts` - the client-side TypeScript types for the provided queries
|
|
22
22
|
* `client/mutations.ts` - standard mutation queries for all models
|
|
23
23
|
* `db/index.ts` - types for data from/to the database
|
|
24
24
|
* `db/knex.ts` - types to extend the `knex` query builder
|
|
25
25
|
|
|
26
|
-
Whenever the models have been changed, it is necessary regenerate this code.
|
|
26
|
+
Whenever the models have been changed, it is necessary to regenerate this code.
|
|
27
27
|
It is recommended to create a `package.json` script and to always generate code after install (or with `npm run generate`):
|
|
28
28
|
|
|
29
29
|
```
|
|
@@ -39,7 +39,7 @@ export const executeGraphql = async <T, V = undefined>(
|
|
|
39
39
|
}
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
This is where you can set up your
|
|
42
|
+
This is where you can set up your GraphQL server with
|
|
43
43
|
|
|
44
44
|
* user authentication (see the [Tutorial](./tutorial) for an example with auth0)
|
|
45
45
|
* custom resolvers (see [Custom resolvers](#custom-resolvers))
|
|
@@ -47,8 +47,8 @@ This is where you can set up your graphql server with
|
|
|
47
47
|
|
|
48
48
|
## Graphql API
|
|
49
49
|
|
|
50
|
-
If you only need to execute
|
|
51
|
-
If you need client side querying, use `executeGraphql` to create a
|
|
50
|
+
If you only need to execute GraphQL on the server (e.g. on `next.js` server components or server actions), you don't need a GraphQL endpoint.
|
|
51
|
+
If you need client side querying, use `executeGraphql` to create a GraphQL endpoint, e.g. in `src/app/api/graphql/route.ts`:
|
|
52
52
|
|
|
53
53
|
```ts
|
|
54
54
|
export const POST = (req) => {
|
|
@@ -18,7 +18,7 @@ module.exports = {
|
|
|
18
18
|
|
|
19
19
|
### Server side
|
|
20
20
|
|
|
21
|
-
On the server side, and with `next.js` server actions, a
|
|
21
|
+
On the server side, and with `next.js` server actions, a GraphQL API becomes unnecessary, and you can execute queries directly using `executeGraphql`:
|
|
22
22
|
|
|
23
23
|
```tsx
|
|
24
24
|
import { GetMeQuery, GetPostsQuery } from "@/generated/client";
|
|
@@ -48,7 +48,7 @@ async function Posts({ me }: { me: GetMeQuery['me'] }) {
|
|
|
48
48
|
|
|
49
49
|
### Client side
|
|
50
50
|
|
|
51
|
-
On the client, you'd need to set up a
|
|
51
|
+
On the client, you'd need to set up a GraphQL endpoint and then query it like any other GraphQL API, such as with [`@apollo/client`](https://www.apollographql.com/docs/react/get-started).
|
|
52
52
|
|
|
53
53
|
```tsx
|
|
54
54
|
import { GetMeQuery, GetPostsQuery } from "@/generated/client";
|
|
@@ -148,4 +148,4 @@ async function CreatePost() {
|
|
|
148
148
|
}
|
|
149
149
|
```
|
|
150
150
|
|
|
151
|
-
Just like with queries, if is necessary to perform mutations on the client, use a
|
|
151
|
+
Just like with queries, if is necessary to perform mutations on the client, use a GraphQL client instead of `executeGraphql`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@smartive/graphql-magic",
|
|
3
|
-
"version": "16.3.
|
|
3
|
+
"version": "16.3.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@graphql-codegen/typescript-compatibility": "^2.1.5",
|
|
51
51
|
"@graphql-codegen/typescript-operations": "^4.0.1",
|
|
52
52
|
"@graphql-codegen/typescript-resolvers": "^4.0.1",
|
|
53
|
-
"code-block-writer": "^
|
|
53
|
+
"code-block-writer": "^13.0.0",
|
|
54
54
|
"commander": "^11.0.0",
|
|
55
55
|
"dayjs": "^1.11.10",
|
|
56
56
|
"dotenv": "^16.3.1",
|
|
@@ -75,16 +75,16 @@
|
|
|
75
75
|
"@types/lodash": "4.17.15",
|
|
76
76
|
"@types/luxon": "3.4.2",
|
|
77
77
|
"@types/pg": "8.11.11",
|
|
78
|
-
"@types/uuid": "
|
|
78
|
+
"@types/uuid": "10.0.0",
|
|
79
79
|
"create-ts-index": "1.14.0",
|
|
80
80
|
"del-cli": "5.1.0",
|
|
81
|
-
"esbuild": "0.
|
|
81
|
+
"esbuild": "0.25.0",
|
|
82
82
|
"eslint": "8.57.1",
|
|
83
83
|
"graphql-request": "6.1.0",
|
|
84
84
|
"jest": "29.7.0",
|
|
85
85
|
"mock-knex": "0.4.13",
|
|
86
86
|
"prettier": "2.8.8",
|
|
87
|
-
"ts-jest": "29.2.
|
|
87
|
+
"ts-jest": "29.2.6",
|
|
88
88
|
"ts-node": "10.9.2",
|
|
89
89
|
"typescript": "5.5.4"
|
|
90
90
|
}
|