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

Implement GraphQL modules #306

Closed
patrys opened this issue Jan 28, 2020 · 4 comments
Closed

Implement GraphQL modules #306

patrys opened this issue Jan 28, 2020 · 4 comments
Assignees
Labels
discussion roadmap Feature that we want to have included
Milestone

Comments

@patrys
Copy link
Contributor

patrys commented Jan 28, 2020

GraphQL Modules are a JavaScript tool that allows types and resolvers to be grouped together into functional blocks that depend on each other. Modules can be composed to build larger parts, ultimately leading to a module that represents the entire application.

Importing a single module is easier than importing a list of types, resolvers, and any dependant types or resolvers. Modules are also a straightforward way to implement a code-first declarative interface like that offered by Strawberry. We could also provide tooling to build modules from Graphene and Strawberry objects.

@rafalp rafalp added this to the Ariadne 0.15 milestone Feb 11, 2022
@rafalp rafalp added discussion roadmap Feature that we want to have included labels Feb 11, 2022
@rafalp
Copy link
Contributor

rafalp commented Feb 11, 2022

I've had short brainstorming session with @patrys about this, and initial idea is to introduce new base class that's basically type definition + requirements + resolvers, something like this:

class User(ObjectType[models.User]):
    # Validated to contain exactly single type declaration
    __schema__ = gql(
        """
        type User {
            id: ID!
            name: String!
            email: String
            group: UserGroup!
            joinedAt: DateTime!
            lastVisit: DateTime
        }
        """
    )

    # Optional, allows mapping fields to resolver functions in python
    # if mapping is defined here but function is not declared on class, it will be autogenerated
    __resolvers__ = {
        "joinedAt": "joined_at",
        "lastVisit": "last_visit",
    }

    # Optional, list of other GraphQL types/objects this type requires
    __requires__ = [UserGroup]

    # Resolvers methods
    @staticmethod
    def email(obj, info) -> Optional[str]:
        if can_see_email(info.context, obj):
            return obj.email
        
        return None

    @staticmethod
    def last_visit(obj, info) -> Optional[datetime]:
        if can_see_activity(info.context, obj):
            return obj.last_visit
        
        return None

We could implement additional type checks in this class, eg. we could have __root__ = models.User attribute on it that would let us verify that group attribute on User model has type expected by UserGroup.__root__. Or we could raise an error when there's typo in resolver's name.

@rafalp
Copy link
Contributor

rafalp commented Feb 18, 2022

I've created feature branch where I'm experimenting on this implementation: new-object-api

My plan is to keep iterating on to get into somewhat working stage for queries, mutations and scalars. When that gets somewhat useable I would like to start including ariadne_future in our releases to let people play with it for small APIs

@rafalp rafalp self-assigned this Feb 18, 2022
@rafalp
Copy link
Contributor

rafalp commented Mar 23, 2022

I've moved modules implementation to ariadne-graphql-modules library. There was already single release of that lib to PYPI, but it will be broken until Ariadne 0.15 is released.

@rafalp
Copy link
Contributor

rafalp commented Apr 12, 2022

Closing this issue. Dev on GraphQL modules for Ariadne will continue in graphql-modules library linked above.

@rafalp rafalp closed this as completed Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion roadmap Feature that we want to have included
Projects
None yet
Development

No branches or pull requests

3 participants