From 3d43f542f6f7fb172a1eda354e881a22148b8691 Mon Sep 17 00:00:00 2001 From: Kevin Sylvestre Date: Thu, 14 Nov 2024 14:11:54 -0800 Subject: [PATCH] Document `graphql_type` on @strawberry.field This documents a use case where the GraphQL type is not the same as data being used. This is useful if you are using an ORM for your data and building an API layer against it. --- docs/general/queries.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/general/queries.md b/docs/general/queries.md index 14963a51bd..8bbec13e98 100644 --- a/docs/general/queries.md +++ b/docs/general/queries.md @@ -55,6 +55,30 @@ class Query: return "Strawberry" ``` +The decorator syntax supports specifying a `graphql_type` for cases when the +return type of the function does not match the GraphQL type: + +```python +class User: + id: str + name: str + + def __init__(self, id: int, name: str): + self.id = id + self.name = name + +@strawberry.type(name="User") +class UserType: + id: strawberry.ID + name: str + +@strawberry.type +class Query: + @strawberry.field(graphql_type=UserType) + def user(self) -> User + return User(id="fake", name="Ringo Star") +``` + ## Arguments GraphQL fields can accept arguments, usually to filter out or retrieve specific