graphql-core-legacy icon indicating copy to clipboard operation
graphql-core-legacy copied to clipboard

Loading Schema from file

Open MichaelAquilina opened this issue 7 years ago • 5 comments

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

MichaelAquilina avatar Aug 15 '17 14:08 MichaelAquilina

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 avatar Sep 13 '17 23:09 mschmo

@mschmo what's the json file looks like?

lisongx avatar Sep 26 '17 03:09 lisongx

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.

mwilliamson avatar Oct 04 '17 08:10 mwilliamson

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
}

KaySackey avatar Oct 24 '17 15:10 KaySackey

I think that using this new library solves the problem: https://graphql-core-next.readthedocs.io/en/latest/usage/sdl.html

x0y-gt avatar Feb 09 '19 21:02 x0y-gt