Skip to content

Commit cd90856

Browse files
committed
release: 1.1.0
1 parent 79a3c84 commit cd90856

File tree

6 files changed

+474
-1
lines changed

6 files changed

+474
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Changelog and release notes
22

3-
## Unreleased
3+
<!-- ## Unreleased -->
44
<!-- here goes all the unreleased changes descriptions -->
5+
6+
## v1.1.0
57
### Features
68
- allow passing custom validation function as `validate` option to `buildSchema`
79
- support defining deprecation reason and description of enum members (#714)
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: Enums
3+
id: version-1.1.0-enums
4+
original_id: enums
5+
---
6+
7+
Nowadays almost all typed languages have support for enumerated types, including TypeScript. Enums limit the range of a variable's values to a set of predefined constants, which makes it easier to document intent.
8+
9+
GraphQL also has enum type support, so TypeGraphQL allows us to use TypeScript enums in our GraphQL schema.
10+
11+
## Creating enum
12+
13+
Let's create a TypeScript enum. It can be a numeric or string enum - the internal values of enums are taken from the enum definition values and the public names taken from the enum keys:
14+
15+
```typescript
16+
// implicit value 0, 1, 2, 3
17+
enum Direction {
18+
UP,
19+
DOWN,
20+
LEFT,
21+
RIGHT,
22+
}
23+
24+
// or explicit values
25+
enum Direction {
26+
UP = "up",
27+
DOWN = "down",
28+
LEFT = "left",
29+
RIGHT = "right",
30+
}
31+
```
32+
33+
To tell TypeGraphQL about our enum, we would ideally mark the enums with the `@EnumType()` decorator. However, TypeScript decorators only work with classes, so we need to make TypeGraphQL aware of the enums manually by calling the `registerEnumType` function and providing the enum name for GraphQL:
34+
35+
```typescript
36+
import { registerEnumType } from "type-graphql";
37+
38+
registerEnumType(Direction, {
39+
name: "Direction", // this one is mandatory
40+
description: "The basic directions", // this one is optional
41+
});
42+
```
43+
44+
In case we need to provide additional GraphQL-related config for values, like description or deprecation reason, we can use `valuesConfig` property and put the data inside it, e.g.:
45+
46+
```typescript
47+
enum Direction {
48+
UP = "UP",
49+
DOWN = "DOWN",
50+
LEFT = "LEFT",
51+
RIGHT = "RIGHT",
52+
SIDEWAYS = "SIDEWAYS",
53+
}
54+
55+
registerEnumType(Direction, {
56+
name: "Direction",
57+
description: "The basic directions",
58+
valuesConfig: {
59+
SIDEWAYS: {
60+
deprecationReason: "Replaced with Left or Right",
61+
},
62+
RIGHT: {
63+
description: "The other left",
64+
},
65+
},
66+
});
67+
```
68+
69+
This way, the additional info will be emitted in the GraphQL schema:
70+
71+
```graphql
72+
enum Direction {
73+
UP
74+
DOWN
75+
LEFT
76+
"""
77+
The other left
78+
"""
79+
RIGHT
80+
SIDEWAYS @deprecated(reason: "Replaced with Left or Right")
81+
}
82+
```
83+
84+
## Using enum
85+
86+
The last step is very important: TypeScript has limited reflection ability, so this is a case where we have to explicitly provide the enum type for object type fields, input type fields, args, and the return type of queries and mutations:
87+
88+
```typescript
89+
@InputType()
90+
class JourneyInput {
91+
@Field(type => Direction) // it's very important
92+
direction: Direction;
93+
}
94+
```
95+
96+
Without this annotation, the generated GQL type would be `String` or `Float` (depending on the enum type), rather than the `ENUM` we are aiming for.
97+
98+
With all that in place, we can use our enum directly in our code 😉
99+
100+
```typescript
101+
@Resolver()
102+
class SpriteResolver {
103+
private sprite = getMarioSprite();
104+
105+
@Mutation()
106+
move(@Arg("direction", type => Direction) direction: Direction): boolean {
107+
switch (direction) {
108+
case Direction.Up:
109+
this.sprite.position.y++;
110+
break;
111+
case Direction.Down:
112+
this.sprite.position.y--;
113+
break;
114+
case Direction.Left:
115+
this.sprite.position.x--;
116+
break;
117+
case Direction.Right:
118+
this.sprite.position.x++;
119+
break;
120+
default:
121+
// it will never be hitten ;)
122+
return false;
123+
}
124+
125+
return true;
126+
}
127+
}
128+
```
129+
130+
## Interoperability
131+
132+
Enums in TypeGraphQL are designed with server side in mind - the runtime will map the string value from input into a corresponding enum value, like `"UP"` into `0`. While this is very handy e.g. for mapping database values into GraphQL API enum names, it makes it unusable on the query side because `Direction.UP` will put `0` in the query which is an invalid value (should be `UP`).
133+
134+
So if we would like to share the types definition and use the enum on the client side app or use the enums directly on the server app e.g. in tests, we have to use the direct mapping of the enum member names with values, e.g.:
135+
136+
```typescript
137+
enum Direction {
138+
UP = "UP",
139+
DOWN = "DOWN",
140+
LEFT = "LEFT",
141+
RIGHT = "RIGHT",
142+
}
143+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: Examples
3+
sidebar_label: List of examples
4+
id: version-1.1.0-examples
5+
original_id: examples
6+
---
7+
8+
On the [GitHub repository](https://github.com/MichalLytek/type-graphql) there are a few simple examples of how to use different TypeGraphQL features and how well they integrate with 3rd party libraries.
9+
10+
All examples have an `examples.gql` file with sample queries/mutations/subscriptions that we can execute.
11+
12+
## Basics
13+
14+
- [Simple usage of fields, basic types and resolvers](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/simple-usage)
15+
16+
## Advanced
17+
18+
- [Enums and unions](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/enums-and-unions)
19+
- [Subscriptions (simple)](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/simple-subscriptions)
20+
- [Subscriptions (using Redis)](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/redis-subscriptions)
21+
- [Interfaces](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/interfaces-inheritance)
22+
- [Extensions (metadata)](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/extensions)
23+
24+
## Features usage
25+
26+
- [Dependency injection (IoC container)](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/using-container)
27+
- [Scoped containers](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/using-scoped-container)
28+
- [Authorization](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/authorization)
29+
- [Validation](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/automatic-validation)
30+
- [Custom validation](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/custom-validation)
31+
- [Types inheritance](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/interfaces-inheritance)
32+
- [Resolvers inheritance](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/resolvers-inheritance)
33+
- [Generic types](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/generic-types)
34+
- [Mixin classes](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/mixin-classes)
35+
- [Middlewares and Custom Decorators](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/middlewares-custom-decorators)
36+
- [Query complexity](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/query-complexity)
37+
38+
## 3rd party libs integration
39+
40+
- [TypeORM (manual, synchronous) \*](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/typeorm-basic-usage)
41+
- [TypeORM (automatic, lazy relations) \*](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/typeorm-lazy-relations)
42+
- [MikroORM](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/mikro-orm)
43+
- [Typegoose](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/typegoose)
44+
- [Apollo federation](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/apollo-federation)
45+
- [Apollo Engine (Apollo Cache Control) \*\*](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/apollo-engine)
46+
- [Apollo client state](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/apollo-client)
47+
- [GraphQL Modules](https://github.com/MichalLytek/type-graphql/tree/v1.1.0/examples/graphql-modules)
48+
49+
_\* Note that we need to edit the TypeORM example's `index.ts` with the credentials of our local database_
50+
51+
_\*\* Note that we need to provide an `APOLLO_ENGINE_API_KEY` env variable with our own API key_
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Prisma 2 Integration
3+
sidebar_label: Prisma 2
4+
id: version-1.1.0-prisma
5+
original_id: prisma
6+
---
7+
8+
TypeGraphQL provides an integration with Prisma 2 by the [`typegraphql-prisma` package](https://www.npmjs.com/package/typegraphql-prisma).
9+
10+
It generates the type classes and CRUD resolvers based on the Prisma schema, so you can execute complex queries or mutations that corresponds to the Prisma actions, without having to write any code for that.
11+
12+
## Overview
13+
14+
To make use of the prisma integration, first you need to add a new generator to the `schema.prisma` file:
15+
16+
```sh
17+
generator typegraphql {
18+
provider = "typegraphql-prisma"
19+
output = "../src/generated/typegraphql-prisma"
20+
}
21+
```
22+
23+
Then, after running `prisma generate` you can import the generated classes and use them to build your schema:
24+
25+
```typescript
26+
import { User, UserRelationsResolver, UserCrudResolver } from "./generated/typegraphql-prisma";
27+
28+
const schema = await buildSchema({
29+
resolvers: [CustomUserResolver, UserRelationsResolver, UserCrudResolver],
30+
validate: false,
31+
});
32+
```
33+
34+
So you will be able to execute such complex query that talks with the db in just a few minutes!
35+
36+
```graphql
37+
query GetSomeUsers {
38+
users(where: { email: { contains: "prisma" } }, orderBy: { name: desc }) {
39+
id
40+
name
41+
email
42+
posts(take: 10, orderBy: { updatedAt: desc }) {
43+
published
44+
title
45+
content
46+
}
47+
}
48+
}
49+
```
50+
51+
## Documentation and examples
52+
53+
To read about all the `typegraphql-prisma` features, like exposing selected Prisma actions or changing exposed model type name, as well as how to write a custom query or how to add some fields to model type, please check the docs [on the separate GitHub repository](https://github.com/MichalLytek/typegraphql-prisma/blob/main/Readme.md).
54+
55+
You can find there also some examples and more detailed info about the installation and the configuration.

0 commit comments

Comments
 (0)