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

Add docs for schema transformations #2253

Merged
merged 4 commits into from
May 29, 2024
Merged
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
30 changes: 30 additions & 0 deletions vuepress/docs/docs/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,3 +607,33 @@ given Schema[Any, User] = customObj("User", Some("A user of the service"))(
```
</code-block>
</code-group>

## Schema transformations

It is also possible to modify your schemas after they have been generated.
This can be useful if you want to rename or remove particular types, fields or arguments from your schema without modifying the related Scala types.

For that, simply use the `GraphQL#transform` method and provide one of the possible transformers:
- `RenameType` to rename types (providing a list of `(OldName -> NewName)`)
- `RenameField` to rename a field (providing a list of `(TypeName -> oldName -> newName)`)
- `RenameArgument` to rename an argument (providing a list of `(TypeName -> fieldName -> oldArgumentName -> newArgumentName)`)
- `ExcludeField` to exclude a field (providing a list of `(TypeName -> fieldToBeExcluded)`)
- `ExcludeInputField` to exclude an input field (providing a list of `(TypeName -> fieldToBeExcluded)`)
- `ExcludeArgument` to exclude an argument (providing a list of `(TypeName -> fieldName -> argumentToBeExcluded)`)


In the following example, we can expose 2 different APIs created from the same schema: the v1 API will not expose the `nicknames` field of the `Character` type.
```scala
case class Queries(character: Character)

case class Character(
name: String,
@GQLDescription("experimental field")
nicknames: List[String]
)

val apiBeta = graphQL(RootResolver(Queries(???, ???)))
val apiV1 = apiBeta.transform(Transformer.ExcludeField("Character" -> "nicknames"))
```

You can also create your own transformers by extending the `Transformer` trait and implementing its methods.
Loading