A GraphQL field builder utilizing Go generics with extra utilities and features. Depends on graphql-go.
type PostsQuery struct {
Limit int `json:"limit"`
Query null.String `json:"query"`
Tags []string `json:"tags"`
}
type Post struct {
ID int `json:"id"`
Body string `json:"body"`
}
func buildField() graphql.Fields {
return graphql.Fields{
"GetPosts": &graphql.Field{
Description : "Returns posts",
Type: graphql.NewList(graphql.NewObject(
graphql.ObjectConfig{
Name: "Post",
Fields: graphql.Fields{
"id" : &graphql.Field{
Type: graphql.Int,
},
"body" : &graphql.Field{
Type: graphql.String,
},
},
},
)),
Args: map[string]*graphql.ArgumentConfig{
"limit": {
Type: graphql.NewNonNull(graphql.Int),
},
"query": {
Type: graphql.String,
},
"tags": {
Type: graphql.NewList(graphql.String),
},
},
Resolve: func(p graphql.ResolveParams) (ret interface{}, err error) {
var q PostsQuery
if err = mapstructure.Decode(p.Args, &q); err != nil {
return
}
/// resolver logic
return
},
},
}
}
type PostsQuery struct {
Limit int `json:"limit"`
Query null.String `json:"query"`
Tags []string `json:"tags"`
}
type Post struct {
ID int `json:"id"`
Body string `json:"body"`
}
func buildFieldGrapher() graphql.Fields{
return graphql.Fields{
"GetPosts" : grapher.NewFieldBuilder[PostsQuery, []Post]().
WithDescription("Returns posts").
WithResolver(func(p graphql.ResolveParams, query PostsQuery) (ret []Post, err error) {
// resolver logic
return
}).
MustBuild(),
}
}
This package is still a work-in-progress. The feature candidates for the v1 release is :
- Struct to
graphql.Object
/graphql.InputObject
translator utilizing struct tags - Struct to
map[string]*graphql.ArgumentConfig{}
- Configurable struct tags translator
- Schema field builder utilizing go 1.18 Generics feature ensuring type safety
- Resolver middlewares
- Resolver arg validator
- Mux-like schema building with
MutationQueryCollector
Most features are done, but needs a little bit of polishing.
If you have any feature ideas, do not hesitate to tell and open a new issue in the Issues tab!
Contributions in any way, shape, or form is super welcomed! If you have any issues, ideas or even a question just open a new issue, or make a pull request.
MIT