From 3df214916af9e90c181509578eadf375672d87b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Lytek?= Date: Sun, 4 Oct 2020 21:50:34 +0200 Subject: [PATCH] docs(enums): add info about advanced enums configuration --- CHANGELOG.md | 1 + docs/enums.md | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81553b30a..96095a73b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### 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 diff --git a/docs/enums.md b/docs/enums.md index 35a370679..5211b83c1 100644 --- a/docs/enums.md +++ b/docs/enums.md @@ -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"; @@ -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: