grapher

package module
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2023 License: MIT Imports: 6 Imported by: 0

README

grapher Go Reference Go Report Card codecov

Neat extra tooling for 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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArgValidatorFn

type ArgValidatorFn func(arg any) (err error)

type BuildRootSchemaConfig

type BuildRootSchemaConfig struct {
}

BuildRootSchemaConfig Configuration for building the root schema, currently a placeholder

type FieldBuilder

type FieldBuilder[argT any, outT any] interface {
	WithDescription(desc string) FieldBuilder[argT, outT]
	WithResolver(resolverFunc ResolverFn[argT, outT]) FieldBuilder[argT, outT]
	WithCustomArgValidator(fn ArgValidatorFn) FieldBuilder[argT, outT]

	Build() (field *graphql.Field, err error)
	MustBuild() *graphql.Field

	AddMiddleware(middlewareFn ResolverMiddlewareFn) FieldBuilder[argT, outT]
}

FieldBuilder interface, put NoArgs to use no args

func NewFieldBuilder

func NewFieldBuilder[argT any, outT any](cfgArgs ...FieldBuilderConfig) FieldBuilder[argT, outT]

type FieldBuilderConfig

type FieldBuilderConfig struct {
	Translator   Translator
	ArgValidator ArgValidatorFn
	Middlewares  []ResolverMiddlewareFn
}

type MutationQueryCollector

type MutationQueryCollector interface {
	PushMutations(mutations graphql.Fields)
	PushQueries(queries graphql.Fields)

	BuildRootSchema(optionalCfg ...BuildRootSchemaConfig) (schema graphql.Schema, err error)

	Mutations() graphql.Fields
	Queries() graphql.Fields
}

MutationQueryCollector A simple Collector struct for *graphql.Field maps for queries and mutations

func NewMutationQueryCollector

func NewMutationQueryCollector() MutationQueryCollector

NewMutationQueryCollector Returns a new mutationQueryCollector

type NoArgs

type NoArgs struct{}

type ResolverFn

type ResolverFn[argT any, outT any] func(p graphql.ResolveParams, arg argT) (outT, error)

type ResolverMiddlewareFn

type ResolverMiddlewareFn func(nextFn graphql.FieldResolveFn) graphql.FieldResolveFn

type TranslationMap

type TranslationMap map[string]graphql.Output

TranslationMap is a map that stores the graphql.Output according to it's type

type Translator

type Translator interface {
	Translate(t interface{}) (ret graphql.Output, err error)
	TranslateInput(t interface{}) (ret *graphql.InputObject, err error)
	TranslateArgs(t interface{}) (ret graphql.FieldConfigArgument, err error)

	MustTranslate(t interface{}) graphql.Output
	MustTranslateInput(t interface{}) *graphql.InputObject
	MustTranslateArgs(t interface{}) (ret graphql.FieldConfigArgument)
}

Translator purpose is to provide an easy way of translating various types into graphql types

func NewTranslator

func NewTranslator(args ...*TranslatorConfig) Translator

NewTranslator returns a new translator It also stores already translated graphql.Object/graphql.InputObject to eliminate duplicates

type TranslatorConfig

type TranslatorConfig struct {
	// PredefinedTranslation ignores whether it's a pointer or not, beware!
	PredefinedTranslation TranslationMap
}

TranslatorConfig Stores the configuration of the graphql object builder

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL