From d178bf64a64e6bb1aafb345711d4dff1a6920f44 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..74164a868a 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: str, 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="ringo", name="Ringo") +``` + ## Arguments GraphQL fields can accept arguments, usually to filter out or retrieve specific