ariadne-graphql-modules
ariadne-graphql-modules copied to clipboard
Code-first API
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
Maybe wrap it around Pydantic so that FastAPI folks would jump on it too :smile:
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
.
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.