Skip to content

Instantly share code, notes, and snippets.

View kabece's full-sized avatar

Chris Czurylo kabece

  • Futureverse
  • Auckland
View GitHub Profile
func (r *queryResolver) ChatRoom(ctx context.Context, id string) (*model.ChatRoom, error) {
if t, ok := r.ChatRooms[id]; ok {
return &t, nil
}
return nil, errors.New("chat room not found")
}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(graph.NewResolver()))
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"))
}
package model
type ChatRoom struct {
ID string
Name string
}
package model
type ChatRoom struct {
ID string `json:"id"`
MessagesConnection *MessagesConnection `json:"messagesConnection"`
}
type Message struct {
ID string `json:"id"`
Text *string `json:"text"`
type Message {
id: ID!
text: String
}
type ChatRoom {
id: ID!
name: String
messagesConnection: # this is what we're going to implement
}
import { relayStylePagination } from '@apollo/client/utilities'
const client = new ApolloClient({
uri: 'http://localhost:4000/query',
cache: new InMemoryCache({
typePolicies: {
ChatRoom: {
fields: {
messagesConnection: relayStylePagination(),
},
query {
chatRoom(id: 1) {
id
name
messagesConnection(first: 10) {
edges {
node {
id
text
}
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
}
package graph
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/kabece/gqlgen-chatroom/graph/generated"
"github.com/kabece/gqlgen-chatroom/graph/model"