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

Code-first API #12

Closed
rafalp opened this issue Oct 27, 2022 · 3 comments
Closed

Code-first API #12

rafalp opened this issue Oct 27, 2022 · 3 comments
Assignees
Labels
next-api Support for both code and schema first approaches prio: high High priority
Milestone

Comments

@rafalp
Copy link
Contributor

rafalp commented Oct 27, 2022

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 rafalp self-assigned this Oct 27, 2022
@dekoza
Copy link

dekoza commented Nov 3, 2022

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

@rafalp
Copy link
Contributor Author

rafalp commented Nov 3, 2022

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 rafalp added the next-api Support for both code and schema first approaches label Dec 1, 2022
@rafalp rafalp changed the title Support declarative approach for type definitions Support code-first approach for type definitions Feb 24, 2023
@rafalp rafalp added the prio: high High priority label Jul 20, 2023
@rafalp rafalp added this to the Next API milestone Jul 20, 2023
@rafalp rafalp changed the title Support code-first approach for type definitions Code-first API Jul 20, 2023
@DamianCzajkowski
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
next-api Support for both code and schema first approaches prio: high High priority
Projects
None yet
Development

No branches or pull requests

3 participants