ideal

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 8 Imported by: 0

README

Ideal

Ideal is a graphql code first builder for golang. Ideal inspects your provided types with the reflect package and produces graphql types for you under the hood.

This package is greatly inspired by type-graphql over in the node.js world.

Documentation

Similar to the internal json package you can tag your struct fields to produce a different name. Example

package example

import "github.com/eskpil/ideal"

type User struct {
    Name  string `gql:"name"`
    Email string `gql:"email"`
}

You define your queries and mutations like this

package example

import (
   "github.com/eskpil/ideal"
   "reflect" 
)

var Hello = ideal.Query{
    Name: "hello",
    Type: reflect.TypeOf(User{})

    Resolve: func(c *ideal.Context) (interface{}, error) {
        return User{Name: "john", Email: "john@doe.com"}
    }

    Description: "hello"
}

To produce our builder we need a builder

package example

import "github.com/eskpil/ideal"

func main() {
	resolver := ideal.Resolver{}
	resolver.Query(hello)

	builder := ideal.NewBuilder(resolver)
	
    runtime := ideal.NewRuntime()
	schema, err := builder.Build(runtime)
}

From this point you have a graphql schema like the one defined in graphql-go on which you can execute graphql requests.

package example

import "github.com/eskpil/ideal"

func main() {
    params := graphql.Params{Schema: schema, RequestString: `{ hello {
    name, email } }`} 

    result := graphql.Do(params)
}

Ideal also supports adding middleware to your resolvers.

For a look at the complete examples, see

Documentation

Index

Constants

View Source
const RuntimeParametersKey key = "ideal.runtime_parameters"

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	Resolvers []Resolver
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder(resolvers ...Resolver) *Builder

func (*Builder) AddResolver added in v0.0.3

func (b *Builder) AddResolver(resolver Resolver)

func (*Builder) Build

func (b *Builder) Build(runtime *Runtime) (graphql.Schema, error)

type Context added in v0.0.6

type Context struct {
	Arguments map[string]interface{}

	Headers http.Header
	Cookies []*http.Cookie
	// contains filtered or unexported fields
}

func (*Context) Cookie added in v0.0.6

func (c *Context) Cookie(name string) (*http.Cookie, error)

func (*Context) Deadline added in v0.0.6

func (c *Context) Deadline() (deadline time.Time, ok bool)

func (*Context) Done added in v0.0.6

func (c *Context) Done() <-chan struct{}

func (*Context) Err added in v0.0.6

func (c *Context) Err() error

func (*Context) Header added in v0.0.6

func (c *Context) Header(key string) string

func (*Context) Set added in v0.0.6

func (c *Context) Set(key any, value any)

func (*Context) Source added in v0.0.6

func (c *Context) Source() any

func (*Context) Value added in v0.0.6

func (c *Context) Value(key any) any

type Field added in v0.0.6

type Field struct {
	Name string

	Middleware []MiddlewareFunc

	Arguments reflect.Type
	Type      reflect.Type
	Resolve   ResolveFunc

	Description       string
	DeprecationReason string
}

type MiddlewareFunc added in v0.0.6

type MiddlewareFunc func(ctx *Context, next NextFunc) (interface{}, error)

type Mutation

type Mutation struct {
	Name string

	Middleware []MiddlewareFunc

	Arguments reflect.Type
	Type      reflect.Type
	Resolve   ResolveFunc

	Description       string
	DeprecationReason string
}

type NextFunc added in v0.0.6

type NextFunc func(ctx *Context) (interface{}, error)

type Query

type Query struct {
	Name string

	Middleware []MiddlewareFunc

	Arguments reflect.Type
	Type      reflect.Type
	Resolve   ResolveFunc

	Description       string
	DeprecationReason string
}

type ResolveFunc added in v0.0.6

type ResolveFunc func(ctx *Context) (interface{}, error)

type Resolver

type Resolver struct {
	Type reflect.Type

	Queries   []Query
	Mutations []Mutation
	Fields    []Field
}

func (*Resolver) Query

func (r *Resolver) Query(query Query)

type Runtime added in v0.0.6

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

func NewRuntime added in v0.0.6

func NewRuntime() *Runtime

func (*Runtime) AddHandler added in v0.0.6

func (r *Runtime) AddHandler(fieldName string, resolve ResolveFunc, middleware ...MiddlewareFunc)

func (*Runtime) Resolve added in v0.0.6

func (r *Runtime) Resolve(p graphql.ResolveParams) (interface{}, error)

type RuntimeParameters added in v0.0.6

type RuntimeParameters struct {
	Headers http.Header
	Cookies []*http.Cookie
}

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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