Skip to content

Commit

Permalink
docs(enums): add info about advanced enums configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Oct 12, 2020
1 parent a72de57 commit 3df2149
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@
<!-- here goes all the unreleased changes descriptions -->
### Features
- allow passing custom validation function as `validate` option to `buildSchema`
- support defining deprecation reason and description of enum members (#714)
### Fixes
- **Breaking Change**: throw error when wrong type of value provided as arg or input for `GraphQLISODateTime` and `GraphQLTimestamp` scalars
- don't include in schema the fields declared as `@FieldResolver` when that resolvers classes aren't provided in `resolvers` array
Expand Down
42 changes: 41 additions & 1 deletion docs/enums.md
Expand Up @@ -28,7 +28,7 @@ enum Direction {
}
```

To tell TypeGraphQL about our enum, we would ideally mark the enums with the `@GraphQLEnumType()` 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:
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:

```typescript
import { registerEnumType } from "type-graphql";
Expand All @@ -39,6 +39,46 @@ registerEnumType(Direction, {
});
```

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

```typescript
enum Direction {
UP = "UP",
DOWN = "DOWN",
LEFT = "LEFT",
RIGHT = "RIGHT",
SIDEWAYS = "SIDEWAYS",
}

registerEnumType(Direction, {
name: "Direction",
description: "The basic directions",
valuesConfig: {
SIDEWAYS: {
deprecationReason: "Replaced with Left or Right",
},
RIGHT: {
description: "The other left",
},
},
});
```

This way, the additional info will be emitted in the GraphQL schema:

```graphql
enum Direction {
UP
DOWN
LEFT
"""
The other left
"""
RIGHT
SIDEWAYS @deprecated(reason: "Replaced with Left or Right")
}
```

## Using enum

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:
Expand Down

0 comments on commit 3df2149

Please sign in to comment.