graphql-core-legacy
graphql-core-legacy copied to clipboard
Loading Schema from file
I'm not sure if this is already provided, but I cant seem to find anything in the docementation and after reading some of the code in this library.
Given some schema language file e.g.:
type Character {
name: String!
appearsIn: [Episode]!
}
I would like to load it as a GraphQLSchema
object
I'm not yet sure about loading from an SDL file, but you could load easily from a JSON file:
In [1]: from graphql import build_client_schema
In [2]: import json
In [3]: schema = build_client_schema(json.loads(open('~/grapql/schema.json').read()))
In [4]: schema
Out[4]: <graphql.type.schema.GraphQLSchema at 0x10b668808>
@mschmo what's the json file looks like?
I've been using:
from graphql import build_ast_schema
from graphql.language.parser import parse
def parse_schema(document):
return build_ast_schema(parse(document))
which seems to work.
I'll just note if you're in the dev branch you can load from a schema file like so:
with open('./schema.graphqls') as source:
document = parse(source.read())
schema = build_ast_schema(document)
Schema will then be a GraphQLSchema object.
In order for this to work though, your schema.graphqls file has to have a Query type and a defined schema object like so:
type Query {
hello(name: String): String
}
schema {
query: Query
}
I think that using this new library solves the problem: https://graphql-core-next.readthedocs.io/en/latest/usage/sdl.html