Last active
October 4, 2021 07:46
-
-
Save kabece/93582d1fe49a80e4710c2bfc6b1ff2c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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