Skip to content

Instantly share code, notes, and snippets.

View kabece's full-sized avatar

Chris Czurylo kabece

View GitHub Profile
@kabece
kabece / schema-full.graphql
Last active December 3, 2021 06:57
Implementing GraphQL pagination in gqlgen
type Message {
id: ID!
text: String
}
type MessagesConnection {
edges: [MessagesEdge!]!
pageInfo: PageInfo!
}
package graph
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/kabece/gqlgen-chatroom/graph/generated"
"github.com/kabece/gqlgen-chatroom/graph/model"
func (r *chatRoomResolver) MessagesConnection(ctx context.Context, obj *model.ChatRoom,
first *int, after *string) (*model.MessagesConnection, error) {
// The cursor is base64 encoded by convention, so we need to decode it first
var decodedCursor string
if after != nil {
b, err := base64.StdEncoding.DecodeString(*after)
if err != nil {
return nil, err
}
query {
chatRoom(id: 1) {
id
name
messagesConnection(first: 10) {
edges {
node {
id
text
}
import { relayStylePagination } from '@apollo/client/utilities'
const client = new ApolloClient({
uri: 'http://localhost:4000/query',
cache: new InMemoryCache({
typePolicies: {
ChatRoom: {
fields: {
messagesConnection: relayStylePagination(),
},
type Message {
id: ID!
text: String
}
type ChatRoom {
id: ID!
name: String
messagesConnection: # this is what we're going to implement
}
package model
type ChatRoom struct {
ID string `json:"id"`
MessagesConnection *MessagesConnection `json:"messagesConnection"`
}
type Message struct {
ID string `json:"id"`
Text *string `json:"text"`
package model
type ChatRoom struct {
ID string
Name string
}
func (r *chatRoomResolver) MessagesConnection(ctx context.Context,
obj *model.ChatRoom, first *int, after *string) (*model.MessagesConnection, error) {
panic(fmt.Errorf("not implemented"))
}
func (r *queryResolver) ChatRoom(ctx context.Context, id string) (*model.ChatRoom, error) {
panic(fmt.Errorf("not implemented"))
}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(graph.NewResolver()))