Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update documentation #192

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
# graphql-magic

Welcome to graphql-magic!
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.

See the [docs](https://)

## Key Features

- **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.

- **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.

- **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.

- **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.


## Why graphql-magic?

`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.

The magic of `graphql-magic` takes TypeScript projects to the next level. To get started, visit the [Documentation](https://smartive.github.io/graphql-magic/).

## Development

Start the required dependencies such as a Postgres database:
```
npm bootstrap
npm run deps
```

Bootstrap the application by generating all the models, migrations and graphql schema:
```
npm test
npm run bootstrap
```

Run the tests to see if everything works properly:
```
npm run build
npm test
```
22 changes: 11 additions & 11 deletions docs/docs/2-models.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ These are the entity options:

### `description`

Will appear as description in the graphql schema.
Will appear as description in the GraphQL schema.

### `plural`

`graphql-magic` detects natural language plurals of model names with the `inflection` npm package. You can override this here.

### `creatable`

When `creatable` is `true`, the entity can be created using a dedicated graphql `create<ModelName>` mutation.
When `creatable` is `true`, the entity can be created using a dedicated GraphQL `create<ModelName>` mutation.

For this to work, at least one entity field needs to be marked as `creatable`.

`creatable` also accepts an object to override properties of the implicitly generated `createdBy` and `createdAt` fields.

### `updatable`

When `updatable` is `true`, the entity can be created using a dedicated graphql `delete<ModelName>` mutation.
When `updatable` is `true`, the entity can be created using a dedicated GraphQL `delete<ModelName>` mutation.

For this to work, at least one entity field needs to be marked as `updatable`.

Expand All @@ -61,15 +61,15 @@ If a field is updatable, a `<ModelName>Revisions` table is created (containing o

### `deletable`

When `deletable` is `true`, the entity can be created using a dedicated graphql `delete<ModelName>` mutation.
When `deletable` is `true`, the entity can be created using a dedicated GraphQL `delete<ModelName>` mutation.

This is a soft delete (the `deleted` field is set to `true`), and the entity can be restored with the graphql `restore<ModelName>` mutation.
This is a soft delete (the `deleted` field is set to `true`), and the entity can be restored with the GraphQL `restore<ModelName>` mutation.

`deletable` also accepts an object to override properties of the implicitly generated `deleted`, `deletedBy` and `deletedAt` fields.

### `queriable`

When `queriable` is `true` a graphql `Query` becomes available to fetch exactly one element by id.
When `queriable` is `true` a GraphQL `Query` becomes available to fetch exactly one element by id.

For example, with

Expand All @@ -82,7 +82,7 @@ For example, with
}
```

the following graphql query becomes possible
the following GraphQL query becomes possible

```graphql
query {
Expand All @@ -94,7 +94,7 @@ query {

### `listQueriable`

When `listQueriable` is `true` a graphql `Query` becomes available to fetch a list of elements of this model.
When `listQueriable` is `true` a GraphQL `Query` becomes available to fetch a list of elements of this model.

For example, with

Expand All @@ -107,7 +107,7 @@ For example, with
}
```

the following graphql query becomes possible
the following GraphQL query becomes possible

```graphql
query {
Expand All @@ -123,15 +123,15 @@ The name of the field that ought to be used as display value, e.g. a `Post`'s `t

### `defaultOrderBy`

An array of orders with the same structure as the `orderBy` parameters in graphql queries. The implicit default order by is `[{ createdAt: 'DESC }]`.
An array of orders with the same structure as the `orderBy` parameters in GraphQL queries. The implicit default order by is `[{ createdAt: 'DESC }]`.

### `fields`

An array of fields. See [fields](./fields)

## Scalars

Used for graphql scalars, e.g.
Used for GraphQL scalars, e.g.

```ts
{
Expand Down
10 changes: 5 additions & 5 deletions docs/docs/3-fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,23 @@ For more details, see section on [kinds](#kinds) below.

### `type`

This represents the graphql "return type", which can be a primitive or a separate model (depending on the [kind](#kinds)).
This represents the GraphQL "return type", which can be a primitive or a separate model (depending on the [kind](#kinds)).

### `description`

Will appear as description in the graphql schema.
Will appear as description in the GraphQL schema.

### `list`

If `list` is `true` the result is an array.

### `nonNull`

Will make the field required both in the graphql schema and in the database.
Will make the field required both in the GraphQL schema and in the database.

### `defaultValue`

Will set this as default value in graphql mutations and in the database.
Will set this as default value in GraphQL mutations and in the database.

### `args`

Expand Down Expand Up @@ -309,7 +309,7 @@ Available postfixes are:

### `queriable`

`true` by default. If explicitly set to `false`, the field won't be queriable via graphql.
`true` by default. If explicitly set to `false`, the field won't be queriable via GraphQL.

Also accepts an object that defines a list of `roles` to restrict access to specific roles.

Expand Down
12 changes: 6 additions & 6 deletions docs/docs/4-generation.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Code generation

`graphql-magic` generates a lot of utility code for you based on the models, in particular typescript types.
`graphql-magic` generates a lot of utility code for you based on the models, in particular TypeScript types.

This can be done directly with `npx gqm generate`.

Expand All @@ -10,20 +10,20 @@ During the first run, the tool applies the following changes to the repo:
* Add local database connection variables to `.env` file.
* Add generated folder to `.gitignore`
* Generate `models.ts` file (if not present).
* Generate a basic `get-me.ts` example graphql query.
* Generate a basic `get-me.ts` example GraphQL query.
* Generate the `execute.ts` file for the execution

With each application, it generates the following files in the configured "generated" folder:

* `schema.graphql` - the schema of the api, for reference
* `schema.graphql` - the schema of the API, for reference
* `models.json` - the final models array, including generated fields such as "id","createdBy"... for reference
* `api/index.ts` - the server-side model typescipt types
* `client/index.ts` - the client-side typescript types for the provided queries
* `api/index.ts` - the server-side model TypeScript types
* `client/index.ts` - the client-side TypeScript types for the provided queries
* `client/mutations.ts` - standard mutation queries for all models
* `db/index.ts` - types for data from/to the database
* `db/knex.ts` - types to extend the `knex` query builder

Whenever the models have been changed, it is necessary regenerate this code.
Whenever the models have been changed, it is necessary to regenerate this code.
It is recommended to create a `package.json` script and to always generate code after install (or with `npm run generate`):

```
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/6-graphql-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ export const executeGraphql = async <T, V = undefined>(
}
```

This is where you can set up your graphql server with
This is where you can set up your GraphQL server with

* user authentication (see the [Tutorial](./tutorial) for an example with auth0)
* custom resolvers (see [Custom resolvers](#custom-resolvers))
* mutation hooks (see [Mutation hooks](#mutation-hooks))

## Graphql API

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.
If you need client side querying, use `executeGraphql` to create a graphql endpoint, e.g. in `src/app/api/graphql/route.ts`:
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.
If you need client side querying, use `executeGraphql` to create a GraphQL endpoint, e.g. in `src/app/api/graphql/route.ts`:

```ts
export const POST = (req) => {
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/7-graphql-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {

### Server side

On the server side, and with `next.js` server actions, a graphql api becomes unnecessary, and you can execute queries directly using `executeGraphql`:
On the server side, and with `next.js` server actions, a GraphQL API becomes unnecessary, and you can execute queries directly using `executeGraphql`:

```tsx
import { GetMeQuery, GetPostsQuery } from "@/generated/client";
Expand Down Expand Up @@ -48,7 +48,7 @@ async function Posts({ me }: { me: GetMeQuery['me'] }) {

### Client side

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).
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).

```tsx
import { GetMeQuery, GetPostsQuery } from "@/generated/client";
Expand Down Expand Up @@ -148,4 +148,4 @@ async function CreatePost() {
}
```

Just like with queries, if is necessary to perform mutations on the client, use a graphql client instead of `executeGraphql`.
Just like with queries, if is necessary to perform mutations on the client, use a GraphQL client instead of `executeGraphql`.
Loading