pagination

package module
v0.0.0-...-3467410 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2018 License: MIT Imports: 9 Imported by: 0

README

graphql-relay-go Build Status

This repository is a fork of https://github.com/graphql-go/relay

At stratumn, we wanted an hybrid system between react-relay implementation of pagination and flat lists

So we made the choice to declare pagination as above:

  type StuffList {
    # list of segments
    items: [Stuff!]!
    # information about the pagination
    pageInfo: PageInfo!
    # total count of entities in DB
    totalCount: Int!
  }

A Go/Golang library to help construct a graphql-go server supporting react-relay.

Source code for demo can be found at https://github.com/graphql-go/playground

Test
$ go get github.com/stratumn/graphql-pagination-go
$ go build && go test ./...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ListArgs = graphql.FieldConfigArgument{
	"before": &graphql.ArgumentConfig{
		Type: graphql.String,
	},
	"after": &graphql.ArgumentConfig{
		Type: graphql.String,
	},
	"first": &graphql.ArgumentConfig{
		Type: graphql.Int,
	},
	"last": &graphql.ArgumentConfig{
		Type: graphql.Int,
	},
}

ListArgs returns a GraphQLFieldConfigArgumentMap appropriate to include on a field whose return type is a list type.

Functions

func CursorToOffset

func CursorToOffset(cursor ListCursor) (int, error)

CursorToOffset re-derives the offset from the cursor string.

func GetOffsetWithDefault

func GetOffsetWithDefault(cursor ListCursor, defaultOffset int) int

GetOffsetWithDefault extracts the offset of a cursor with a default value.

func GlobalIDField

func GlobalIDField(typeName string, idFetcher GlobalIDFetcherFn) *graphql.Field

GlobalIDField creates the configuration for an id field on a Item, using `toGlobalId` to construct the ID from the provided typename. The type-specific ID is fetcher by calling idFetcher on the object, or if not provided, by accessing the `id` property on the object.

func MutationWithClientMutationID

func MutationWithClientMutationID(config MutationConfig) *graphql.Field

MutationWithClientMutationID returns a GraphQLField for the mutation described by the provided MutationConfig.

func NewListArgs

NewListArgs adds pagination arguments to configMap

func PluralIdentifyingRootField

func PluralIdentifyingRootField(config PluralIdentifyingRootFieldConfig) *graphql.Field

PluralIdentifyingRootField is ...

func ToGlobalID

func ToGlobalID(ttype string, id string) string

ToGlobalID takes a type name and an ID specific to that type name, and returns a "global ID" that is unique among all types.

Types

type ArraySliceMetaInfo

type ArraySliceMetaInfo struct {
	SliceStart  int `json:"sliceStart"`
	ArrayLength int `json:"arrayLength"`
}

ArraySliceMetaInfo describes which part of array you want to work on.

type GlobalIDFetcherFn

type GlobalIDFetcherFn func(obj interface{}, info graphql.ResolveInfo, ctx context.Context) (string, error)

GlobalIDFetcherFn returns the id of an object

type GraphQLListDefinitions

type GraphQLListDefinitions struct {
	ListType *graphql.Object `json:"listType"`
}

GraphQLListDefinitions is the GraphQL object type for a list

func ListDefinitions

func ListDefinitions(config ListConfig) *GraphQLListDefinitions

ListDefinitions returns a GraphQLObjectType for a list with the given name, and whose items are of the specified type.

type IDFetcherFn

type IDFetcherFn func(id string, info graphql.ResolveInfo, ctx context.Context) (interface{}, error)

IDFetcherFn returns the the object from an id

type ItemDefinitions

type ItemDefinitions struct {
	ItemInterface *graphql.Interface
	ItemField     *graphql.Field
}

ItemDefinitions is the GraphQL object type for an item list

func NewItemDefinitions

func NewItemDefinitions(config ItemDefinitionsConfig) *ItemDefinitions

NewItemDefinitions constructs a `Item` interface that objects can implement, and a field config for a `Item` root field, given a function to map from an ID to an underlying object, and a function to map from an underlying object to the concrete GraphQLObjectType it corresponds to. If the typeResolver is omitted, object resolution on the interface will be handled with the `isTypeOf` method on object types, as with any GraphQL interface without a provided `resolveType` method.

type ItemDefinitionsConfig

type ItemDefinitionsConfig struct {
	IDFetcher   IDFetcherFn
	TypeResolve graphql.ResolveTypeFn
}

ItemDefinitionsConfig is the configuration object for item list

type List

type List struct {
	Items      []interface{} `json:"items"`
	PageInfo   PageInfo      `json:"pageInfo"`
	TotalCount int           `json:"totalCount"`
}

List contains items with meta information about the content

func ListFromArray

func ListFromArray(data []interface{}, args ListArguments) *List

ListFromArray is a simple function that accepts an array and list arguments, and returns a list object for use in GraphQL. It uses array offsets as pagination, so pagination will only work if the array is static.

func ListFromArraySlice

func ListFromArraySlice(
	arraySlice []interface{},
	args ListArguments,
	meta ArraySliceMetaInfo,
) *List

ListFromArraySlice returns a list object for use in GraphQL, given a slice (subset) of an array. This function is similar to `ListFromArray`, but is intended for use cases where you know the cardinality of the list, consider it too large to materialize the entire array, and instead wish pass in a slice of the total result large enough to cover the range specified in `args`.

func NewList

func NewList() *List

NewList is a list constructor

type ListArguments

type ListArguments struct {
	Before ListCursor `json:"before"`
	After  ListCursor `json:"after"`
	First  int        `json:"first"` // -1 for undefined, 0 would return zero results
	Last   int        `json:"last"`  //  -1 for undefined, 0 would return zero results
}

ListArguments is pagination query arguments Use NewListArguments() to properly initialize default values

func NewListArguments

func NewListArguments(filters map[string]interface{}) ListArguments

NewListArguments is a list arguments constructor

type ListConfig

type ListConfig struct {
	Name       string          `json:"name"`
	ItemType   *graphql.Object `json:"itemType"`
	ListFields graphql.Fields  `json:"listFields"`
}

ListConfig is the configuration object for list

type ListCursor

type ListCursor string

ListCursor is an opaque index in a list

func CursorForObjectInList

func CursorForObjectInList(data []interface{}, object interface{}) ListCursor

CursorForObjectInList returns the cursor associated with an object in an array.

func OffsetToCursor

func OffsetToCursor(offset int) ListCursor

OffsetToCursor creates the cursor string from an offset

type MutationConfig

type MutationConfig struct {
	Name                string                            `json:"name"`
	InputFields         graphql.InputObjectConfigFieldMap `json:"inputFields"`
	OutputFields        graphql.Fields                    `json:"outputFields"`
	MutateAndGetPayload MutationFn                        `json:"mutateAndGetPayload"`
}

MutationConfig description below A description of a mutation consumable by mutationWithClientMutationId to create a GraphQLField for that mutation. The inputFields and outputFields should not include `clientMutationId`, as this will be provided automatically. An input object will be created containing the input fields, and an object will be created containing the output fields. mutateAndGetPayload will receive an Object with a key for each input field, and it should return an Object with a key for each output field. It may return synchronously, or return a Promise.

type MutationFn

type MutationFn func(inputMap map[string]interface{}, info graphql.ResolveInfo, ctx context.Context) (map[string]interface{}, error)

MutationFn mutates and returns the payload

type PageInfo

type PageInfo struct {
	StartCursor     ListCursor `json:"startCursor"`
	EndCursor       ListCursor `json:"endCursor"`
	HasPreviousPage bool       `json:"hasPreviousPage"`
	HasNextPage     bool       `json:"hasNextPage"`
}

PageInfo is the information about the current pagination of the list

type PluralIdentifyingRootFieldConfig

type PluralIdentifyingRootFieldConfig struct {
	ArgName            string               `json:"argName"`
	InputType          graphql.Input        `json:"inputType"`
	OutputType         graphql.Output       `json:"outputType"`
	ResolveSingleInput ResolveSingleInputFn `json:"resolveSingleInput"`
	Description        string               `json:"description"`
}

PluralIdentifyingRootFieldConfig is ...

type ResolveSingleInputFn

type ResolveSingleInputFn func(input interface{}) interface{}

ResolveSingleInputFn is ...

type ResolvedGlobalID

type ResolvedGlobalID struct {
	Type string `json:"type"`
	ID   string `json:"id"`
}

ResolvedGlobalID is the type and id of an object

func FromGlobalID

func FromGlobalID(globalID string) *ResolvedGlobalID

FromGlobalID takes the "global ID" created by toGlobalID, and returns the type name and ID used to create it.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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