Giter Site home page Giter Site logo

grapher's Introduction

grapher Go Reference Go Report Card codecov

A GraphQL field builder utilizing Go generics with extra utilities and features. Depends on graphql-go.

Examples

Without grapher

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
			},
		},
	}
}

With grapher

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(),
	}
}

v1 Development

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!

Contributing

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.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.