本文整理汇总了Golang中github.com/graphql-go/relay.ConnectionFromArray函数的典型用法代码示例。如果您正苦于以下问题:Golang ConnectionFromArray函数的具体用法?Golang ConnectionFromArray怎么用?Golang ConnectionFromArray使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ConnectionFromArray函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestConnectionFromArray_HandlesPagination_RespectsLastAndAfterAndBefore_TooFew
func TestConnectionFromArray_HandlesPagination_RespectsLastAndAfterAndBefore_TooFew(t *testing.T) {
filter := map[string]interface{}{
"last": 2,
"after": "YXJyYXljb25uZWN0aW9uOjA=",
"before": "YXJyYXljb25uZWN0aW9uOjQ=",
}
args := relay.NewConnectionArguments(filter)
expected := &relay.Connection{
Edges: []*relay.Edge{
&relay.Edge{
Node: "C",
Cursor: "YXJyYXljb25uZWN0aW9uOjI=",
},
&relay.Edge{
Node: "D",
Cursor: "YXJyYXljb25uZWN0aW9uOjM=",
},
},
PageInfo: relay.PageInfo{
StartCursor: "YXJyYXljb25uZWN0aW9uOjI=",
EndCursor: "YXJyYXljb25uZWN0aW9uOjM=",
HasPreviousPage: true,
HasNextPage: false,
},
}
result := relay.ConnectionFromArray(arrayConnectionTestLetters, args)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, connection result diff: %v", testutil.Diff(expected, result))
}
}
示例2: TestConnectionFromArray_HandlesCursorEdgeCases_ReturnsNoElementsIfFirstIsZero
func TestConnectionFromArray_HandlesCursorEdgeCases_ReturnsNoElementsIfFirstIsZero(t *testing.T) {
filter := map[string]interface{}{
"first": 0,
}
args := relay.NewConnectionArguments(filter)
expected := &relay.Connection{
Edges: []*relay.Edge{},
PageInfo: relay.PageInfo{},
}
result := relay.ConnectionFromArray(arrayConnectionTestLetters, args)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, connection result diff: %v", testutil.Diff(expected, result))
}
}
示例3: TestConnectionFromArray_HandlesCursorEdgeCases_ReturnsNoElementsIfCursorsCross
func TestConnectionFromArray_HandlesCursorEdgeCases_ReturnsNoElementsIfCursorsCross(t *testing.T) {
filter := map[string]interface{}{
"before": "YXJyYXljb25uZWN0aW9uOjI=", // ==> offset: int(2)
"after": "YXJyYXljb25uZWN0aW9uOjQ=", // ==> offset: int(4)
}
args := relay.NewConnectionArguments(filter)
expected := &relay.Connection{
Edges: []*relay.Edge{},
PageInfo: relay.PageInfo{},
}
result := relay.ConnectionFromArray(arrayConnectionTestLetters, args)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, connection result diff: %v", testutil.Diff(expected, result))
}
}
示例4: TestConnectionFromArray_HandlesBasicSlicing_RespectsAnOverlyLargeFirst
func TestConnectionFromArray_HandlesBasicSlicing_RespectsAnOverlyLargeFirst(t *testing.T) {
filter := map[string]interface{}{
"first": 10,
}
args := relay.NewConnectionArguments(filter)
expected := &relay.Connection{
Edges: []*relay.Edge{
&relay.Edge{
Node: "A",
Cursor: "YXJyYXljb25uZWN0aW9uOjA=",
},
&relay.Edge{
Node: "B",
Cursor: "YXJyYXljb25uZWN0aW9uOjE=",
},
&relay.Edge{
Node: "C",
Cursor: "YXJyYXljb25uZWN0aW9uOjI=",
},
&relay.Edge{
Node: "D",
Cursor: "YXJyYXljb25uZWN0aW9uOjM=",
},
&relay.Edge{
Node: "E",
Cursor: "YXJyYXljb25uZWN0aW9uOjQ=",
},
},
PageInfo: relay.PageInfo{
StartCursor: "YXJyYXljb25uZWN0aW9uOjA=",
EndCursor: "YXJyYXljb25uZWN0aW9uOjQ=",
HasPreviousPage: false,
HasNextPage: false,
},
}
result := relay.ConnectionFromArray(arrayConnectionTestLetters, args)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, connection result diff: %v", testutil.Diff(expected, result))
}
}
示例5: TestConnectionFromArray_HandlesCursorEdgeCases_ReturnsAllElementsIfCursorsAreOnTheOutside
func TestConnectionFromArray_HandlesCursorEdgeCases_ReturnsAllElementsIfCursorsAreOnTheOutside(t *testing.T) {
filter := map[string]interface{}{
"before": "YXJyYXljb25uZWN0aW9uOjYK", // ==> offset: int(6)
"after": "YXJyYXljb25uZWN0aW9uOi0xCg==", // ==> offset: int(-1)
}
args := relay.NewConnectionArguments(filter)
expected := &relay.Connection{
Edges: []*relay.Edge{
&relay.Edge{
Node: "A",
Cursor: "YXJyYXljb25uZWN0aW9uOjA=",
},
&relay.Edge{
Node: "B",
Cursor: "YXJyYXljb25uZWN0aW9uOjE=",
},
&relay.Edge{
Node: "C",
Cursor: "YXJyYXljb25uZWN0aW9uOjI=",
},
&relay.Edge{
Node: "D",
Cursor: "YXJyYXljb25uZWN0aW9uOjM=",
},
&relay.Edge{
Node: "E",
Cursor: "YXJyYXljb25uZWN0aW9uOjQ=",
},
},
PageInfo: relay.PageInfo{
StartCursor: "YXJyYXljb25uZWN0aW9uOjA=",
EndCursor: "YXJyYXljb25uZWN0aW9uOjQ=",
HasPreviousPage: false,
HasNextPage: false,
},
}
result := relay.ConnectionFromArray(arrayConnectionTestLetters, args)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, connection result diff: %v", testutil.Diff(expected, result))
}
}
示例6: TestConnectionFromArray_HandlesBasicSlicing_ReturnsAllElementsWithoutFilters
func TestConnectionFromArray_HandlesBasicSlicing_ReturnsAllElementsWithoutFilters(t *testing.T) {
args := relay.NewConnectionArguments(nil)
expected := &relay.Connection{
Edges: []*relay.Edge{
&relay.Edge{
Node: "A",
Cursor: "YXJyYXljb25uZWN0aW9uOjA=",
},
&relay.Edge{
Node: "B",
Cursor: "YXJyYXljb25uZWN0aW9uOjE=",
},
&relay.Edge{
Node: "C",
Cursor: "YXJyYXljb25uZWN0aW9uOjI=",
},
&relay.Edge{
Node: "D",
Cursor: "YXJyYXljb25uZWN0aW9uOjM=",
},
&relay.Edge{
Node: "E",
Cursor: "YXJyYXljb25uZWN0aW9uOjQ=",
},
},
PageInfo: relay.PageInfo{
StartCursor: "YXJyYXljb25uZWN0aW9uOjA=",
EndCursor: "YXJyYXljb25uZWN0aW9uOjQ=",
HasPreviousPage: false,
HasNextPage: false,
},
}
result := relay.ConnectionFromArray(arrayConnectionTestLetters, args)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, connection result diff: %v", testutil.Diff(expected, result))
}
}
示例7: TestConnectionFromArray_HandlesBasicSlicing_RespectsASmallerFirst
func TestConnectionFromArray_HandlesBasicSlicing_RespectsASmallerFirst(t *testing.T) {
// Create connection arguments from map[string]interface{},
// which you usually get from types.GQLParams.Args
filter := map[string]interface{}{
"first": 2,
}
args := relay.NewConnectionArguments(filter)
// Alternatively, you can create connection arg the following way.
// args := relay.NewConnectionArguments(filter)
// args.First = 2
expected := &relay.Connection{
Edges: []*relay.Edge{
&relay.Edge{
Node: "A",
Cursor: "YXJyYXljb25uZWN0aW9uOjA=",
},
&relay.Edge{
Node: "B",
Cursor: "YXJyYXljb25uZWN0aW9uOjE=",
},
},
PageInfo: relay.PageInfo{
StartCursor: "YXJyYXljb25uZWN0aW9uOjA=",
EndCursor: "YXJyYXljb25uZWN0aW9uOjE=",
HasPreviousPage: false,
HasNextPage: true,
},
}
result := relay.ConnectionFromArray(arrayConnectionTestLetters, args)
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, connection result diff: %v", testutil.Diff(expected, result))
}
}
示例8: init
func init() {
connectionTestUserType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
// re-define `friends` field later because `connectionTestUserType` has `connectionTestConnectionDef` has `connectionTestUserType` (cyclic-reference)
"friends": &graphql.Field{},
},
})
connectionTestConnectionDef = relay.ConnectionDefinitions(relay.ConnectionConfig{
Name: "Friend",
NodeType: connectionTestUserType,
EdgeFields: graphql.Fields{
"friendshipTime": &graphql.Field{
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return "Yesterday", nil
},
},
},
ConnectionFields: graphql.Fields{
"totalCount": &graphql.Field{
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return len(connectionTestAllUsers), nil
},
},
},
})
// define `friends` field here after getting connection definition
connectionTestUserType.AddFieldConfig("friends", &graphql.Field{
Type: connectionTestConnectionDef.ConnectionType,
Args: relay.ConnectionArgs,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
arg := relay.NewConnectionArguments(p.Args)
res := relay.ConnectionFromArray(connectionTestAllUsers, arg)
return res, nil
},
})
connectionTestQueryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"user": &graphql.Field{
Type: connectionTestUserType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return connectionTestAllUsers[0], nil
},
},
},
})
var err error
connectionTestSchema, err = graphql.NewSchema(graphql.SchemaConfig{
Query: connectionTestQueryType,
})
if err != nil {
panic(err)
}
}
示例9: init
//.........这里部分代码省略.........
* This implements the following type system shorthand:
* type Faction : Node {
* id: String!
* name: String
* ships: ShipConnection
* }
*/
factionType = graphql.NewObject(graphql.ObjectConfig{
Name: "Faction",
Description: "A faction in the Star Wars saga",
Fields: graphql.Fields{
"id": relay.GlobalIDField("Faction", nil),
"name": &graphql.Field{
Type: graphql.String,
Description: "The name of the faction.",
},
"ships": &graphql.Field{
Type: shipConnectionDefinition.ConnectionType,
Args: relay.ConnectionArgs,
Resolve: func(p graphql.ResolveParams) interface{} {
// convert args map[string]interface into ConnectionArguments
args := relay.NewConnectionArguments(p.Args)
// get ship objects from current faction
ships := []interface{}{}
if faction, ok := p.Source.(*Faction); ok {
for _, shipId := range faction.Ships {
ships = append(ships, GetShip(shipId))
}
}
// let relay library figure out the result, given
// - the list of ships for this faction
// - and the filter arguments (i.e. first, last, after, before)
return relay.ConnectionFromArray(ships, args)
},
},
},
Interfaces: []*graphql.Interface{
nodeDefinitions.NodeInterface,
},
})
/**
* This is the type that will be the root of our query, and the
* entry point into our schema.
*
* This implements the following type system shorthand:
* type Query {
* rebels: Faction
* empire: Faction
* node(id: String!): Node
* }
*/
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"rebels": &graphql.Field{
Type: factionType,
Resolve: func(p graphql.ResolveParams) interface{} {
return GetRebels()
},
},
"empire": &graphql.Field{
Type: factionType,
Resolve: func(p graphql.ResolveParams) interface{} {
return GetEmpire()
示例10: init
func init() {
/**
* We get the node interface and field from the Relay library.
*
* The first method defines the way we resolve an ID to its object.
* The second defines the way we resolve an object to its GraphQL type.
*/
nodeDefinitions = relay.NewNodeDefinitions(relay.NodeDefinitionsConfig{
IDFetcher: func(id string, info graphql.ResolveInfo, ct context.Context) (interface{}, error) {
resolvedID := relay.FromGlobalID(id)
if resolvedID.Type == "User" {
return GetUser(resolvedID.ID), nil
}
if resolvedID.Type == "Widget" {
return GetWidget(resolvedID.ID), nil
}
return nil, nil
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
switch value.(type) {
case *User:
return userType
case *Widget:
return widgetType
}
return nil
},
})
/**
* Define your own types here
*/
widgetType = graphql.NewObject(graphql.ObjectConfig{
Name: "Widget",
Description: "A shiny widget'",
Fields: graphql.Fields{
"id": relay.GlobalIDField("Widget", nil),
"name": &graphql.Field{
Description: "The name of the widget",
Type: graphql.String,
},
},
Interfaces: []*graphql.Interface{
nodeDefinitions.NodeInterface,
},
})
widgetConnection = relay.ConnectionDefinitions(relay.ConnectionConfig{
Name: "WidgetConnection",
NodeType: widgetType,
})
userType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Description: "A person who uses our app",
Fields: graphql.Fields{
"id": relay.GlobalIDField("User", nil),
"widgets": &graphql.Field{
Type: widgetConnection.ConnectionType,
Description: "A person's collection of widgets",
Args: relay.ConnectionArgs,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
args := relay.NewConnectionArguments(p.Args)
dataSlice := WidgetsToInterfaceSlice(GetWidgets()...)
return relay.ConnectionFromArray(dataSlice, args), nil
},
},
},
Interfaces: []*graphql.Interface{
nodeDefinitions.NodeInterface,
},
})
/**
* This is the type that will be the root of our query,
* and the entry point into our schema.
*/
queryType := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"node": nodeDefinitions.NodeField,
// Add you own root fields here
"viewer": &graphql.Field{
Type: userType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return GetViewer(), nil
},
},
},
})
/**
* This is the type that will be the root of our mutations,
* and the entry point into performing writes in our schema.
*/
// mutationType := graphql.NewObject(graphql.ObjectConfig{
// Name: "Mutation",
// Fields: graphql.Fields{
// // Add you own mutations here
//.........这里部分代码省略.........
示例11: init
func init() {
nodeDefinitions = relay.NewNodeDefinitions(relay.NodeDefinitionsConfig{
IDFetcher: func(id string, info graphql.ResolveInfo) interface{} {
resolvedID := relay.FromGlobalID(id)
if resolvedID.Type == "Todo" {
return GetTodo(resolvedID.ID)
}
if resolvedID.Type == "User" {
return GetUser(resolvedID.ID)
}
return nil
},
TypeResolve: func(value interface{}, info graphql.ResolveInfo) *graphql.Object {
switch value.(type) {
case *Todo:
return todoType
case *User:
return userType
default:
return userType
}
},
})
todoType = graphql.NewObject(graphql.ObjectConfig{
Name: "Todo",
Fields: graphql.Fields{
"id": relay.GlobalIDField("Todo", nil),
"text": &graphql.Field{
Type: graphql.String,
},
"complete": &graphql.Field{
Type: graphql.Boolean,
},
},
Interfaces: []*graphql.Interface{nodeDefinitions.NodeInterface},
})
todosConnection = relay.ConnectionDefinitions(relay.ConnectionConfig{
Name: "Todo",
NodeType: todoType,
})
userType = graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Fields: graphql.Fields{
"id": relay.GlobalIDField("User", nil),
"todos": &graphql.Field{
Type: todosConnection.ConnectionType,
Args: relay.NewConnectionArgs(graphql.FieldConfigArgument{
"status": &graphql.ArgumentConfig{
Type: graphql.String,
DefaultValue: "any",
},
}),
Resolve: func(p graphql.ResolveParams) interface{} {
status, _ := p.Args["status"].(string)
args := relay.NewConnectionArguments(p.Args)
todos := TodosToSliceInterface(GetTodos(status))
return relay.ConnectionFromArray(todos, args)
},
},
"totalCount": &graphql.Field{
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) interface{} {
return len(GetTodos("any"))
},
},
"completedCount": &graphql.Field{
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) interface{} {
return len(GetTodos("completed"))
},
},
},
Interfaces: []*graphql.Interface{nodeDefinitions.NodeInterface},
})
rootType := graphql.NewObject(graphql.ObjectConfig{
Name: "Root",
Fields: graphql.Fields{
"viewer": &graphql.Field{
Type: userType,
Resolve: func(p graphql.ResolveParams) interface{} {
return GetViewer()
},
},
"node": nodeDefinitions.NodeField,
},
})
addTodoMutation := relay.MutationWithClientMutationID(relay.MutationConfig{
Name: "AddTodo",
InputFields: graphql.InputObjectConfigFieldMap{
"text": &graphql.InputObjectFieldConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
OutputFields: graphql.Fields{
//.........这里部分代码省略.........
示例12: Start
func (api *SearchAPI) Start() error {
api.Type = graphql.NewObject(graphql.ObjectConfig{
Name: "Search",
Fields: graphql.Fields{
"id": relay.GlobalIDField("Search", func(obj interface{}, info graphql.ResolveInfo, ctx context.Context) (string, error) {
se, ok := obj.(*Search)
if !ok {
return "", fmt.Errorf("Search's GlobalIDField() was called with a non-Search")
}
cid, err := se.ClientID()
if err != nil {
return "", fmt.Errorf("Failed to create a ClientID for %v", se)
}
return cid, nil
}),
"createdAt": &graphql.Field{
Description: "When the search was first saved.",
Type: graphql.String,
},
"name": &graphql.Field{
Description: "The name to display for the search.",
Type: graphql.String,
},
// TODO#Perf: Consider storing the search results on the context or ResolveInfo to avoid computing them twice (numResults and results).
"numResults": &graphql.Field{
Description: "The total number of results that match the query",
Type: graphql.Int,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
span := trace.FromContext(p.Context).NewChild("trythings.searchAPI.numResults")
defer span.Finish()
se, ok := p.Source.(*Search)
if !ok {
return nil, errors.New("expected search source")
}
sp, err := api.SearchService.Space(p.Context, se)
if err != nil {
return nil, err
}
// TODO#Perf: Run a count query instead of fetching all of the matches.
ts, err := api.TaskService.Search(p.Context, sp, se.Query)
if err != nil {
return nil, err
}
return len(ts), nil
},
},
"query": &graphql.Field{
Description: "The query used to search for tasks.",
Type: graphql.String,
},
"results": &graphql.Field{
Description: "The tasks that match the query",
Type: api.TaskAPI.ConnectionType,
Args: relay.ConnectionArgs,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
span := trace.FromContext(p.Context).NewChild("trythings.searchAPI.results")
defer span.Finish()
se, ok := p.Source.(*Search)
if !ok {
return nil, errors.New("expected search source")
}
sp, err := api.SearchService.Space(p.Context, se)
if err != nil {
return nil, err
}
ts, err := api.TaskService.Search(p.Context, sp, se.Query)
if err != nil {
return nil, err
}
objs := []interface{}{}
for _, t := range ts {
objs = append(objs, *t)
}
// TODO#Performance: Run a limited query instead of filtering after the query.
args := relay.NewConnectionArguments(p.Args)
return relay.ConnectionFromArray(objs, args), nil
},
},
},
Interfaces: []*graphql.Interface{
api.NodeInterface,
},
})
return nil
}