tools

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 16 Imported by: 3

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 added in v0.3.0

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

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

func GetPathFieldSubSelections added in v0.3.0

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 added in v0.3.0

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

MakeExecutableSchemaWithContext make a schema and supply a context

func MergeExtensions added in v0.3.0

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) []interface{}

UnaliasedPathArray gets the path array for a resolve function without aliases

Types

type DependencyMap added in v0.3.0

type DependencyMap map[string]map[string]interface{}

type DirectiveMap

type DirectiveMap map[string]*graphql.Directive

DirectiveMap a map of directives

type EnumResolver

type EnumResolver struct {
	Values map[string]interface{}
}

EnumResolver config for enum values

type ExecutableSchema

type ExecutableSchema struct {
	TypeDefs         interface{}               // a string, []string, or func() []string
	Resolvers        map[string]interface{}    // 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 added in v1.0.0

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 added in v0.3.0

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]interface{}

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 added in v0.3.0

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

VisitArgumentDefinitionParams params

type VisitEnumParams added in v0.3.0

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

VisitEnumParams params

type VisitEnumValueParams added in v0.3.0

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

VisitEnumValueParams params

type VisitFieldDefinitionParams added in v0.3.0

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

VisitFieldDefinitionParams params

type VisitInputFieldDefinitionParams added in v0.3.0

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

VisitInputFieldDefinitionParams params

type VisitInputObjectParams added in v0.3.0

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

VisitInputObjectParams params

type VisitInterfaceParams added in v0.3.0

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

VisitInterfaceParams params

type VisitObjectParams added in v0.3.0

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

VisitObjectParams params

type VisitScalarParams added in v0.3.0

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

VisitScalarParams params

type VisitSchemaParams added in v0.3.0

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

VisitSchemaParams params

type VisitUnionParams added in v0.3.0

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

VisitUnionParams params

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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