Categorygithub.com/bduncanj/go-graphql-struct
modulepackage
1.0.2
Repository: https://github.com/bduncanj/go-graphql-struct.git
Documentation: pkg.go.dev

# 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.

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

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
WithArgs creates an `Option` that sets the arguments for a field.
WithDefaultvalue creates an `Option` that provides sets the description for arguments.
WithDeprecationReason creates an `Option` that sets the deprecation reason for fields.
WithDescription creates an `Option` that provides sets the description for fields, objects and arguments.
WithResolver creates an `Option` that sets the resolver for fields.
WithType creates an `Option` that sets the type of fields.

# Structs

No description provided by the author

# Interfaces

GraphqlResolver is the interface implemented by types that will provide a a resolver.
GraphqlTyped is the interface implemented by types that will provide a special `graphql.Type`.
FieldOption describes how an option will behave when applied to a field.