Complete GraphQL implementation in Go with schema definition, query execution, and validation.
The type system is the foundation of a GraphQL API. Use these types to build a schema that describes your data and how it can be queried.
A schema is the entry point for all GraphQL operations. It defines the root query type, optional mutation type, and optional subscription type.
type SchemaConfig struct {
Query *Object
Mutation *Object // optional
Subscription *Object // optional
Types []Type // additional types to include
Directives []*Directive // custom directives; if nil, uses SpecifiedDirectives
Extensions []Extension // schema-level extensions
}
func NewSchema(config SchemaConfig) (Schema, error)type Schema struct{} // opaque
func (gq *Schema) QueryType() *Object
func (gq *Schema) MutationType() *Object
func (gq *Schema) SubscriptionType() *Object
func (gq *Schema) TypeMap() TypeMap
func (gq *Schema) Type(name string) Type
func (gq *Schema) Directives() []*Directive
func (gq *Schema) Directive(name string) *Directive
func (gq *Schema) PossibleTypes(abstractType Abstract) []*Object
func (gq *Schema) IsPossibleType(abstractType Abstract, possibleType *Object) bool
func (gq *Schema) AddExtensions(e ...Extension)
func (gq *Schema) AddImplementation() error
func (gq *Schema) AppendType(objectType Type) errorExample:
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
Mutation: mutationType,
})The most common type. Defines a set of fields that can be queried.
type ObjectConfig struct {
Name string
Interfaces interface{} // []*Interface or func() []*Interface (InterfacesThunk)
Fields interface{} // Fields or FieldsThunk
IsTypeOf IsTypeOfFn
Description string
}
func NewObject(config ObjectConfig) *Objecttype Object struct {
PrivateName string `json:"name"`
PrivateDescription string `json:"description"`
IsTypeOf IsTypeOfFn
// unexported fields
}
func (gt *Object) Name() string
func (gt *Object) Description() string
func (gt *Object) Fields() FieldDefinitionMap
func (gt *Object) Interfaces() []*Interface
func (gt *Object) AddFieldConfig(fieldName string, fieldConfig *Field)
func (gt *Object) String() string
func (gt *Object) Error() errortype Fields map[string]*Field
type FieldsThunk func() Fields // use for circular references
type Field struct {
Name string `json:"name"`
Type Output `json:"type"`
Args FieldConfigArgument `json:"args"`
Resolve FieldResolveFn `json:"-"`
Subscribe FieldResolveFn `json:"-"` // for subscription fields
DeprecationReason string `json:"deprecationReason"`
Description string `json:"description"`
}
type FieldResolveFn func(p ResolveParams) (interface{}, error)Example with thunk (for circular references):
var personType = graphql.NewObject(graphql.ObjectConfig{
Name: "Person",
Fields: graphql.FieldsThunk(func() graphql.Fields {
return graphql.Fields{
"name": &graphql.Field{Type: graphql.String},
"friends": &graphql.Field{Type: graphql.NewList(personType)},
}
}),
})type FieldConfigArgument map[string]*ArgumentConfig
type ArgumentConfig struct {
Type Input `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
Description string `json:"description"`
}type FieldDefinitionMap map[string]*FieldDefinition
type FieldDefinition struct {
Name string `json:"name"`
Description string `json:"description"`
Type Output `json:"type"`
Args []*Argument `json:"args"`
Resolve FieldResolveFn `json:"-"`
Subscribe FieldResolveFn `json:"-"`
DeprecationReason string `json:"deprecationReason"`
}type FieldResolver interface {
Resolve(p ResolveParams) (interface{}, error)
}DefaultResolveFn checks if the source implements FieldResolver. If so, it calls Resolve. Otherwise, it reflects on the source struct field with the same name as the field.
func DefaultResolveFn(p ResolveParams) (interface{}, error)func BindFields(obj interface{}) Fields
// Binds exported struct fields to GraphQL Fields. Cannot handle recursive types.
func BindArg(obj interface{}, tags ...string) FieldConfigArgument
// Lazily binds struct fields to FieldConfigArgument using struct tags (default: "json").
const TAG = "json" // struct tag name used by BindFieldsDefines a set of fields that multiple object types can implement.
type InterfaceConfig struct {
Name string
Fields interface{} // Fields or FieldsThunk
ResolveType ResolveTypeFn
Description string
}
func NewInterface(config InterfaceConfig) *Interfacetype Interface struct {
PrivateName string `json:"name"`
PrivateDescription string `json:"description"`
ResolveType ResolveTypeFn
// unexported fields
}
func (it *Interface) Name() string
func (it *Interface) Description() string
func (it *Interface) Fields() (fields FieldDefinitionMap)
func (it *Interface) AddFieldConfig(fieldName string, fieldConfig *Field)
func (it *Interface) String() string
func (it *Interface) Error() errortype ResolveTypeFn func(p ResolveTypeParams) *Object
type ResolveTypeParams struct {
Value interface{}
Info ResolveInfo
Context context.Context
}Describes a type that can be one of several object types.
type UnionConfig struct {
Name string
Types interface{} // []*Object or UnionTypesThunk
ResolveType ResolveTypeFn
Description string
}
type UnionTypesThunk func() []*Object
func NewUnion(config UnionConfig) *Uniontype Union struct {
PrivateName string `json:"name"`
PrivateDescription string `json:"description"`
ResolveType ResolveTypeFn
// unexported fields
}
func (ut *Union) Name() string
func (ut *Union) Description() string
func (ut *Union) Types() []*Object
func (ut *Union) String() string
func (ut *Union) Error() errorDefines a set of named values.
type EnumConfig struct {
Name string `json:"name"`
Values EnumValueConfigMap `json:"values"`
Description string `json:"description"`
}
type EnumValueConfigMap map[string]*EnumValueConfig
type EnumValueConfig struct {
Value interface{} `json:"value"`
DeprecationReason string `json:"deprecationReason"`
Description string `json:"description"`
}
func NewEnum(config EnumConfig) *Enumtype Enum struct {
PrivateName string `json:"name"`
PrivateDescription string `json:"description"`
// unexported fields
}
func (gt *Enum) Name() string
func (gt *Enum) Description() string
func (gt *Enum) Values() []*EnumValueDefinition
func (gt *Enum) Serialize(value interface{}) interface{}
func (gt *Enum) ParseValue(value interface{}) interface{}
func (gt *Enum) ParseLiteral(valueAST ast.Value) interface{}
func (gt *Enum) String() string
func (gt *Enum) Error() error
type EnumValueDefinition struct {
Name string `json:"name"`
Value interface{} `json:"value"`
DeprecationReason string `json:"deprecationReason"`
Description string `json:"description"`
}Example:
episodeEnum := graphql.NewEnum(graphql.EnumConfig{
Name: "Episode",
Values: graphql.EnumValueConfigMap{
"NEWHOPE": &graphql.EnumValueConfig{Value: 4},
"EMPIRE": &graphql.EnumValueConfig{Value: 5},
"JEDI": &graphql.EnumValueConfig{Value: 6},
},
})Custom leaf values with serialize/parse logic.
type ScalarConfig struct {
Name string `json:"name"`
Description string `json:"description"`
Serialize SerializeFn
ParseValue ParseValueFn
ParseLiteral ParseLiteralFn
}
type SerializeFn func(value interface{}) interface{}
type ParseValueFn func(value interface{}) interface{}
type ParseLiteralFn func(valueAST ast.Value) interface{}
func NewScalar(config ScalarConfig) *Scalartype Scalar struct {
PrivateName string `json:"name"`
PrivateDescription string `json:"description"`
// unexported fields
}
func (st *Scalar) Name() string
func (st *Scalar) Description() string
func (st *Scalar) Serialize(value interface{}) interface{}
func (st *Scalar) ParseValue(value interface{}) interface{}
func (st *Scalar) ParseLiteral(valueAST ast.Value) interface{}
func (st *Scalar) String() string
func (st *Scalar) Error() errorA structured collection of fields used as argument values.
type InputObjectConfig struct {
Name string `json:"name"`
Fields interface{} `json:"fields"` // InputObjectConfigFieldMap or InputObjectConfigFieldMapThunk
Description string `json:"description"`
}
type InputObjectConfigFieldMap map[string]*InputObjectFieldConfig
type InputObjectConfigFieldMapThunk func() InputObjectConfigFieldMap
type InputObjectFieldConfig struct {
Type Input `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
Description string `json:"description"`
}
func NewInputObject(config InputObjectConfig) *InputObjecttype InputObject struct {
PrivateName string `json:"name"`
PrivateDescription string `json:"description"`
// unexported fields
}
func (gt *InputObject) Name() string
func (gt *InputObject) Description() string
func (gt *InputObject) Fields() InputObjectFieldMap
func (gt *InputObject) AddFieldConfig(fieldName string, fieldConfig *InputObjectFieldConfig)
func (gt *InputObject) String() string
func (gt *InputObject) Error() error
type InputObjectFieldMap map[string]*InputObjectField
type InputObjectField struct {
PrivateName string `json:"name"`
Type Input `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
PrivateDescription string `json:"description"`
}
func (st *InputObjectField) Name() string
func (st *InputObjectField) Description() string
func (st *InputObjectField) String() string
func (st *InputObjectField) Error() errortype List struct {
OfType Type `json:"ofType"`
// unexported fields
}
func NewList(ofType Type) *List
func (gl *List) Name() string
func (gl *List) Description() string
func (gl *List) String() string
func (gl *List) Error() errortype NonNull struct {
OfType Type `json:"ofType"`
// unexported fields
}
func NewNonNull(ofType Type) *NonNull
func (gl *NonNull) Name() string
func (gl *NonNull) Description() string
func (gl *NonNull) String() string
func (gl *NonNull) Error() errortype DirectiveConfig struct {
Name string `json:"name"`
Description string `json:"description"`
Locations []string `json:"locations"`
Args FieldConfigArgument `json:"args"`
}
func NewDirective(config DirectiveConfig) *Directive
type Directive struct {
Name string `json:"name"`
Description string `json:"description"`
Locations []string `json:"locations"`
Args []*Argument `json:"args"`
// unexported fields
}const (
// Executable locations
DirectiveLocationQuery = "QUERY"
DirectiveLocationMutation = "MUTATION"
DirectiveLocationSubscription = "SUBSCRIPTION"
DirectiveLocationField = "FIELD"
DirectiveLocationFragmentDefinition = "FRAGMENT_DEFINITION"
DirectiveLocationFragmentSpread = "FRAGMENT_SPREAD"
DirectiveLocationInlineFragment = "INLINE_FRAGMENT"
// Type system locations
DirectiveLocationSchema = "SCHEMA"
DirectiveLocationScalar = "SCALAR"
DirectiveLocationObject = "OBJECT"
DirectiveLocationFieldDefinition = "FIELD_DEFINITION"
DirectiveLocationArgumentDefinition = "ARGUMENT_DEFINITION"
DirectiveLocationInterface = "INTERFACE"
DirectiveLocationUnion = "UNION"
DirectiveLocationEnum = "ENUM"
DirectiveLocationEnumValue = "ENUM_VALUE"
DirectiveLocationInputObject = "INPUT_OBJECT"
DirectiveLocationInputFieldDefinition = "INPUT_FIELD_DEFINITION"
)var IncludeDirective *Directive // @include(if: Boolean!)
var SkipDirective *Directive // @skip(if: Boolean!)
var DeprecatedDirective *Directive // @deprecated(reason: String)
var SpecifiedDirectives = []*Directive{IncludeDirective, SkipDirective, DeprecatedDirective}
const DefaultDeprecationReason = "No longer supported"type Argument struct {
PrivateName string `json:"name"`
Type Input `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
PrivateDescription string `json:"description"`
}
func (st *Argument) Name() string
func (st *Argument) Description() string
func (st *Argument) String() string
func (st *Argument) Error() error// FieldArgument represents a resolved argument on a field definition.
type FieldArgument struct {
Name string `json:"name"`
Type Type `json:"type"`
DefaultValue interface{} `json:"defaultValue"`
Description string `json:"description"`
}var Int *Scalar // GraphQL Int (32-bit signed)
var Float *Scalar // GraphQL Float (double-precision)
var String *Scalar // GraphQL String (UTF-8)
var Boolean *Scalar // GraphQL Boolean
var ID *Scalar // GraphQL ID (serialized as string)
var DateTime *Scalar // RFC 3339 date-timetype Type interface {
Name() string
Description() string
String() string
Error() error
}
type Input interface {
Name() string
Description() string
String() string
Error() error
}
type Output interface {
Name() string
Description() string
String() string
Error() error
}
type Leaf interface {
Name() string
Description() string
String() string
Error() error
Serialize(value interface{}) interface{}
}
type Composite interface {
Name() string
Description() string
String() string
Error() error
}
type Abstract interface {
Name() string
}
type Named interface {
String() string
}
type Nullable interface{}
type TypeMap map[string]Typefunc IsInputType(ttype Type) bool
func IsOutputType(ttype Type) bool
func IsLeafType(ttype Type) bool
func IsCompositeType(ttype interface{}) bool
func IsAbstractType(ttype interface{}) bool
func GetNamed(ttype Type) Named
func GetNullable(ttype Type) Nullableconst (
TypeKindScalar = "SCALAR"
TypeKindObject = "OBJECT"
TypeKindInterface = "INTERFACE"
TypeKindUnion = "UNION"
TypeKindEnum = "ENUM"
TypeKindInputObject = "INPUT_OBJECT"
TypeKindList = "LIST"
TypeKindNonNull = "NON_NULL"
)These are automatically added to every schema:
var SchemaType *Object // __Schema
var TypeType *Object // __Type
var FieldType *Object // __Field
var InputValueType *Object // __InputValue
var EnumValueType *Object // __EnumValue
var DirectiveType *Object // __Directive
var TypeKindEnumType *Enum // __TypeKind
var DirectiveLocationEnumType *Enum // __DirectiveLocation
var SchemaMetaFieldDef *FieldDefinition // __schema field
var TypeMetaFieldDef *FieldDefinition // __type field
var TypeNameMetaFieldDef *FieldDefinition // __typename fieldvar NameRegExp = regexp.MustCompile("^[_a-zA-Z][_a-zA-Z0-9]*$")type InterfacesThunk func() []*Interface
type UnionTypesThunk func() []*ObjectUse thunks in ObjectConfig.Interfaces and UnionConfig.Types to defer evaluation for circular references.
Install with Tessl CLI
npx tessl i tessl/golang-github-com--graphql-go--graphqldocs