schema

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// QuerySchema gets basic info about the schema
	QuerySchema = `query { __schema { mutationType { name } queryType { name } subscriptionType { name } } }`

	// QuerySchemaTypes is used to fetch all of the data types in the schema
	QuerySchemaTypes = `query { __schema { types { ...FullType } } }` + " " + fragmentFullType

	// QueryType returns all of the data on that specific type, useful for fetching queries / mutations
	QueryType = `query($typeName:String!) { __type(name: $typeName) { ...FullType } }` + " " + fragmentFullType
)

https://github.com/graphql/graphql-js/blob/master/src/utilities/getIntrospectionQuery.js#L35

Modified from the following as we only care about the Types
query IntrospectionQuery {
  __schema {
    directives { name description locations args { ...InputValue } }
    mutationType { name }
    queryType { name }
    subscriptionType { name }
    types { ...FullType }
  }
}

Variables

This section is empty.

Functions

func ExpandTypes

func ExpandTypes(s *Schema, pkgConfig *config.PackageConfig) (*[]*Type, error)

ExpandTypes receives a set of config.TypeConfig, which is then expanded to include all the nested types from the fields.

func PrefixLineTab added in v0.2.4

func PrefixLineTab(s string) string

PrefixLineTab adds a \t character to the beginning of each line.

Types

type EnumValue

type EnumValue struct {
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
	Kind        Kind   `json:"kind,omitempty"`

	IsDeprecated      bool   `json:"isDeprecated"`
	DeprecationReason string `json:"deprecationReason"`
}

func (*EnumValue) GetDescription

func (e *EnumValue) GetDescription() string

GetDescription formats the description into a GoDoc comment.

func (*EnumValue) GetName

func (e *EnumValue) GetName() string

GetName returns a recusive lookup of the type name

type Expander added in v0.2.4

type Expander struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Expander is mean to hold the state while the schema is being expanded.

func NewExpander added in v0.2.4

func NewExpander(schema *Schema, skipTypes []string) *Expander

NewExpander is to return a sane Expander.

func (*Expander) ExpandType added in v0.2.4

func (x *Expander) ExpandType(t *Type) (err error)

ExpandType is used to populate the expander, one Type at a time.

func (*Expander) ExpandTypeFromName added in v0.2.4

func (x *Expander) ExpandTypeFromName(name string) error

ExpandTypeFromName will expand a named type if found or error.

func (*Expander) ExpandedTypes added in v0.2.4

func (x *Expander) ExpandedTypes() *[]*Type

ExpandedTypes is the final report of all the expanded types.

type Field

type Field struct {
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
	Kind        Kind   `json:"kind,omitempty"`

	Type         TypeRef     `json:"type"`
	Args         []Field     `json:"args,omitempty"`
	DefaultValue interface{} `json:"defaultValue,omitempty"`
}

Field is an attribute of a schema Type object.

func (*Field) GetDescription

func (f *Field) GetDescription() string

GetDescription formats the description into a GoDoc comment.

func (*Field) GetName

func (f *Field) GetName() string

GetName returns a recusive lookup of the type name

func (*Field) GetTags

func (f *Field) GetTags() string

GetTags is used to return the Go struct tags for a field.

func (*Field) GetTagsWithOverrides added in v0.9.0

func (f *Field) GetTagsWithOverrides(parentType Type, pkgConfig *config.PackageConfig) string

func (*Field) GetTypeNameWithOverride

func (f *Field) GetTypeNameWithOverride(pkgConfig *config.PackageConfig) (string, error)

GetTypeNameWithOverride returns the typeName, taking into consideration any FieldTypeOverride specified in the PackageConfig.

func (*Field) HasRequiredArg added in v0.4.0

func (f *Field) HasRequiredArg() bool

func (*Field) IsEnum added in v0.3.0

func (f *Field) IsEnum() bool

func (*Field) IsPrimitiveType added in v0.3.0

func (f *Field) IsPrimitiveType() bool

func (*Field) IsRequired added in v0.3.0

func (f *Field) IsRequired() bool

Convenience method that proxies to TypeRef method

func (*Field) IsScalarID added in v0.3.0

func (f *Field) IsScalarID() bool

Convenience method that proxies to TypeRef method

type Kind

type Kind string
const (
	KindENUM        Kind = "ENUM"
	KindInputObject Kind = "INPUT_OBJECT"
	KindInterface   Kind = "INTERFACE"
	KindList        Kind = "LIST"
	KindNonNull     Kind = "NON_NULL"
	KindObject      Kind = "OBJECT"
	KindScalar      Kind = "SCALAR"
)

type QueryArg added in v0.2.4

type QueryArg struct {
	Key   string
	Value string
}

QueryArg is the key value pair for an nerdgraph query argument on an endpoint. These might be required, or not.

type QueryResponse

type QueryResponse struct {
	Data struct {
		Type   Type   `json:"__type"`
		Schema Schema `json:"__schema"`
	} `json:"data"`
}

func ParseResponse

func ParseResponse(resp *http.Response) (*QueryResponse, error)

type QueryTypeVars

type QueryTypeVars struct {
	Name string `json:"typeName"`
}

Helper function to make queries, lives here with the constant query

type Schema

type Schema struct {
	MutationType     *Type   `json:"mutationType,omitempty"`
	QueryType        *Type   `json:"queryType,omitempty"`
	SubscriptionType *Type   `json:"subscriptionType,omitempty"`
	Types            []*Type `json:"types,omitempty"`
}

Schema contains data about the GraphQL schema as returned by the server

func Load

func Load(file string) (*Schema, error)

Load takes a file and unmarshals the JSON into a Schema struct

func (*Schema) BuildQueryArgsForEndpoint added in v0.3.0

func (s *Schema) BuildQueryArgsForEndpoint(t *Type, fields []string, includeArguments []string) []QueryArg

BuildQueryArgsForEndpoint is meant to fill in the data necessary for a query(<args_go_here>) string. i.e: query($guids: [String]!) { actor ...

func (*Schema) GetInputFieldsForQueryPath added in v0.3.0

func (s *Schema) GetInputFieldsForQueryPath(queryPath []string) map[string][]Field

GetInputFieldsForQueryPath is intended to return the fields that are available as input arguments when performing a query using the received query path. For example, a []string{"actor", "account"} would look at `actor { account { ...` for the representivate types at those schema levels and determine collect the available arguments for return.

func (*Schema) GetQueryArg added in v0.4.0

func (s *Schema) GetQueryArg(field Field) QueryArg

GetQueryArg returns the GraphQL formatted Query Argument including formatting for List and NonNull

Example:

{
  "name": "tags",
  "type": { "kind": "NON_NULL", "ofType": { "kind": "LIST", "ofType": { "kind": "NON_NULL", "ofType": { "name": "TaggingTagInput", "kind": "INPUT_OBJECT" } } } }
}

[TaggingTagInput!]!

func (*Schema) GetQueryStringForEndpoint added in v0.2.4

func (s *Schema) GetQueryStringForEndpoint(typePath []*Type, fieldPath []string, endpoint config.EndpointConfig) string

GetQueryStringForEndpoint packs a nerdgraph query header and footer around the set of query fields for a given type name and endpoint.

func (*Schema) GetQueryStringForMutation added in v0.2.4

func (s *Schema) GetQueryStringForMutation(mutation *Field, cfg config.MutationConfig) string

GetQueryStringForMutation packs a nerdgraph query header and footer around the set of query fields GraphQL mutation name.

func (*Schema) LookupMutationByName added in v0.2.4

func (s *Schema) LookupMutationByName(mutationName string) (*Field, error)

LookupMutationByName searches for the specific mutation by name and returns a reference if found.

func (*Schema) LookupMutationsByPattern added in v0.8.0

func (s *Schema) LookupMutationsByPattern(pattern string) []Field

LookupMutationsByPattern finds all matching mutations given the regexp

func (*Schema) LookupQueryFieldsByFieldPath added in v0.6.1

func (s *Schema) LookupQueryFieldsByFieldPath(fieldPath []string) ([]string, error)

LookupQueryFieldsByFieldPath is used to turn a GraphQL field path into Golang struct path

func (*Schema) LookupQueryTypesByFieldPath added in v0.2.4

func (s *Schema) LookupQueryTypesByFieldPath(fieldPath []string) ([]*Type, error)

LookupQueryTypesByFieldPath is used to retrieve the types, when all you know is the path of field names to an endpoint.

func (*Schema) LookupRootMutationTypeFieldByName added in v0.3.0

func (s *Schema) LookupRootMutationTypeFieldByName(name string) (*Field, error)

func (*Schema) LookupRootQueryTypeFieldByName added in v0.3.0

func (s *Schema) LookupRootQueryTypeFieldByName(name string) (*Field, error)

func (*Schema) LookupTypeByName

func (s *Schema) LookupTypeByName(typeName string) (*Type, error)

LookupTypeByName digs in the schema for a type that matches the given name. This is commonly used for retrieving the Type of a TypeRef, since the name is the only piece of data to go on.

func (*Schema) QueryFieldsForTypeName added in v0.2.4

func (s *Schema) QueryFieldsForTypeName(name string, maxDepth int, isMutation bool, excludeFields []string) string

QueryFieldsForTypeName will lookup a type by the received name, and return the query fields for that type, or log an error and return and empty string. The returned string is used in a mutation, so that the relevant fields are returned once the mutation is performed, or during a query for a given type.

func (*Schema) RecursiveLookupFieldByPath added in v0.3.0

func (s *Schema) RecursiveLookupFieldByPath(queryFieldPath []string, obj *Type) *Field

RecursiveLookupFieldByPath traverses the GraphQL query types based on the provided query fields path which represents a GraphQL query. This method returns the last field of the last "node" of the query tree "branch" - i.e. actor.apiAccess.key where `key` is the primary target "leaf".

e.g. `query { actor { apiAccess { key }}}` would have a path of ["actor", "apiAccess", "key"]

func (*Schema) Save

func (s *Schema) Save(file string) error

Save writes the schema out to a file for later use.

type Type

type Type struct {
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
	Kind        Kind   `json:"kind,omitempty"`

	EnumValues    []EnumValue `json:"enumValues,omitempty"`
	Fields        []Field     `json:"fields,omitempty"`
	InputFields   []Field     `json:"inputFields,omitempty"`
	Interfaces    []TypeRef   `json:"interfaces,omitempty"`
	PossibleTypes []TypeRef   `json:"possibleTypes,omitempty"`
	SkipFields    []string    `json:"skipFields,omitempty"`
}

Type defines a specific type within the schema

func (*Type) GetDescription

func (t *Type) GetDescription() string

GetDescription formats the description into a GoDoc comment.

func (*Type) GetField added in v0.3.0

func (t *Type) GetField(name string) (*Field, error)

func (*Type) GetName

func (t *Type) GetName() string

GetName returns the name of a Type, formatted for Go title casing.

func (*Type) GetQueryStringFields added in v0.2.4

func (t *Type) GetQueryStringFields(s *Schema, depth, maxDepth int, isMutation bool, excludeFields []string) string

func (*Type) IsGoType

func (t *Type) IsGoType() bool

IsGoType is used to determine if a type in NerdGraph is already a native type of Golang.

func (*Type) Save

func (t *Type) Save(file string) error

Save writes the schema out to a file

type TypeRef

type TypeRef struct {
	Name        string `json:"name,omitempty"`
	Description string `json:"description,omitempty"`
	Kind        Kind   `json:"kind,omitempty"`

	OfType *TypeRef `json:"ofType,omitempty"`
}

TypeRef is a GraphQL reference to a Type.

func (*TypeRef) GetDescription

func (r *TypeRef) GetDescription() string

GetDescription looks for anything in the description before \n\n---\n and filters off anything after that (internal messaging that is not useful here)

func (*TypeRef) GetKinds

func (r *TypeRef) GetKinds() []Kind

GetKinds returns an array or the type kind

func (*TypeRef) GetName

func (r *TypeRef) GetName() string

GetName returns a recusive lookup of the type name

func (*TypeRef) GetType

func (r *TypeRef) GetType() (string, bool, error)

GetType resolves the given SchemaInputField into a field name to use on a go struct.

type, recurse, error

func (*TypeRef) GetTypeName

func (r *TypeRef) GetTypeName() string

GetTypeName returns the name of the current type, or performs a recursive lookup to determine the name of the nested OfType object's name. In the case that neither are matched, the string "UNKNOWN" is returned. In the GraphQL schema, a non-empty name seems to appear only once in a TypeRef tree, so we want to find the first non-empty.

func (*TypeRef) IsInputObject added in v0.3.0

func (r *TypeRef) IsInputObject() bool

func (*TypeRef) IsInterface added in v0.4.0

func (r *TypeRef) IsInterface() bool

IsList determines if a TypeRef is of a KIND INTERFACE.

func (*TypeRef) IsList

func (r *TypeRef) IsList() bool

IsList determines if a TypeRef is of a KIND LIST.

func (*TypeRef) IsNonNull added in v0.3.0

func (r *TypeRef) IsNonNull() bool

IsNonNull walks down looking for NON_NULL kind, however that can appear multiple times, so this is likely a bit deceiving... Example:

{
   "name": "tags",
   "description": "An array of key-values pairs to represent a tag. For example:  Team:TeamName.",
   "type": {
    "kind": "NON_NULL",
    "ofType": {
     "kind": "LIST",
     "ofType": {
      "kind": "NON_NULL",
      "ofType": {
       "name": "TaggingTagInput",
       "kind": "INPUT_OBJECT"
      }
     }
    }
   }
  }
 ]
}

func (*TypeRef) IsScalarID added in v0.3.0

func (r *TypeRef) IsScalarID() bool

Jump to

Keyboard shortcuts

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