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

How to load graphql schema from a file ? #211

yogeshpandey opened this Issue Jul 7, 2017 · 9 comments


None yet
10 participants

yogeshpandey commented Jul 7, 2017

Is there a way, where I can load schema from files or string. I am building versioned apis using graphql-go, therefore | need to load set of schema based on api version.

@yogeshpandey yogeshpandey changed the title from Loading graphql schema from a file to How to load graphql schema from a file ? Jul 7, 2017


This comment has been minimized.

rydrman commented Jul 27, 2017

I would also be curious to know if this was considered. I have been digging around but so far all I can think of is to parse the file using the language/parser package and then manually generate the necessary object definitions based on the result.

@chris-ramon chris-ramon added the question label Aug 6, 2017


This comment has been minimized.

ulm0 commented Sep 8, 2017

It'd be really great to be able to do this, as of now you can achieve this in Java GraphQL implementation


This comment has been minimized.

tiaanl commented Oct 7, 2017

I'm guessing it would be a port of this from the graphql-js package:


This comment has been minimized.

hugowetterberg commented Oct 8, 2017

The schema parser from SourceGraphs GraphQL might be a good candidate as well:


This comment has been minimized.

djmcgreal commented Dec 18, 2017

Is anyone doing anything for this? Is there a plan for such support?
Thanks! Dan.


This comment has been minimized.

tcolgate commented May 23, 2018

Since reflection isn't used, and fields/objects are represented by concretete types, it seems that this should be completely doable from a parsed AST. ResolveFn could be added after the fact.


This comment has been minimized.


JulienBreux commented Jul 27, 2018

Just create a generator :)


This comment has been minimized.

ulm0 commented Jul 28, 2018

Better yet, give a try to


This comment has been minimized.

Zaba505 commented Nov 8, 2018

This can be simply done given the fact that GraphQL schemas are self-describing through introspection. Thus, graphql.Schema could implement json.Marshaller and json.Unmarshaller. The only API change to graphql.Schema would have to be some way to add Resolvers to type decls. at runtime or you could just walk the schema yourself starting from Schema.QueryType(). This solution extends @tcolgate's comment.

Saving a Schema

schema := // Create a schema
b, _ := schema.MarshallJSON()
f, _ := os.Open("my_api.gql")
defer f.Close()

f.Write(b) // json.NewEncoder(f).Encode(schema) could also be used too

Loading a Schema

schema := &graphql.Schema{} // Note: this would be inited as empty
b, _ := ioutil.ReadFile("my_api.gql")

schema.UnMarshallJSON(b) // json.NewDecoder(f).Decode(schema) could also be used too

// Now, all you'd have to do is add FieldResolverFns to your types

Mock MarshallJSON impl.

func (s *Schema) MarshallJSON() ([]byte, error) {
     res := graphql.Do(s, `{
                   __schema { /* Add all fields here */ }
     if len(r.Errors) > 0 {
           return nil, errors.New("report a meaningful error here")
     return json.Marshall(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment