Documentation ¶
Index ¶
- Variables
- func Batch(f func([]graphql.FieldContext) []graphql.ResolveResult) func(graphql.FieldContext) (interface{}, error)
- func Connection(config *ConnectionConfig) *graphql.FieldDefinition
- func ConnectionFieldDefinition(config *ConnectionFieldDefinitionConfig) *graphql.FieldDefinition
- func ConnectionInterface(config *ConnectionInterfaceConfig) *graphql.InterfaceType
- func Go(ctx context.Context, f func() (interface{}, error)) graphql.ResolvePromise
- func NonEmptyString(fieldName string) *graphql.FieldDefinition
- func NonNull(t graphql.Type, fieldName string) *graphql.FieldDefinition
- func NonZeroDateTime(fieldName string) *graphql.FieldDefinition
- func PersistedQueryExtension(storage PersistedQueryStorage, ...) func(*graphql.Request) *graphql.Response
- func TimeBasedConnection(config *TimeBasedConnectionConfig) *graphql.FieldDefinition
- type API
- type Config
- func (cfg *Config) AddMutation(name string, def *graphql.FieldDefinition)
- func (cfg *Config) AddNamedType(t graphql.NamedType)
- func (cfg *Config) AddQueryField(name string, def *graphql.FieldDefinition)
- func (cfg *Config) AddSubscription(name string, def *graphql.FieldDefinition)
- func (cfg *Config) MutationType() *graphql.ObjectType
- func (cfg *Config) NodeInterface() *graphql.InterfaceType
- func (cfg *Config) QueryType() *graphql.ObjectType
- type ConnectionConfig
- type ConnectionDirection
- type ConnectionFieldDefinitionConfig
- type ConnectionInterfaceConfig
- type PageInfo
- type PersistedQueryStorage
- type RequestInfo
- type SubscriptionSourceStream
- type TimeBasedConnectionConfig
- type TimeBasedCursor
Constants ¶
This section is empty.
Variables ¶
var DateTimeType = &graphql.ScalarType{ Name: "DateTime", Description: "DateTime represents an RFC-3339 datetime.", LiteralCoercion: func(v ast.Value) interface{} { switch v := v.(type) { case *ast.StringValue: return parseDateTime(v.Value) } return nil }, VariableValueCoercion: parseDateTime, ResultCoercion: func(v interface{}) interface{} { switch v := v.(type) { case time.Time: if b, err := v.MarshalText(); err == nil { return string(b) } } return nil }, }
DateTimeType provides a DateTime implementation that serializing to and from RFC-3339 datetimes.
var LongIntType = &graphql.ScalarType{ Name: "LongInt", Description: "LongInt represents a signed integer that may be longer than 32 bits, but still within JavaScript / IEEE-654's \"safe\" range.", LiteralCoercion: func(v ast.Value) interface{} { switch v := v.(type) { case *ast.IntValue: if n, err := strconv.ParseInt(v.Value, 10, 64); err == nil && n >= minSafeInteger && n <= maxSafeInteger { return n } } return nil }, VariableValueCoercion: coerceLongInt, ResultCoercion: coerceLongInt, }
LongIntType provides a scalar implementation for integers that may be larger than 32 bits, but can still be represented by JavaScript numbers.
var PageInfoType = &graphql.ObjectType{ Name: "PageInfo", Fields: map[string]*graphql.FieldDefinition{ "hasPreviousPage": NonNull(graphql.BooleanType, "HasPreviousPage"), "hasNextPage": NonNull(graphql.BooleanType, "HasNextPage"), "startCursor": NonNull(graphql.StringType, "StartCursor"), "endCursor": NonNull(graphql.StringType, "EndCursor"), }, }
PageInfoType implements the GraphQL type for the page info of a GraphQL Cursor Connection.
Functions ¶
func Batch ¶
func Batch(f func([]graphql.FieldContext) []graphql.ResolveResult) func(graphql.FieldContext) (interface{}, error)
Batch batches up the resolver invocations into a single call. As queries are executed, whenever resolution gets "stuck", all pending batch resolvers will be triggered concurrently. Batch resolvers must return one result for every field context it receives.
func Connection ¶
func Connection(config *ConnectionConfig) *graphql.FieldDefinition
Connection is used to create a connection field that adheres to the GraphQL Cursor Connections Specification.
func ConnectionFieldDefinition ¶
func ConnectionFieldDefinition(config *ConnectionFieldDefinitionConfig) *graphql.FieldDefinition
Returns a minimal connection field definition, with default arguments and cost function defined.
func ConnectionInterface ¶
func ConnectionInterface(config *ConnectionInterfaceConfig) *graphql.InterfaceType
Returns an interface for a connection.
func Go ¶
func Go(ctx context.Context, f func() (interface{}, error)) graphql.ResolvePromise
Go completes resolution asynchronously and concurrently with any other asynchronous resolutions.
func NonEmptyString ¶
func NonEmptyString(fieldName string) *graphql.FieldDefinition
NonEmptyString returns a field that resolves to a string if the field's value is non-empty. Otherwise, the field resolves to nil.
func NonNull ¶
func NonNull(t graphql.Type, fieldName string) *graphql.FieldDefinition
NonNull returns a non-null field that resolves to the given type.
func NonZeroDateTime ¶
func NonZeroDateTime(fieldName string) *graphql.FieldDefinition
NonZeroDateTime returns a field definition that resolves to the value of the field with the given name. If the field's value is the zero time, the field resolves to nil instead.
func PersistedQueryExtension ¶
func PersistedQueryExtension(storage PersistedQueryStorage, execute func(*graphql.Request) *graphql.Response) func(*graphql.Request) *graphql.Response
PersistedQueryExtension implements Apollo persisted queries: https://www.apollographql.com/docs/react/api/link/persisted-queries/
Typically this shouldn't be invoked directly. Instead, set the PersistedQueryStorage Config field.
func TimeBasedConnection ¶
func TimeBasedConnection(config *TimeBasedConnectionConfig) *graphql.FieldDefinition
TimeBasedConnection creates a new connection for edges sorted by time. In addition to the standard first, last, after, and before fields, the connection will have atOrAfterTime and beforeTime fields, which can be used to query a specific time range.
Types ¶
type API ¶
type API struct {
// contains filtered or unexported fields
}
API is responsible for serving your API traffic. Construct an API by creating a Config, then calling NewAPI.
func (*API) CloseHijackedConnections ¶
CloseHijackedConnections closes connections hijacked by ServeGraphQLWS.
func (*API) ServeGraphQL ¶
func (api *API) ServeGraphQL(w http.ResponseWriter, r *http.Request)
ServeGraphQL serves GraphQL HTTP requests. Requests may be GET requests using query string parameters or POST requests with either the application/json or application/graphql content type.
func (*API) ServeGraphQLWS ¶
func (api *API) ServeGraphQLWS(w http.ResponseWriter, r *http.Request)
ServeGraphQLWS serves a GraphQL WebSocket connection. It will serve connections for both the deprecated graphql-ws subprotocol and the newer graphql-transport-ws subprotocol.
This method hijacks connections. To gracefully close them, use CloseHijackedConnections.
type Config ¶
type Config struct { Logger logrus.FieldLogger WebSocketOriginCheck func(r *http.Request) bool // If given, these fields will be added to the Node interface. AdditionalNodeFields map[string]*graphql.FieldDefinition // Invoked to get nodes by their global ids. ResolveNodesByGlobalIds func(ctx context.Context, ids []string) ([]interface{}, error) // If given, Apollo persisted queries are supported by the API: // https://www.apollographql.com/docs/react/api/link/persisted-queries/ PersistedQueryStorage PersistedQueryStorage // When calculating field costs, this is used as the default. This is typically either // `graphql.FieldCost{Resolver: 1}` or left as zero. DefaultFieldCost graphql.FieldCost // Execute is invoked to execute a GraphQL request. If not given, this is simply // graphql.Execute. You may wish to provide this to perform request logging or // pre/post-processing. Execute func(*graphql.Request, *RequestInfo) *graphql.Response // If given, this function is invoked when the servers receives the graphql-ws connection init // payload. If an error is returned, it will be sent to the client and the connection will be // closed. Otherwise the returned context will become associated with the connection. // // This is commonly used for authentication. HandleGraphQLWSInit func(ctx context.Context, parameters json.RawMessage) (context.Context, error) // Explicitly adds named types to the schema. This is generally only required for interface // implementations that aren't explicitly referenced elsewhere in the schema. AdditionalTypes map[string]graphql.NamedType // If given, these function will be executed as the schema is built. It is executed on a clone // of the schema and can be used to make last minute modifications to types, such as injecting // documentation. PreprocessGraphQLSchemaDefinition func(schema *graphql.SchemaDefinition) error // If given, this function will be invoked to get the feature set for a request. Features func(ctx context.Context) graphql.FeatureSet // contains filtered or unexported fields }
Config defines the schema and other parameters for an API.
func (*Config) AddMutation ¶
func (cfg *Config) AddMutation(name string, def *graphql.FieldDefinition)
AddMutation adds a mutation to your schema.
func (*Config) AddNamedType ¶
AddNamedType adds a named type to the schema. This is generally only required for interface implementations that aren't explicitly referenced elsewhere in the schema.
func (*Config) AddQueryField ¶
func (cfg *Config) AddQueryField(name string, def *graphql.FieldDefinition)
AddQueryField adds a field to your schema's query object.
func (*Config) AddSubscription ¶
func (cfg *Config) AddSubscription(name string, def *graphql.FieldDefinition)
AddSubscription adds a subscription operation to your schema.
When a subscription is started, your resolver will be invoked with ctx.IsSubscribe set to true. When this happens, you should return a pointer to a SubscriptionSourceStream (or an error). For example:
Resolve: func(ctx graphql.FieldContext) (interface{}, error) { if ctx.IsSubscribe { ticker := time.NewTicker(time.Second) return &apifu.SubscriptionSourceStream{ EventChannel: ticker.C, Stop: ticker.Stop, }, nil } else if ctx.Object != nil { return ctx.Object, nil } else { return nil, fmt.Errorf("Subscriptions are not supported using this protocol.") } },
func (*Config) MutationType ¶
func (cfg *Config) MutationType() *graphql.ObjectType
MutationType returns the root mutation type.
func (*Config) NodeInterface ¶
func (cfg *Config) NodeInterface() *graphql.InterfaceType
NodeInterface returns the node interface.
func (*Config) QueryType ¶
func (cfg *Config) QueryType() *graphql.ObjectType
QueryType returns the root query type.
type ConnectionConfig ¶
type ConnectionConfig struct { // A prefix to use for the connection and edge type names. For example, if you provide // "Example", the connection type will be named "ExampleConnection" and the edge type will be // "ExampleEdge". NamePrefix string // An optional description for the connection. Description string // An optional deprecation reason for the connection. DeprecationReason string // The direction of the connection. This determines which of the first/last/before/after // arguments are defined on the connection. Direction ConnectionDirection // An optional map of additional arguments to add to the connection. Arguments map[string]*graphql.InputValueDefinition // If getting all edges for the connection is cheap, you can just provide ResolveAllEdges. // ResolveAllEdges should return a slice value, with one item for each edge, and a function that // can be used to sort the cursors produced by EdgeCursor. ResolveAllEdges func(ctx graphql.FieldContext) (edgeSlice interface{}, cursorLess func(a, b interface{}) bool, err error) // If getting all edges for the connection is too expensive for ResolveAllEdges, you can provide // ResolveEdges. ResolveEdges is just like ResolveAllEdges, but is only required to return edges // within the range defined by the given cursors and is only required to return up to `limit` // edges. If limit is negative, the last edges within the range should be returned instead of // the first. // // Returning extra edges or out-of-order edges is fine. They will be sorted and filtered // automatically. However, you should ensure that no duplicate edges are returned. // // If desired, edges outside of the given range may be returned to indicate the presence of more // pages before or after the given range. This is completely optional, and the connection's // behavior will be fully compliant with the Relay Pagination spec regardless. However, // providing these additional edges will allow hasNextPage and hasPreviousPage to be true in // scenarios where the spec allows them to be false for performance reasons. ResolveEdges func(ctx graphql.FieldContext, after, before interface{}, limit int) (edgeSlice interface{}, cursorLess func(a, b interface{}) bool, err error) // If you use ResolveEdges, you can optionally provide ResolveTotalCount to add a totalCount // field to the connection. If you use ResolveAllEdges, there is no need to provide this. ResolveTotalCount func(ctx graphql.FieldContext) (interface{}, error) // CursorType allows the connection to deserialize cursors. It is required for all connections. CursorType reflect.Type // EdgeCursor should return a value that can be used to determine the edge's relative ordering. // For example, this might be a struct with a name and id for a connection whose edges are // sorted by name. The value must be able to be marshaled to and from binary. This function // should return the type of cursor assigned to CursorType. EdgeCursor func(edge interface{}) interface{} // EdgeFields should provide definitions for the fields of each node. You must provide the // "node" field, but the "cursor" field will be provided for you. EdgeFields map[string]*graphql.FieldDefinition // The connection will implement these interfaces. If any of the interfaces define an edge // field as an interface, this connection's edges will also implement that interface. ImplementedInterfaces []*graphql.InterfaceType // This connection is only available for introspection and use when the given features are enabled. RequiredFeatures graphql.FeatureSet }
ConnectionConfig defines the configuration for a connection that adheres to the GraphQL Cursor Connections Specification.
type ConnectionDirection ¶
type ConnectionDirection int
const ( ConnectionDirectionBidirectional ConnectionDirection = iota ConnectionDirectionForwardOnly ConnectionDirectionBackwardOnly )
type ConnectionFieldDefinitionConfig ¶
type ConnectionFieldDefinitionConfig struct { // The type of the connection. Type graphql.Type // The direction of the connection. Direction ConnectionDirection // An optional description for the connection field. Description string // An optional deprecation reason for the connection field. DeprecationReason string // An optional map of additional arguments to add to the field. Arguments map[string]*graphql.InputValueDefinition // This connection is only available for introspection and use when the given features are enabled. RequiredFeatures graphql.FeatureSet }
Defines the configuration for a connection interface.
type ConnectionInterfaceConfig ¶
type ConnectionInterfaceConfig struct { // A prefix to use for the connection and edge type names. For example, if you provide // "Example", the connection type will be named "ExampleConnection" and the edge type will be // "ExampleEdge". NamePrefix string // EdgeFields should provide definitions for the fields of each node. You must provide the // "node" field, but the "cursor" field will be provided for you. EdgeFields map[string]*graphql.FieldDefinition // If true, implementations must provide the "totalCount" field. HasTotalCount bool // This connection is only available for introspection and use when the given features are enabled. RequiredFeatures graphql.FeatureSet }
Defines the configuration for a connection interface.
type PersistedQueryStorage ¶
type PersistedQueryStorage interface { // GetPersistedQuery should return the query if it's available or an empty string otherwise. GetPersistedQuery(ctx context.Context, hash []byte) string // PersistQuery should persist the query with the given hash. PersistQuery(ctx context.Context, query string, hash []byte) }
PersistedQueryStorage represents the storage backend for persisted queries. Storage operations are done on a best effort basis and cannot return errors – any errors that happen internally will not prevent the execution of a query (though it might force clients to make additional requests).
type RequestInfo ¶
type RequestInfo struct {
Cost int
}
type SubscriptionSourceStream ¶
type SubscriptionSourceStream struct { // A channel of events. The channel can be of any type. EventChannel interface{} // Stop is invoked when the subscription should be stopped and the event channel should be // closed. Stop func() }
SubscriptionSourceStream defines the source stream for a subscription.
type TimeBasedConnectionConfig ¶
type TimeBasedConnectionConfig struct { // An optional description for the connection. Description string // An optional deprecation reason for the connection. DeprecationReason string // A required prefix for the type names. For a field named "friendsConnection" on a User type, // the recommended prefix would be "UserFriends". This will result in types named // "UserFriendsConnection" and "UserFriendsEdge". NamePrefix string // This function should return a TimeBasedCursor for the given edge. EdgeCursor func(edge interface{}) TimeBasedCursor // Returns the fields for the edge. This should always at least include a "node" field. EdgeFields map[string]*graphql.FieldDefinition // The getter for the edges. If limit is zero, all edges within the given range should be // returned. If limit is greater than zero, up to limit edges at the start of the range should // be returned. If limit is less than zero, up to -limit edge at the end of the range should be // returned. EdgeGetter func(ctx graphql.FieldContext, minTime time.Time, maxTime time.Time, limit int) (interface{}, error) // An optional map of additional arguments to add to the connection. Arguments map[string]*graphql.InputValueDefinition // To support the "totalCount" connection field, you can provide this method. ResolveTotalCount func(ctx graphql.FieldContext) (interface{}, error) // The connection will implement these interfaces. If any of the interfaces define an edge // field as an interface, this connection's edges will also implement that interface. ImplementedInterfaces []*graphql.InterfaceType // This connection is only available for introspection and use when the given features are enabled. RequiredFeatures graphql.FeatureSet }
TimeBasedConnectionConfig defines the configuration for a time-based connection that adheres to the GraphQL Cursor Connections Specification.
type TimeBasedCursor ¶
TimeBasedCursor represents the data embedded in cursors for time-based connections.
func NewTimeBasedCursor ¶
func NewTimeBasedCursor(t time.Time, id string) TimeBasedCursor
NewTimeBasedCursor constructs a TimeBasedCursor.