udacity / graphb

GraphQL Query Builder for Go
MIT License
38 stars 20 forks source link

Unmarshall graphql query to use it with graphb #8

Open hubyhuby opened 4 years ago

hubyhuby commented 4 years ago

Feature request : The use case of this, is when you build a Go API that receives a graphql query that you want to validate and modify before passing it to your graph DB.

Basically : 1) you receive a string as mentionned in your example :
const queryTemplate = "query": " query an_operation_name { a_field_name (`

2) You want to marshall it to graphb. And modify it.

3)Marshall it and send it to the graph DB.

Thanks you for this library.

CreatCodeBuild commented 4 years ago

@hubyhuby It looks like you want to parse a query to an AST. You might be able to use some function in graph gopher. I don't know if Udacity is still maintaining this lib. I don't work for Udacity anymore. Can't really help you on this.

hubyhuby commented 4 years ago

@CreatCodeBuild Thanks for your help, Yes I wish to convert it to an AST. I found this lib that seems to do the job of parsing to an AST graphql queries. Then hopefully it is easy after modifying the request, to convert it back to a regular graphql request I will try it in the coming month. https://github.com/vektah/gqlparser/blob/6b4a7854a7ef7ee3f626490d1e2d22a823828fc6/ast/document_test.go#L14

CreatCodeBuild commented 4 years ago

@hubyhuby You are welcome.

But here is my warning. I basically wrote the whole library on an airplane with no internet. I took lots of short cuts and didn't follow the exact GraphQL spec. I didn't understand GraphQL's AST enough at that time. This library was definitely a nice try.

Here are the pros and cons:

Pros: Its main goal was to dynamically compose a query based on user input. For example, if a user only selects 2 items, then the selection set will only contain 2 fields. { getMyInfo { x, y } } vs { getMyInfo { x } } I think it's still unique in this capability compared to other query builders such as https://github.com/machinebox/graphql and https://github.com/shurcooL/graphql

Cons: This library is basically my undone work. The 2 other libraries are more complete to the spec.

According to your description, it's likely that you need to dynamically compose an AST so that this lib might be the right one.

If I can squeeze some time next year, I will love to continue this work on my fork or just write a new one because many API could be redesigned. As I now work fulltime as a GraphQL infrastructure engineer (in JS though), I have much deeper understanding of GraphQL now.

For example, I probably should consider using AST from https://github.com/vektah/gqlparser or https://github.com/graph-gophers/graphql-go instead of reimplementing the AST. It's likely that I will use vektah/gqlparser because it mirrors graphql-js which has a very nice AST implementation. graph-gophers/graphql-go 's AST is in /internal so that will be a headache to port out.

However, I may not have time to do this work anytime soon because

  1. My fulltime job doesn't use Go (sad).
  2. Currently, I am working on a more demanding problem: Loading a remote resolver to local. https://github.com/CreatCodeBuild/deno-graphql/tree/master/remote-graph It addresses a similar problem as Apollo Federation does but it focuses on loading a resolver(a subgraph) that you don't own into your schema/resolvers. For example, composing parts of GitHub schema into your schema. If this can be done, you can basically compose any SaaS that has a GraphQL API into your own schema and do GraphQL driven cross-organization programming. A new way to webhook if you wish.

Of course, the second half of this message has nothing to do with your feature request. Just want to provide you more context.

Good luck and Merry Christmas!