gqlstruct

package
v0.0.0-...-7854308 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StructDirectiveTypeDef = `` /* 181-byte string literal not displayed */

)

Struct directive

Variables

View Source
var RegistryKey registryKey = "directive_struct_registry"

Actual registry key

View Source
var StructDirective = graphql.NewDirective(graphql.DirectiveConfig{
	Name:        structDirectiveName,
	Description: "A directive to define struct types",
	Locations: []string{
		graphql.DirectiveLocationFieldDefinition,
		graphql.DirectiveLocationObject,
	},
	Args: graphql.FieldConfigArgument{
		"is_root": &graphql.ArgumentConfig{
			Type:         graphql.Boolean,
			DefaultValue: false,
		},
		"private": &graphql.ArgumentConfig{
			Type:         graphql.Boolean,
			DefaultValue: false,
		},
		"omit": &graphql.ArgumentConfig{
			Type:         graphql.Boolean,
			DefaultValue: false,
		},
		"name": &graphql.ArgumentConfig{
			Type: graphql.String,
		},
		"service": &graphql.ArgumentConfig{
			Type: graphql.String,
		},
		"type": &graphql.ArgumentConfig{
			Type: graphql.String,
		},
		"prototype": &graphql.ArgumentConfig{
			Type: graphql.String,
		},
		"tags": &graphql.ArgumentConfig{
			Type: graphql.NewList(
				graphql.NewNonNull(
					graphql.String,
				),
			),
		},
	},
})

Struct direcrtive

View Source
var StructDirectiveVisitor = &tools.SchemaDirectiveVisitor{
	VisitObject: func(p tools.VisitObjectParams) {
		def := tools.MergeExtensions(p.Node, p.Extensions...)

		reg := p.Context.Value(RegistryKey)
		registry := reg.(*Registry)

		args := &Args{}
		if err := mapProto(p.Args, args); err != nil {
			fmt.Printf("Failed to map arguments: %v\n", err)
			return
		}

		name := p.Config.Name
		obj, found := registry.Structs[name]
		if !found {
			obj = &Def{
				Name:         name,
				Fields:       map[string]*FieldDef{},
				Private:      args.Private,
				ExplicitName: args.Name,
			}
			registry.Structs[name] = obj
		}

		for _, field := range def.Fields {
			args, err := getDirectiveConfig(field.Directives)
			if err != nil {
				fmt.Printf("Failed to get field directive args: %v\n", err)
				return
			}

			if args.Service != "" {
				svc, found := registry.Services[args.Service]
				if !found {
					svc = &Service{
						Name:    args.Service,
						Methods: map[string]*ServiceMethod{},
					}
					registry.Services[args.Service] = svc
				}

				methodName := strcase.ToCamel(field.Name.Value)
				method, found := svc.Methods[methodName]
				if !found {
					method = &ServiceMethod{
						Name:     methodName,
						Response: field.Type.(*ast.Named).Name.Value,
					}
				}
				svc.Methods[methodName] = method
			}

			if args.Omit {
				continue
			}

			fieldName := field.Name.Value
			ft := getFieldType(registry, field.Type, nil)
			def := &FieldDef{
				Ref:          fieldName,
				Name:         fieldName,
				GqlType:      ft.gqltype,
				GoType:       ft.String(),
				Prototype:    ft.prototype,
				Private:      args.Private,
				ExplicitName: args.Name,
				Tags:         args.Tags,
			}

			if args.Type != "" {
				def.GoType = args.Type
			}

			if args.Prototype != "" {
				def.Prototype = args.Prototype
			}

			obj.Fields[fieldName] = def
		}
	},
}

Functions

func Make

func Make(config tools.ExecutableSchema) (context.Context, error)

Make makes the types

Types

type Args

type Args struct {
	Private   bool     `json:"private"`
	Omit      bool     `json:"omit"`
	Name      string   `json:"name"`
	Service   string   `json:"service"`
	Type      string   `json:"type"`
	Prototype string   `json:"prototype"`
	Tags      []string `json:"tags"`
}

Args arguments

type Def

type Def struct {
	Name         string               `json:"name"`
	Fields       map[string]*FieldDef `json:"fields"`
	Private      bool                 `json:"private"`
	ExplicitName string               `json:"explicit_name"`
	IsRoot       bool                 `json:"is_root"`
}

type FieldDef

type FieldDef struct {
	Ref          string   `json:"ref"`
	Name         string   `json:"name"`
	GoType       string   `json:"gotype"`
	GqlType      string   `json:"gqltype"`
	Prototype    string   `json:"prototype"`
	ProtoIndex   int      `json:"proto_index"`
	Tags         []string `json:"tags"`
	Private      bool     `json:"private"`
	ExplicitName string   `json:"explicit_name"`
}

type FieldDefs

type FieldDefs []*FieldDef

FieldDefs list of fields

func (FieldDefs) Len

func (f FieldDefs) Len() int

func (FieldDefs) Less

func (f FieldDefs) Less(i, j int) bool

func (FieldDefs) Swap

func (f FieldDefs) Swap(i, j int)

type FieldType

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

func (*FieldType) String

func (c *FieldType) String() string

type Registry

type Registry struct {
	Services         map[string]*Service `json:"services"`
	Structs          map[string]*Def     `json:"structs"`
	RootQuery        string              `json:"root_query"`
	RootMutation     string              `json:"root_mutation"`
	RootSubscription string              `json:"root_subscription"`
}

type Service

type Service struct {
	Name    string `json:"name"`
	Methods map[string]*ServiceMethod
}

type ServiceMethod

type ServiceMethod struct {
	Name     string `json:"name"`
	Request  string `json:"request"`
	Response string `json:"response"`
}

type ServiceMethods

type ServiceMethods []*ServiceMethod

ServiceMethods list of methods

func (ServiceMethods) Len

func (f ServiceMethods) Len() int

func (ServiceMethods) Less

func (f ServiceMethods) Less(i, j int) bool

func (ServiceMethods) Swap

func (f ServiceMethods) Swap(i, j int)

Jump to

Keyboard shortcuts

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