tools

package module
v0.0.0-...-6b7534b Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2023 License: MIT Imports: 15 Imported by: 1

README

graphql-go-tools

Like apollo-tools for graphql-go

Current Tools

MakeExecutableSchema

Currently supports:

  • Merge multiple graphql documents
  • Object type extending
  • Custom Directives
  • Import types and directives

Planned:

  • Schema-stitching

Limitations:

  • Only types and directives defined in the TypeDefs with schema language can be extended and have custom directives applied.

Example

func main() {
  schema, err := tools.MakeExecutableSchema(tools.ExecutableSchema{
    TypeDefs: `
    directive @description(value: String!) on FIELD_DEFINITION

    type Foo {
      id: ID!
      name: String!
      description: String
    }
    
    type Query {
      foo(id: ID!): Foo @description(value: "bazqux")
    }`,
    Resolvers: tools.ResolverMap{
      "Query": &tools.ObjectResolver{
        Fields: tools.FieldResolveMap{
          "foo": &tools.FieldResolver{
            Resolve: func(p graphql.ResolveParams) (interface{}, error) {
              // lookup data
              return foo, nil
            }
          },
        },
      },
    },
    SchemaDirectives: tools.SchemaDirectiveVisitorMap{
      "description": &tools.SchemaDirectiveVisitor{
        VisitFieldDefinition: func(field *graphql.Field, args map[string]interface{}) {
          resolveFunc := field.Resolve
          field.Resolve = func(p graphql.ResolveParams) (interface{}, error) {
            result, err := resolveFunc(p)
            if err != nil {
              return result, err
            }
            data := result.(map[string]interface{})
            data["description"] = args["value"]
            return data, nil
          }
        },
      },
    },
  })

  if err != nil {
    log.Fatalf("Failed to build schema, error: %v", err)
  }

  params := graphql.Params{
    Schema: schema,
    RequestString: `
    query GetFoo {
      foo(id: "5cffbf1ccecefcfff659cea8") {
        description
      }
    }`,
  }

  r := graphql.Do(params)
  if r.HasErrors() {
		log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
	}
	rJSON, _ := json.Marshal(r)
	fmt.Printf("%s \n", rJSON)
}

Handler

Modified graphql-go/handler with updated GraphiQL and Playground

See handler package

Documentation

Index

Constants

View Source
const (
	DefaultRootQueryName        = "Query"
	DefaultRootMutationName     = "Mutation"
	DefaultRootSubscriptionName = "Subscription"
)

default root type names

View Source
const IntrospectionQuery = `` /* 1304-byte string literal not displayed */

Variables

View Source
var HideDirective = graphql.NewDirective(graphql.DirectiveConfig{
	Name:        directiveHide,
	Description: "Hide a field, useful when generating types from the AST where the backend type has more fields than the graphql type",
	Locations:   []string{graphql.DirectiveLocationFieldDefinition},
	Args:        graphql.FieldConfigArgument{},
})

HideDirective hides a define field

Functions

func GetArgumentValues

func GetArgumentValues(argDefs []*graphql.Argument, argASTs []*ast.Argument, variableVariables map[string]any) (map[string]any, error)

Prepares an object map of argument values given a list of argument definitions and list of argument AST nodes.

func GetPathFieldSubSelections

func GetPathFieldSubSelections(info graphql.ResolveInfo, field ...string) (names []string, err error)

GetPathFieldSubSelections gets the subselectiond for a path

func MakeExecutableSchema

func MakeExecutableSchema(config ExecutableSchema) (graphql.Schema, error)

MakeExecutableSchema is shorthand for ExecutableSchema{}.Make(ctx context.Context)

func MakeExecutableSchemaWithContext

func MakeExecutableSchemaWithContext(ctx context.Context, config ExecutableSchema) (graphql.Schema, error)

MakeExecutableSchemaWithContext make a schema and supply a context

func MergeExtensions

func MergeExtensions(obj *ast.ObjectDefinition, extensions ...*ast.ObjectDefinition) *ast.ObjectDefinition

Merges object definitions

func ReadSourceFiles

func ReadSourceFiles(p string, recursive ...bool) (string, error)

ReadSourceFiles reads all source files from a specified path

func UnaliasedPathArray

func UnaliasedPathArray(info graphql.ResolveInfo) []any

UnaliasedPathArray gets the path array for a resolve function without aliases

Types

type DependencyMap

type DependencyMap map[string]map[string]any

type DirectiveMap

type DirectiveMap map[string]*graphql.Directive

DirectiveMap a map of directives

type EnumResolver

type EnumResolver struct {
	Values map[string]any
}

EnumResolver config for enum values

type ExecutableSchema

type ExecutableSchema struct {
	TypeDefs         any                       // a string, []string, or func() []string
	Resolvers        map[string]any            // a map of Resolver, Directive, Scalar, Enum, Object, InputObject, Union, or Interface
	SchemaDirectives SchemaDirectiveVisitorMap // Map of SchemaDirectiveVisitor
	Extensions       []graphql.Extension       // GraphQL extensions
	Debug            bool                      // Prints debug messages during compile
	// contains filtered or unexported fields
}

ExecutableSchema configuration for making an executable schema this attempts to provide similar functionality to Apollo graphql-tools https://www.apollographql.com/docs/graphql-tools/generate-schema

func (*ExecutableSchema) ConcatenateTypeDefs

func (c *ExecutableSchema) ConcatenateTypeDefs() (*ast.Document, error)

ConcatenateTypeDefs combines one ore more typeDefs into an ast Document

func (*ExecutableSchema) Document

func (c *ExecutableSchema) Document() *ast.Document

Document returns the document

func (*ExecutableSchema) Make

Make creates a graphql schema config, this struct maintains intact the types and does not require the use of a non empty Query

type FieldResolve

type FieldResolve struct {
	Resolve   graphql.FieldResolveFn
	Subscribe graphql.FieldResolveFn
}

FieldResolve field resolver

type FieldResolveMap

type FieldResolveMap map[string]*FieldResolve

FieldResolveMap map of field resolve functions

type InterfaceResolver

type InterfaceResolver struct {
	ResolveType graphql.ResolveTypeFn
	Fields      FieldResolveMap
}

InterfaceResolver config for interface resolve

type ObjectResolver

type ObjectResolver struct {
	IsTypeOf graphql.IsTypeOfFn
	Fields   FieldResolveMap
}

ObjectResolver config for object resolver map

type Resolver

type Resolver interface {
	// contains filtered or unexported methods
}

Resolver interface to a resolver configuration

type ResolverMap

type ResolverMap map[string]any

ResolverMap a map of resolver configurations. Accept generic interfaces and identify types at build

type ScalarResolver

type ScalarResolver struct {
	Serialize    graphql.SerializeFn
	ParseValue   graphql.ParseValueFn
	ParseLiteral graphql.ParseLiteralFn
}

ScalarResolver config for a scalar resolve map

type SchemaDirectiveVisitor

type SchemaDirectiveVisitor struct {
	VisitSchema               func(p VisitSchemaParams) error
	VisitScalar               func(p VisitScalarParams) error
	VisitObject               func(p VisitObjectParams) error
	VisitFieldDefinition      func(p VisitFieldDefinitionParams) error
	VisitArgumentDefinition   func(p VisitArgumentDefinitionParams) error
	VisitInterface            func(p VisitInterfaceParams) error
	VisitUnion                func(p VisitUnionParams) error
	VisitEnum                 func(p VisitEnumParams) error
	VisitEnumValue            func(p VisitEnumValueParams) error
	VisitInputObject          func(p VisitInputObjectParams) error
	VisitInputFieldDefinition func(p VisitInputFieldDefinitionParams) error
}

SchemaDirectiveVisitor defines a schema visitor. This attempts to provide similar functionality to Apollo graphql-tools https://www.apollographql.com/docs/graphql-tools/schema-directives/

type SchemaDirectiveVisitorMap

type SchemaDirectiveVisitorMap map[string]*SchemaDirectiveVisitor

SchemaDirectiveVisitorMap a map of schema directive visitors

type UnionResolver

type UnionResolver struct {
	ResolveType graphql.ResolveTypeFn
}

UnionResolver config for interface resolve

type VisitArgumentDefinitionParams

type VisitArgumentDefinitionParams struct {
	Context context.Context
	Config  *graphql.ArgumentConfig
	Node    *ast.InputValueDefinition
	Args    map[string]any
}

VisitArgumentDefinitionParams params

type VisitEnumParams

type VisitEnumParams struct {
	Context context.Context
	Config  *graphql.EnumConfig
	Node    *ast.EnumDefinition
	Args    map[string]any
}

VisitEnumParams params

type VisitEnumValueParams

type VisitEnumValueParams struct {
	Context context.Context
	Config  *graphql.EnumValueConfig
	Node    *ast.EnumValueDefinition
	Args    map[string]any
}

VisitEnumValueParams params

type VisitFieldDefinitionParams

type VisitFieldDefinitionParams struct {
	Context    context.Context
	Config     *graphql.Field
	Node       *ast.FieldDefinition
	Args       map[string]any
	ParentName string
	ParentKind string
}

VisitFieldDefinitionParams params

type VisitInputFieldDefinitionParams

type VisitInputFieldDefinitionParams struct {
	Context context.Context
	Config  *graphql.InputObjectFieldConfig
	Node    *ast.InputValueDefinition
	Args    map[string]any
}

VisitInputFieldDefinitionParams params

type VisitInputObjectParams

type VisitInputObjectParams struct {
	Context context.Context
	Config  *graphql.InputObjectConfig
	Node    *ast.InputObjectDefinition
	Args    map[string]any
}

VisitInputObjectParams params

type VisitInterfaceParams

type VisitInterfaceParams struct {
	Context context.Context
	Config  *graphql.InterfaceConfig
	Node    *ast.InterfaceDefinition
	Args    map[string]any
}

VisitInterfaceParams params

type VisitObjectParams

type VisitObjectParams struct {
	Context    context.Context
	Config     *graphql.ObjectConfig
	Node       *ast.ObjectDefinition
	Extensions []*ast.ObjectDefinition
	Args       map[string]any
}

VisitObjectParams params

type VisitScalarParams

type VisitScalarParams struct {
	Context context.Context
	Config  *graphql.ScalarConfig
	Node    *ast.ScalarDefinition
	Args    map[string]any
}

VisitScalarParams params

type VisitSchemaParams

type VisitSchemaParams struct {
	Context context.Context
	Config  *graphql.SchemaConfig
	Node    *ast.SchemaDefinition
	Args    map[string]any
}

VisitSchemaParams params

type VisitUnionParams

type VisitUnionParams struct {
	Context context.Context
	Config  *graphql.UnionConfig
	Node    *ast.UnionDefinition
	Args    map[string]any
}

VisitUnionParams params

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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