schema

package
v0.0.0-...-5d34579 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BooleanType = &ScalarType{
	Name: "Boolean",
	LiteralCoercion: func(v ast.Value) interface{} {
		switch v := v.(type) {
		case *ast.BooleanValue:
			return v.Value
		}
		return nil
	},
	VariableValueCoercion: coerceBoolean,
	ResultCoercion:        coerceBoolean,
}

BooleanType implements the Boolean type as defined by the GraphQL spec.

View Source
var BuiltInTypes = map[string]*ScalarType{
	"Int":     IntType,
	"Float":   FloatType,
	"String":  StringType,
	"Boolean": BooleanType,
	"ID":      IDType,
}

BuiltInTypes maps all of the built-in types to their names.

View Source
var FloatType = &ScalarType{
	Name: "Float",
	LiteralCoercion: func(v ast.Value) interface{} {
		switch v := v.(type) {
		case *ast.IntValue:
			if n, err := strconv.ParseFloat(v.Value, 64); err == nil {
				return n
			}
		case *ast.FloatValue:
			if n, err := strconv.ParseFloat(v.Value, 64); err == nil {
				return n
			}
		}
		return nil
	},
	VariableValueCoercion: coerceFloat,
	ResultCoercion:        coerceFloat,
}

FloatType implements the Float type as defined by the GraphQL spec.

View Source
var IDType = &ScalarType{
	Name: "ID",
	LiteralCoercion: func(v ast.Value) interface{} {
		switch v := v.(type) {
		case *ast.IntValue:
			if n, err := strconv.ParseInt(v.Value, 10, 0); err == nil {
				return int(n)
			}
		case *ast.StringValue:
			return v.Value
		}
		return nil
	},
	VariableValueCoercion: func(v interface{}) interface{} {
		switch v := v.(type) {
		case int:
			return v
		case float64:
			if n := int(math.Trunc(v)); float64(n) == v {
				return n
			}
		case string:
			return v
		}
		return nil
	},
	ResultCoercion: func(v interface{}) interface{} {
		switch v := v.(type) {
		case int8:
			return strconv.FormatInt(int64(v), 10)
		case uint8:
			return strconv.FormatInt(int64(v), 10)
		case int16:
			return strconv.FormatInt(int64(v), 10)
		case uint16:
			return strconv.FormatInt(int64(v), 10)
		case int32:
			return strconv.FormatInt(int64(v), 10)
		case uint32:
			return strconv.FormatInt(int64(v), 10)
		case int64:
			return strconv.FormatInt(v, 10)
		case uint64:
			if v <= math.MaxInt64 {
				return strconv.FormatInt(int64(v), 10)
			}
		case int:
			return strconv.FormatInt(int64(v), 10)
		case uint:
			if v <= math.MaxInt64 {
				return strconv.FormatInt(int64(v), 10)
			}
		case string:
			return v
		}
		return nil
	},
}

IDType implements the ID type as defined by the GraphQL spec. It can be deserialized from a string or an integer type, but always serializes to a string.

View Source
var IncludeDirective = &DirectiveDefinition{
	Description: "The @include directive may be provided for fields, fragment spreads, and inline fragments, and allows for conditional inclusion during execution as described by the if argument.",
	Arguments: map[string]*InputValueDefinition{
		"if": {
			Type: NewNonNullType(BooleanType),
		},
	},
	Locations: []DirectiveLocation{DirectiveLocationField, DirectiveLocationFragmentSpread, DirectiveLocationInlineFragment},
	FieldCollectionFilter: func(arguments map[string]interface{}) bool {
		return arguments["if"].(bool)
	},
}

IncludeDirective implements the @include directive as defined by the GraphQL spec.

View Source
var IntType = &ScalarType{
	Name: "Int",
	LiteralCoercion: func(v ast.Value) interface{} {
		switch v := v.(type) {
		case *ast.IntValue:
			if n, err := strconv.ParseInt(v.Value, 10, 32); err == nil {
				return int(n)
			}
		}
		return nil
	},
	VariableValueCoercion: coerceInt,
	ResultCoercion:        coerceInt,
}

IntType implements the Int type as defined by the GraphQL spec.

View Source
var Null = (*explicitNull)(nil)

Null is to specify an explicit "null" default for input values.

View Source
var SkipDirective = &DirectiveDefinition{
	Description: "The @skip directive may be provided for fields, fragment spreads, and inline fragments, and allows for conditional exclusion during execution as described by the if argument.",
	Arguments: map[string]*InputValueDefinition{
		"if": {
			Type: NewNonNullType(BooleanType),
		},
	},
	Locations: []DirectiveLocation{DirectiveLocationField, DirectiveLocationFragmentSpread, DirectiveLocationInlineFragment},
	FieldCollectionFilter: func(arguments map[string]interface{}) bool {
		return !arguments["if"].(bool)
	},
}

SkipDirective implements the @skip directive as defined by the GraphQL spec.

View Source
var StringType = &ScalarType{
	Name: "String",
	LiteralCoercion: func(v ast.Value) interface{} {
		switch v := v.(type) {
		case *ast.StringValue:
			return v.Value
		}
		return nil
	},
	VariableValueCoercion: coerceString,
	ResultCoercion:        coerceString,
}

StringType implements the String type as defined by the GraphQL spec.

Functions

func CoerceLiteral

func CoerceLiteral(from ast.Value, to Type, variableValues map[string]interface{}) (interface{}, error)

func CoerceVariableValue

func CoerceVariableValue(value interface{}, t Type) (interface{}, error)

func FieldResolverCost

func FieldResolverCost(n int) func(FieldCostContext) FieldCost

Returns a cost function which returns a constant resolver cost with no multiplier.

func Inspect

func Inspect(node interface{}, f func(interface{}) bool)

Inspect traverses the types referenced by the schema, invoking f for each one. If f returns true, Inspect will recursively inspect the types referenced by the given node. For many schemas, this means f must be able to break cycles to prevent Inspect from running infinitely.

func IsEnumType

func IsEnumType(t Type) bool

func IsListType

func IsListType(t Type) bool

func IsNonNullType

func IsNonNullType(t Type) bool

func IsObjectType

func IsObjectType(t Type) bool

func IsScalarType

func IsScalarType(t Type) bool

Types

type Argument

type Argument struct {
	Name  string
	Value interface{}
}

type Directive

type Directive struct {
	Definition *DirectiveDefinition
	Arguments  []*Argument
}

type DirectiveDefinition

type DirectiveDefinition struct {
	Description string
	Arguments   map[string]*InputValueDefinition
	Locations   []DirectiveLocation

	// If non-nil, this function will be invoked during field collection for each selection with
	// this directive present. If the function returns false, the selection will be skipped.
	FieldCollectionFilter func(arguments map[string]interface{}) bool
}

type DirectiveLocation

type DirectiveLocation string
const (
	DirectiveLocationQuery              DirectiveLocation = "QUERY"
	DirectiveLocationMutation           DirectiveLocation = "MUTATION"
	DirectiveLocationSubscription       DirectiveLocation = "SUBSCRIPTION"
	DirectiveLocationField              DirectiveLocation = "FIELD"
	DirectiveLocationFragmentDefinition DirectiveLocation = "FRAGMENT_DEFINITION"
	DirectiveLocationFragmentSpread     DirectiveLocation = "FRAGMENT_SPREAD"
	DirectiveLocationInlineFragment     DirectiveLocation = "INLINE_FRAGMENT"

	DirectiveLocationSchema               DirectiveLocation = "SCHEMA"
	DirectiveLocationScalar               DirectiveLocation = "SCALAR"
	DirectiveLocationObject               DirectiveLocation = "OBJECT"
	DirectiveLocationFieldDefinition      DirectiveLocation = "FIELD_DEFINITION"
	DirectiveLocationArgumentDefinition   DirectiveLocation = "ARGUMENT_DEFINITION"
	DirectiveLocationInterface            DirectiveLocation = "INTERFACE"
	DirectiveLocationUnion                DirectiveLocation = "UNION"
	DirectiveLocationEnum                 DirectiveLocation = "ENUM"
	DirectiveLocationEnumValue            DirectiveLocation = "ENUM_VALUE"
	DirectiveLocationInputObject          DirectiveLocation = "INPUT_OBJECT"
	DirectiveLocationInputFieldDefinition DirectiveLocation = "INPUT_FIELD_DEFINITION"
)

type EnumType

type EnumType struct {
	Name        string
	Description string
	Directives  []*Directive
	Values      map[string]*EnumValueDefinition

	// This type is only available for introspection and use when the given features are enabled.
	RequiredFeatures FeatureSet
}

func (*EnumType) CoerceLiteral

func (t *EnumType) CoerceLiteral(from ast.Value) (interface{}, error)

func (*EnumType) CoerceResult

func (t *EnumType) CoerceResult(result interface{}) (string, error)

func (*EnumType) CoerceVariableValue

func (t *EnumType) CoerceVariableValue(v interface{}) (interface{}, error)

func (*EnumType) IsInputType

func (t *EnumType) IsInputType() bool

func (*EnumType) IsOutputType

func (t *EnumType) IsOutputType() bool

func (*EnumType) IsSameType

func (t *EnumType) IsSameType(other Type) bool

func (*EnumType) IsSubTypeOf

func (t *EnumType) IsSubTypeOf(other Type) bool

func (*EnumType) String

func (t *EnumType) String() string

func (*EnumType) TypeName

func (t *EnumType) TypeName() string

func (*EnumType) TypeRequiredFeatures

func (t *EnumType) TypeRequiredFeatures() FeatureSet

type EnumValueDefinition

type EnumValueDefinition struct {
	Description       string
	Directives        []*Directive
	Value             interface{}
	DeprecationReason string
}

type FeatureSet

type FeatureSet map[string]struct{}

func NewFeatureSet

func NewFeatureSet(features ...string) FeatureSet

func (FeatureSet) Has

func (s FeatureSet) Has(feature string) bool

func (FeatureSet) IsSubsetOf

func (s FeatureSet) IsSubsetOf(other FeatureSet) bool

func (FeatureSet) Union

func (s FeatureSet) Union(other FeatureSet) FeatureSet

type FieldContext

type FieldContext struct {
	Context   context.Context
	Schema    *Schema
	Object    interface{}
	Features  FeatureSet
	Arguments map[string]interface{}

	// IsSubscribe is true if this is a subscription field being invoked for a subscribe operation.
	// Subselections of this field will not be executed, and the return value will be returned
	// immediately to the caller of Subscribe.
	IsSubscribe bool
}

FieldContext contains important context passed to resolver implementations.

type FieldCost

type FieldCost struct {
	// If non-nil, this context will be passed on to sub-selections of the current field.
	Context context.Context

	// This is the cost of executing the resolver. Typically it will be 1, but if a resolver is
	// particularly expensive, it may be greater.
	Resolver int

	// This is a multiplier applied to all sub-selections of the current field. For fields that
	// return arrays, this is typically the number of expected results (e.g. the "first" or "last"
	// argument to a connection field). Defaults to 1 if not set.
	Multiplier int
}

FieldCost describes the cost of resolving a field, enabling rate limiting and metering.

type FieldCostContext

type FieldCostContext struct {
	Context context.Context

	// The arguments that were provided.
	Arguments map[string]interface{}
}

FieldCostContext contains important context passed to field cost functions.

type FieldDefinition

type FieldDefinition struct {
	Description       string
	Arguments         map[string]*InputValueDefinition
	Type              Type
	Directives        []*Directive
	DeprecationReason string

	// This field is only available for introspection and use when the given features are enabled.
	RequiredFeatures FeatureSet

	// This function can be used to define the cost of resolving the field. The total cost of an
	// operation can be calculated before the operation is executed, enabling rate limiting and
	// metering.
	Cost func(FieldCostContext) FieldCost

	Resolve func(FieldContext) (interface{}, error)
}

FieldDefinition defines an object's field.

type InputObjectType

type InputObjectType struct {
	Name        string
	Description string
	Directives  []*Directive
	Fields      map[string]*InputValueDefinition

	// This type is only available for introspection and use when the given features are enabled.
	RequiredFeatures FeatureSet

	// If given, input objects can validated and converted to other types via this function.
	// Otherwise the objects will remain as maps. This function is called after all fields are fully
	// coerced.
	InputCoercion func(map[string]interface{}) (interface{}, error)

	// Normally input objects only need to be coerced from inputs. However, if an argument of this
	// type is given a default value, we need to be able to do the reverse in order to serialize it
	// for introspection queries.
	//
	// For most use-cases, this function is optional. If it is required, but nil, you will get an
	// error when you attempt to create the schema.
	ResultCoercion func(interface{}) (map[string]interface{}, error)
}

func (*InputObjectType) CoerceLiteral

func (t *InputObjectType) CoerceLiteral(node *ast.ObjectValue, variableValues map[string]interface{}) (interface{}, error)

func (*InputObjectType) CoerceVariableValue

func (t *InputObjectType) CoerceVariableValue(v interface{}) (interface{}, error)

func (*InputObjectType) IsInputType

func (t *InputObjectType) IsInputType() bool

func (*InputObjectType) IsOutputType

func (t *InputObjectType) IsOutputType() bool

func (*InputObjectType) IsSameType

func (t *InputObjectType) IsSameType(other Type) bool

func (*InputObjectType) IsSubTypeOf

func (t *InputObjectType) IsSubTypeOf(other Type) bool

func (*InputObjectType) String

func (t *InputObjectType) String() string

func (*InputObjectType) TypeName

func (t *InputObjectType) TypeName() string

func (*InputObjectType) TypeRequiredFeatures

func (t *InputObjectType) TypeRequiredFeatures() FeatureSet

type InputValueDefinition

type InputValueDefinition struct {
	Description string
	Type        Type

	// For null, set this to Null.
	DefaultValue interface{}

	Directives []*Directive
}

InputValueDefinition defines an input value such as an argument.

type InterfaceType

type InterfaceType struct {
	Name        string
	Description string
	Directives  []*Directive
	Fields      map[string]*FieldDefinition

	// This type is only available for introspection and use when the given features are enabled.
	RequiredFeatures FeatureSet
}

func (*InterfaceType) GetField

func (t *InterfaceType) GetField(name string, features FeatureSet) *FieldDefinition

func (*InterfaceType) IsInputType

func (t *InterfaceType) IsInputType() bool

func (*InterfaceType) IsOutputType

func (t *InterfaceType) IsOutputType() bool

func (*InterfaceType) IsSameType

func (t *InterfaceType) IsSameType(other Type) bool

func (*InterfaceType) IsSubTypeOf

func (t *InterfaceType) IsSubTypeOf(other Type) bool

func (*InterfaceType) String

func (t *InterfaceType) String() string

func (*InterfaceType) TypeName

func (t *InterfaceType) TypeName() string

func (*InterfaceType) TypeRequiredFeatures

func (t *InterfaceType) TypeRequiredFeatures() FeatureSet

type ListType

type ListType struct {
	Type Type
}

func NewListType

func NewListType(t Type) *ListType

func (*ListType) CoerceLiteral

func (t *ListType) CoerceLiteral(node ast.Value, variableValues map[string]interface{}) ([]interface{}, error)

func (*ListType) CoerceVariableValue

func (t *ListType) CoerceVariableValue(v interface{}) (interface{}, error)

func (*ListType) IsInputType

func (t *ListType) IsInputType() bool

func (*ListType) IsOutputType

func (t *ListType) IsOutputType() bool

func (*ListType) IsSameType

func (t *ListType) IsSameType(other Type) bool

func (*ListType) IsSubTypeOf

func (t *ListType) IsSubTypeOf(other Type) bool

func (*ListType) String

func (t *ListType) String() string

func (*ListType) TypeRequiredFeatures

func (t *ListType) TypeRequiredFeatures() FeatureSet

func (*ListType) Unwrap

func (t *ListType) Unwrap() Type

type NamedType

type NamedType interface {
	Type
	TypeName() string
}

func UnwrappedType

func UnwrappedType(t Type) NamedType

type NonNullType

type NonNullType struct {
	Type Type
}

func NewNonNullType

func NewNonNullType(t Type) *NonNullType

func (*NonNullType) IsInputType

func (t *NonNullType) IsInputType() bool

func (*NonNullType) IsOutputType

func (t *NonNullType) IsOutputType() bool

func (*NonNullType) IsSameType

func (t *NonNullType) IsSameType(other Type) bool

func (*NonNullType) IsSubTypeOf

func (t *NonNullType) IsSubTypeOf(other Type) bool

func (*NonNullType) String

func (t *NonNullType) String() string

func (*NonNullType) TypeRequiredFeatures

func (t *NonNullType) TypeRequiredFeatures() FeatureSet

func (*NonNullType) Unwrap

func (t *NonNullType) Unwrap() Type

type ObjectType

type ObjectType struct {
	Name        string
	Description string
	Directives  []*Directive
	Fields      map[string]*FieldDefinition

	// This type is only available for introspection and use when the given features are enabled.
	RequiredFeatures FeatureSet

	ImplementedInterfaces []*InterfaceType

	// Objects that implement one or more interfaces must define this. The function should return
	// true if obj is an object of this type.
	IsTypeOf func(obj interface{}) bool
}

func (*ObjectType) GetField

func (t *ObjectType) GetField(name string, features FeatureSet) *FieldDefinition

func (*ObjectType) IsInputType

func (t *ObjectType) IsInputType() bool

func (*ObjectType) IsOutputType

func (t *ObjectType) IsOutputType() bool

func (*ObjectType) IsSameType

func (t *ObjectType) IsSameType(other Type) bool

func (*ObjectType) IsSubTypeOf

func (t *ObjectType) IsSubTypeOf(other Type) bool

func (*ObjectType) String

func (t *ObjectType) String() string

func (*ObjectType) TypeName

func (t *ObjectType) TypeName() string

func (*ObjectType) TypeRequiredFeatures

func (t *ObjectType) TypeRequiredFeatures() FeatureSet

type ScalarType

type ScalarType struct {
	Name        string
	Description string
	Directives  []*Directive

	// This type is only available for introspection and use when the given features are enabled.
	RequiredFeatures FeatureSet

	// Should return nil if coercion is impossible.
	LiteralCoercion func(ast.Value) interface{}

	// Should return nil if coercion is impossible.
	VariableValueCoercion func(interface{}) interface{}

	// Should return nil if coercion is impossible. In many cases, this can be the same as
	// VariableValueCoercion.
	ResultCoercion func(interface{}) interface{}
}

func (*ScalarType) CoerceResult

func (t *ScalarType) CoerceResult(result interface{}) (interface{}, error)

func (*ScalarType) CoerceVariableValue

func (t *ScalarType) CoerceVariableValue(v interface{}) (interface{}, error)

func (*ScalarType) IsInputType

func (t *ScalarType) IsInputType() bool

func (*ScalarType) IsOutputType

func (t *ScalarType) IsOutputType() bool

func (*ScalarType) IsSameType

func (t *ScalarType) IsSameType(other Type) bool

func (*ScalarType) IsSubTypeOf

func (t *ScalarType) IsSubTypeOf(other Type) bool

func (*ScalarType) String

func (t *ScalarType) String() string

func (*ScalarType) TypeName

func (t *ScalarType) TypeName() string

func (*ScalarType) TypeRequiredFeatures

func (t *ScalarType) TypeRequiredFeatures() FeatureSet

type Schema

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

func New

func New(def *SchemaDefinition) (*Schema, error)

func (*Schema) Directives

func (s *Schema) Directives() map[string]*DirectiveDefinition

func (*Schema) InterfaceImplementations

func (s *Schema) InterfaceImplementations(name string) []*ObjectType

func (*Schema) MutationType

func (s *Schema) MutationType() *ObjectType

func (*Schema) NamedTypes

func (s *Schema) NamedTypes() map[string]NamedType

func (*Schema) QueryType

func (s *Schema) QueryType() *ObjectType

func (*Schema) SubscriptionType

func (s *Schema) SubscriptionType() *ObjectType

type SchemaDefinition

type SchemaDefinition struct {
	// Directives to define within the schema. For example, you might want to add IncludeDirective
	// and SkipDirective here.
	Directives map[string]*DirectiveDefinition

	Query        *ObjectType
	Mutation     *ObjectType
	Subscription *ObjectType

	// AdditionalTypes is used to add otherwise unreferenced types to the schema.
	AdditionalTypes []NamedType
}

func (*SchemaDefinition) Clone

func (def *SchemaDefinition) Clone() *SchemaDefinition

Creates a deep copy of the given schema definition. This allows you to safely modify descriptions or other attributes of the schema without modifying the original definition.

type Type

type Type interface {
	String() string
	IsInputType() bool
	IsOutputType() bool
	IsSubTypeOf(Type) bool
	IsSameType(Type) bool
	TypeRequiredFeatures() FeatureSet
}

func NullableType

func NullableType(t Type) Type

type UnionType

type UnionType struct {
	Name        string
	Description string
	Directives  []*Directive
	MemberTypes []*ObjectType

	// This type is only available for introspection and use when the given features are enabled.
	RequiredFeatures FeatureSet
}

func (*UnionType) IsInputType

func (d *UnionType) IsInputType() bool

func (*UnionType) IsOutputType

func (d *UnionType) IsOutputType() bool

func (*UnionType) IsSameType

func (d *UnionType) IsSameType(other Type) bool

func (*UnionType) IsSubTypeOf

func (d *UnionType) IsSubTypeOf(other Type) bool

func (*UnionType) String

func (d *UnionType) String() string

func (*UnionType) TypeName

func (d *UnionType) TypeName() string

func (*UnionType) TypeRequiredFeatures

func (d *UnionType) TypeRequiredFeatures() FeatureSet

type WrappedType

type WrappedType interface {
	Type
	Unwrap() Type
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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