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

Open
yogeshpandey opened this Issue Jul 7, 2017 · 9 comments

Comments

Projects
None yet
10 participants
@yogeshpandey

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

@rydrman

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

@ulm0

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 https://github.com/graphql-java/graphql-java/blob/36b0ec9214e9c40adeb6e8a52faeef0bacd71cf2/src/main/java/graphql/schema/idl/SchemaParser.java

@tiaanl

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:

https://github.com/graphql/graphql-js/blob/master/src/utilities/buildASTSchema.js#L505

@hugowetterberg

This comment has been minimized.

hugowetterberg commented Oct 8, 2017

The schema parser from SourceGraphs GraphQL might be a good candidate as well: https://github.com/neelance/graphql-go/blob/master/internal/schema/schema.go

@djmcgreal

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.

@tcolgate

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.

@JulienBreux

This comment has been minimized.

Contributor

JulienBreux commented Jul 27, 2018

Just create a generator :)

@ulm0

This comment has been minimized.

ulm0 commented Jul 28, 2018

Better yet, give a try to https://github.com/vektah/gqlgen

@Zaba505

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
schema.AddResolvers(...)

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