ariadne-graphql-modules icon indicating copy to clipboard operation
ariadne-graphql-modules copied to clipboard

Code-first API

Open rafalp opened this issue 2 years ago • 2 comments

We should support code-first approach to type definition in addition to current schema-first one. My original idea was to do this through decorator (like how Strawberry does it) but my current idea is to use universal base types for both code-first and schema-first approaches to GraphQL schema definition. The differentiation factor between those two would be a presence of __schema__ atribute on the type:

# Schema first object
class Query(ObjectType):
    __schema__ = gql(
        """
        type Query {
            message: String!
            year: Int!
        }
        """
    )

    @field
    def message(*_):
        return "Hello world!"

    @field
    def year(*_):
        return date.today().year


# Code first object
class Query(ObjectType):
    @field
    def message(*_) -> str:
        return "Hello world!"

    @field
    def year(*_) -> str:
        return date.today().year

rafalp avatar Oct 27 '22 15:10 rafalp

Maybe wrap it around Pydantic so that FastAPI folks would jump on it too :smile:

dekoza avatar Nov 03 '22 16:11 dekoza

Yup, it should work with Pydantic too! The prototype I has decorator that introspects given type and sets __ariadne_graphql__ attribute on it with GraphQLObjectModel that make_executable_schema looks for to create GraphQLObjectType.

rafalp avatar Nov 03 '22 16:11 rafalp

Currently, we are working on PR #32, which covers both schema-first and code-first approaches. Regarding Pydantic, we need to consider and plan whether and how we want to implement it.

DamianCzajkowski avatar Jul 29 '24 07:07 DamianCzajkowski