gqlstruct

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 6, 2022 License: MIT Imports: 5 Imported by: 0

README

CircleCI codecov Go Report Card Go Doc

go-graphql-struct (gqlstruct)

This library implements generating GraphQL Schema based on tagged structs using the github.com/graphql-go/graphql implementation.

Usually, building the schema is a one time task and it is done statically. So, this library does not degrade the performance, not even a little, but in that one-time initialization.

Supported tags

  • desc This will become the GraphQL "Description" field on structs.
  • dep This will become the GraphQL "DeprecatedReason" field on structs.

Usage

Check the examples in the /examples folder.

Custom Types

The default data types of the GraphQL can be count in one hand, which is not a bad thing. However, that means that you may need to implement some scalar types (or event complexes types) yourself.

In order to provide custom types for the fields the GraphqlTyped interface was defined:

type GraphqlTyped interface {
    GraphqlType() graphql.Type
}

An example:

type TypeA string

func (*TypeA) GraphqlType() graphql.Type {
    return graphql.Int
}

Remember, this library is all about declaring the schema. If you need marshalling/unmarshaling a custom type to another, use the implementation of the github.com/graphql-go/graphql library (check on the graphql.NewScalar and graphql.ScalarConfig).

Resolver

To implement resolvers over a Custom Type, you will implement the interface GraphqlResolver:

type GraphqlResolver interface {
    GraphqlResolve(p graphql.ResolveParams) (interface{}, error)
}

IMPORTANT: Although the method GraphqlResolve is a member of a struct, it is called statically. So, do not make any references of the struct itself, inside of this method.

An example:

type TypeA string

func (*TypeA) GraphqlType() graphql.Type {
    return graphql.Int
}

Limitations

  • This library do not deal with arrays yet.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args added in v1.0.3

func Args(obj interface{}) graphql.FieldConfigArgument

Args Obtain the arguments property of a mutation object

func Field

func Field(t interface{}, options ...Option) graphql.Field

func FieldOf

func FieldOf(t reflect.Type, options ...Option) (graphql.Field, error)

func InputObject added in v1.0.3

func InputObject(name string, obj interface{}) *graphql.InputObject

func NewEncoder

func NewEncoder() *encoder

func NewErrTypeNotRecognized

func NewErrTypeNotRecognized(t reflect.Type) error

func NewErrTypeNotRecognizedWithStruct

func NewErrTypeNotRecognizedWithStruct(reason error, structType reflect.Type, structField reflect.StructField) error

func Struct

func Struct(obj interface{}) *graphql.Object

Types

type GraphqlResolver

type GraphqlResolver interface {
	// GraphqlResolve is the method will be set to the `graphql.Field` as the
	// resolver method.
	GraphqlResolve(p graphql.ResolveParams) (interface{}, error)
}

GraphqlResolver is the interface implemented by types that will provide a a resolver.

type GraphqlTyped

type GraphqlTyped interface {
	// GraphqlType returns the `graphql.Type` that represents the data type that
	// implements this interface.
	GraphqlType() graphql.Type
}

GraphqlTyped is the interface implemented by types that will provide a special `graphql.Type`.

type Option

type Option interface {
	Apply(dst interface{}) error
}

FieldOption describes how an option will behave when applied to a field.

func WithArgs

func WithArgs(args ...interface{}) Option

WithArgs creates an `Option` that sets the arguments for a field.

It can be applied to: * Fields;

func WithDefaultvalue

func WithDefaultvalue(defaultValue interface{}) Option

WithDefaultvalue creates an `Option` that provides sets the description for arguments.

It can be applied to: * Arguments;

func WithDeprecationReason

func WithDeprecationReason(description string) Option

WithDeprecationReason creates an `Option` that sets the deprecation reason for fields.

It can be applied to: * Fields;

func WithDescription

func WithDescription(description string) Option

WithDescription creates an `Option` that provides sets the description for fields, objects and arguments.

It can be applied to: * Field; * Arguments; * Objects;

func WithResolve

func WithResolve(resolver graphql.FieldResolveFn) Option

WithResolver creates an `Option` that sets the resolver for fields.

It can be applied to: * Fields;

func WithType added in v1.0.2

func WithType(t graphql.Type) Option

WithType creates an `Option` that sets the type of fields.

It can be applied to: * Fields;

type TypeNotRecognizedWithStructError

type TypeNotRecognizedWithStructError struct {
	// contains filtered or unexported fields
}

func (*TypeNotRecognizedWithStructError) Error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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