Am I missing something, or is there no way to take advantage of the handy graphql schema definition language? It would be great to be able to use the schema language in the same way the javascript implementation does (with buildschema, I think).
There is a way, but not in Graphene.
import graphql
source = open('/path/to/graphql/schema/file')
ast = graphql.parse(source.read())
source.close()
schema = graphql.build_ast_schema(ast)
def resolve_users(args, context, info):
// do something
resolvers = {
'users': resolve_users
}
Then, in theory you can use graphql(schema, root_value=resolvers) to execute.
I tested parse the schema file to graphql schema, but not execute with my own resolvers yet.
I made a minimal example here:
https://gist.github.com/jasonphillips/d80642fc33d98cb34bad131adfcf6ed8
which could be further expanded, but covers the basics (simple resolvers).
@jasonphillips that is awesome! I would love for something like this to be included in graphene or installable from another package.
Would this approach also enable the output of the graphql schema language definition from an instance of graphene.Schema, or should I open a new issue to discuss that?
nm, can do this already with
from graphql.utils import schema_printer
schema_printer.print_schema(schema)
...and even that's unnecessary, as the __str__ method of graphene.Schema already does this.
There's a issue about that on https://github.com/graphql-python/graphql-core?
Because I want to implement a PR on graphql-core based on the @jasonphillips code
I think that using this new library solves the problem:
https://graphql-core-next.readthedocs.io/en/latest/usage/sdl.html
Most helpful comment
...and even that's unnecessary, as the
__str__method ofgraphene.Schemaalready does this.