From a733167c6f7a0eb12548eb44bfad0a62490de83d Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Thu, 19 Sep 2024 20:25:10 +0300 Subject: [PATCH] doc: add MapsTo annotation to graphql tutorial (#4219) --- doc/md/tutorial-todo-gql-field-collection.md | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/doc/md/tutorial-todo-gql-field-collection.md b/doc/md/tutorial-todo-gql-field-collection.md index 4d1821e25..1f4150064 100644 --- a/doc/md/tutorial-todo-gql-field-collection.md +++ b/doc/md/tutorial-todo-gql-field-collection.md @@ -149,6 +149,27 @@ SELECT DISTINCT `todos`.`id`, `todos`.`text`, `todos`.`created_at`, `todos`.`sta If you're having trouble running this example, go to the [first section](#clone-the-code-optional), clone the code and run the example. +## Field Mappings + +The [`entgql.MapsTo`](https://pkg.go.dev/entgo.io/contrib/entgql#MapsTo) allows you to add a custom field/edge mapping +between the Ent schema and the GraphQL schema. This is useful when you want to expose a field or edge with a different +name(s) in the GraphQL schema. For example: + +```go +// One to one mapping. +field.Int("priority"). + Annotations( + entgql.OrderField("PRIORITY_ORDER"), + entgql.MapsTo("priorityOrder"), + ) + +// Multiple GraphQL fields can map to the same Ent field. +field.Int("category_id"). + Annotations( + entgql.MapsTo("categoryID", "category_id", "categoryX"), + ) +``` + --- Well done! By using automatic field collection for our Ent schema definition, we were able to greatly improve the