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

Plugin hook to allow other plugins to define GraphQL APIs #69

Closed
simonw opened this issue Dec 3, 2020 · 9 comments
Closed

Plugin hook to allow other plugins to define GraphQL APIs #69

simonw opened this issue Dec 3, 2020 · 9 comments
Labels
enhancement New feature or request idea

Comments

@simonw
Copy link
Owner

simonw commented Dec 3, 2020

Bonus idea: if https://github.com/simonw/datasette-graphql provided its own plugin hook then plugins like this one could expose a GraphQL API as well.

Originally posted by @simonw in simonw/datasette-ripgrep#18 (comment)

@simonw simonw added enhancement New feature or request idea labels Dec 3, 2020
@simonw
Copy link
Owner Author

simonw commented Dec 3, 2020

from datasette_graphql import hookimpl

def extra_fields(datasette):
    return [
        ("hello", graphene.String(), lambda root, info: "Hello world"),
    ]

Returns a name, a field type and a resolve function - these are then added to the query at the root of the Schema.

@simonw
Copy link
Owner Author

simonw commented Dec 3, 2020

Would use the same optional async def inner() pattern as other hooks.

@simonw
Copy link
Owner Author

simonw commented Dec 3, 2020

Open question: what if the name from a plugin collides with the name of a SQLite table?

Could solve that by automatically renaming the GraphQL field for that table.

@markhalonen
Copy link

The Postgraphile project handles many of the same types of issues, you may want to look there for inspiration. It has awesome plugins

@simonw
Copy link
Owner Author

simonw commented Jul 2, 2022

I proved in https://github.com/simonw/datasette-low-disk-space-hook that you can add new plugin hooks as part of another plugin, see also this TIL: https://til.simonwillison.net/datasette/register-new-plugin-hooks

@simonw
Copy link
Owner Author

simonw commented Jul 3, 2022

I'm going to experiment with this hook using https://datasette.io/plugins/datasette-packages since it's a really simple plugin (much simpler than datasette-ripgrep).

@simonw
Copy link
Owner Author

simonw commented Jul 3, 2022

Here's the hookspec:

from pluggy import HookspecMarker

hookspec = HookspecMarker("datasette")


@hookspec
def graphql_extra_fields(datasette):
    "A list of (name, field_type) tuples to include in the GraphQL schema"

And an example plugin implementation:

@hookimpl
def graphql_extra_fields():
    class Package(graphene.ObjectType):
        "An installed package"
        name = graphene.String()
        version = graphene.String()

    return [
        (
            "packages",
            graphene.Field(
                graphene.List(Package),
                description="List of installed packages",
                resolver=lambda root, info: [
                {"name": d.project_name, "version": d.version}
                for d in sorted(
                    pkg_resources.working_set, key=lambda d: d.project_name.lower()
                )
            ])
        ),
    ]

@simonw simonw closed this as completed in df76578 Jul 3, 2022
simonw added a commit that referenced this issue Jul 3, 2022
simonw added a commit to simonw/datasette-packages that referenced this issue Jul 3, 2022
simonw added a commit to simonw/datasette-packages that referenced this issue Jul 3, 2022
simonw added a commit to simonw/latest-datasette-with-all-plugins that referenced this issue Jul 3, 2022
simonw added a commit that referenced this issue Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request idea
Projects
None yet
Development

No branches or pull requests

2 participants