gql_auto

package module
v0.0.0-...-6472021 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: MIT Imports: 8 Imported by: 1

README

Golang GrahpQL Automatic Schema Generation

This repository contains a library to auto generate a GraphQL schemas based on golang structs.

It based on github.com/lab259/go-graphql-struct and github.com/graphql-go/graphql.

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.

Usage

type Person struct {
	Name    string   `graphql:"!name"`
	Age     int      `graphql:"age"`
	Friends []Person `graphql:"friends"`
}

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
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultEncoder = NewEncoder()

Functions

func AddField

func AddField(root *graphql.Object, field *graphql.Field)

func Args

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

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

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 Encoder

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

func NewEncoder

func NewEncoder() *Encoder

func (*Encoder) Args

func (enc *Encoder) Args(obj interface{}) (graphql.FieldConfigArgument, error)

func (*Encoder) ArgsOf

func (*Encoder) ArrayOf

func (enc *Encoder) ArrayOf(t reflect.Type, options ...Option) (graphql.Type, error)

func (*Encoder) Field

func (enc *Encoder) Field(t interface{}, options ...Option) (graphql.Field, error)

func (*Encoder) FieldOf

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

func (*Encoder) InputObjectFieldMap

func (enc *Encoder) InputObjectFieldMap(t reflect.Type) (graphql.InputObjectConfigFieldMap, error)

func (*Encoder) Struct

func (enc *Encoder) Struct(obj interface{}, options ...Option) (*graphql.Object, error)

func (*Encoder) StructOf

func (enc *Encoder) StructOf(t reflect.Type, options ...Option) (*graphql.Object, error)

StructOf returns a `*graphql.Object` with the description extracted from the obj passed.

This method extracts the information needed from the fields of the obj informed. All fields tagged with "graphql" are added.

The "graphql" tag can be defined as:

```

type T struct {
    field string `graphql:"fieldname"`
}

```

* fieldname: The name of the field.

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

Option 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 WithName

func WithName(name string) Option

WithName creates an `Option` that provides sets the name for fields and objects

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

func WithResolver

func WithResolver(resolver graphql.FieldResolveFn) Option

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

It can be applied to: * Fields;

func WithType

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