Idlewild
Use the amazing Python PLY library to parse GraphQL IDL into a schema.
Purpose
Idlewild is a Python library for creating GraphQL schemas from GraphQL IDL format. Optionally it also allows you to register data fetchers for fields and these can be implemented in your own application.
It was inspired by a similar idea in JVM called graphql-java-tools.
It is very small and consists of a parser, builder and tools module. The parser uses PLY to build a simple grammar which the builder turns into a GraphQL schema using graphql-core.
As stated on graphql-core:
-
An overview of the GraphQL language is available in the README for the Specification for GraphQL.
-
Examples can be found in tests in this repository.
Concepts
To build a schema, we need only a schema definition in this form:
schema {
query: QueryType
}
type QueryType {
hero(episode: Episode): Character
human(id : String) : Human
droid(id: ID!): Droid
}
...
At the time of building the schema we can attach data fetchers or type resolvers to fields.
from idlewild.schematools import (
parse_and_build_idl_file,
setup_logging
)
from .fixtures import (
get_hero,
get_human,
get_droid,
get_friends,
)
datafetchers = {
'human': lambda human, info, **args: get_human(args['id']),
'droid': lambda droid, info, **args: get_droid(args['id']),
'hero': lambda root, info, **args: get_hero(args.get('episode')),
'friends': lambda character, *_: get_friends(character),
}
interface_resolvers = {
'Character': lambda character, info: (
'Human' if get_human(character.id) else 'Droid'
)
}
# Build the schema
schema = parse_and_build_idl_file(
'starwars.graphqls',
resolver_mappings=datafetchers,
interface_resolver_mappings=interface_resolvers,
)
StarWarsSchema = schema