Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active September 7, 2021 17:35
Show Gist options
  • Save percybolmer/7bb5ca822a5b46a02d54e2e86ac5ad6f to your computer and use it in GitHub Desktop.
Save percybolmer/7bb5ca822a5b46a02d54e2e86ac5ad6f to your computer and use it in GitHub Desktop.
graphql
// InMemoryRepository is a storage for gophers that uses a map to store them
type InMemoryRepository struct {
// gophers is our super storage for gophers.
gophers []Gopher
sync.Mutex
}
// NewMemoryRepository initializes a memory with mock data
func NewMemoryRepository() *InMemoryRepository {
gophers := []Gopher{
{
ID: "1",
Name: "Original Gopher",
Hired: true,
Profession: "Logo",
}, {
ID: "2",
Name: "Jan",
Hired: true,
Profession: "The Janitor",
},
}
return &InMemoryRepository{
gophers: gophers,
}
}
// GetGophers returns all gophers
func (imr *InMemoryRepository) GetGophers() ([]Gopher, error) {
return imr.gophers, nil
}
// GetGopher will return a goper by its ID
func (imr *InMemoryRepository) GetGopher(id string) (Gopher, error) {
for _, gopher := range imr.gophers {
if gopher.ID == id {
return gopher, nil
}
}
return Gopher{}, errors.New("no such gopher exists")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment