Skip to content

Instantly share code, notes, and snippets.

@kabece
Last active October 4, 2021 07:46
Show Gist options
  • Select an option

  • Save kabece/93582d1fe49a80e4710c2bfc6b1ff2c3 to your computer and use it in GitHub Desktop.

Select an option

Save kabece/93582d1fe49a80e4710c2bfc6b1ff2c3 to your computer and use it in GitHub Desktop.
package graph
import (
"fmt"
"math/rand"
"strconv"
"time"
"github.com/kabece/gqlgen-chatroom/graph/generated"
"github.com/kabece/gqlgen-chatroom/graph/model"
)
type Resolver struct {
ChatRooms map[string]model.ChatRoom
Messages map[string][]model.Message
}
func NewResolver() generated.Config {
const nChatRooms = 20
const nMessagesPerChatRoom = 100
r := Resolver{}
r.ChatRooms = make(map[string]model.ChatRoom, nChatRooms)
r.Messages = make(map[string][]model.Message, nChatRooms)
rand.Seed(time.Now().UnixNano())
for i := 0; i < nChatRooms; i++ {
id := strconv.Itoa(i + 1)
mockChatRoom := model.ChatRoom{
ID: id,
Name: fmt.Sprintf("ChatRoom %d", i),
}
r.ChatRooms[id] = mockChatRoom
r.Messages[id] = make([]model.Message, nMessagesPerChatRoom)
// Generate messages for the ChatRoom
for k := 0; k < nMessagesPerChatRoom; k++ {
id := strconv.Itoa(k + 1)
text := fmt.Sprintf("Message %d", k)
mockMessage := model.Message{
ID: id,
Text: &text,
}
r.Messages[id][k] = mockMessage
}
}
return generated.Config{
Resolvers: &r,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment