go_gen_graphql

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2023 License: MIT Imports: 5 Imported by: 1

README

go-gen-graphql

Actions Status Coverage Status PkgGoDev go-report

Generate a graphql query/mutation body from a struct

package main

import (
	"fmt"
	gengraphql "github.com/Eun/go-gen-graphql"
)

func main() {
	type Data struct {
		ID             string
		Name           string `json:"name"`
		CreationTime   string `graphql:"createdOn"`
		ActiveProjects struct {
			ID string `json:"id"`
		} `json:"projects" graphql:"projects(filter: %q)"`
	}

	s, err := gengraphql.Generatef(Data{}, nil, "active")
	if err != nil {
		panic(err)
	}
	fmt.Println(s)
	// Output:
	// ID
	// name
	// createdOn
	// projects(filter: "active"){
	//   id
	// }
}

Build History

Build history

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Generate

func Generate(in any, options *Options) (string, error)

Generate generates a graphql body from a struct. Returns the graphql body and error.

Example
type Data struct {
	ID             string
	Name           string `json:"name"`
	CreationTime   string `graphql:"createdOn"`
	ActiveProjects struct {
		ID string `json:"id"`
	} `json:"projects" graphql:"projects(filter: \"active\")"`
}

s, err := Generate(Data{}, nil)
if err != nil {
	panic(err)
}
fmt.Println(s)
Output:

ID
name
createdOn
projects(filter: "active"){
  id
}

func GenerateFromReflectValue

func GenerateFromReflectValue(v reflect.Type, options *Options) (string, error)

GenerateFromReflectValue generates a graphql body from reflect Type. Returns the graphql body and error.

func Generatef

func Generatef(in any, options *Options, a ...any) (string, error)

Generatef generates a graphql body from a struct. Returns the graphql body and error.

Example
type Data struct {
	ID             string
	Name           string `json:"name"`
	CreationTime   string `graphql:"createdOn"`
	ActiveProjects struct {
		ID string `json:"id"`
	} `json:"projects" graphql:"projects(filter: %q)"`
}

s, err := Generatef(Data{}, nil, "active")
if err != nil {
	panic(err)
}
fmt.Println(s)
Output:

ID
name
createdOn
projects(filter: "active"){
  id
}

Types

type InvalidTypeError

type InvalidTypeError struct{}

InvalidTypeError indicates that the type is invalid or not supported.

func (InvalidTypeError) Error

func (InvalidTypeError) Error() string

type Options

type Options struct {
	GraphQLTag string
	JSONTag    string
	Indent     string
}

Options can be used to modify the behavior of Generate, Generatef and GenerateFromReflectValue.

Jump to

Keyboard shortcuts

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