-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerics-relay-connections.go
More file actions
88 lines (79 loc) · 1.76 KB
/
generics-relay-connections.go
File metadata and controls
88 lines (79 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package examples
// This example demonstrates how to use Go generics with gqlschemagen
// to create reusable Relay-style connection types.
// Edge represents a Relay connection edge with a generic node type
type Edge[T any] struct {
Node T `json:"node"`
Cursor string `json:"cursor"`
}
// Connection represents a Relay connection with generic edge type
type Connection[T any] struct {
Edges []*Edge[T] `json:"edges"`
PageInfo *PageInfo `json:"pageInfo"`
}
/**
* @gqlType
* Information about pagination in a connection
*/
type PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
HasPreviousPage bool `json:"hasPreviousPage"`
StartCursor string `json:"startCursor"`
EndCursor string `json:"endCursor"`
}
/**
* @gqlType
* A user in the system
*/
type User struct {
ID string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
/**
* @gqlType
* A blog post
*/
type Post struct {
ID string `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
}
/**
* @gqlType
* Paginated list of users with total count
*/
type UserConnection struct {
Connection[*User]
TotalCount int `gql:"type:Int!"`
}
/**
* @gqlType
* Paginated list of posts with metadata
*/
type PostConnection struct {
Connection[*Post]
TotalCount int `gql:"type:Int!"`
HasUnpublished bool `json:"hasUnpublished"`
}
// Generated GraphQL schema will include:
//
// type UserConnection {
// edges: [Edge!]!
// pageInfo: PageInfo!
// totalCount: Int!
// }
//
// type PostConnection {
// edges: [Edge!]!
// pageInfo: PageInfo!
// totalCount: Int!
// hasUnpublished: Boolean!
// }
//
// type PageInfo {
// hasNextPage: Boolean!
// hasPreviousPage: Boolean!
// startCursor: String!
// endCursor: String!
// }