Closed barrettj12 closed 1 year ago
This change probably necessitates a new data model too, since artists/albums are now their own entities, rather than something derived from songs.
This explains how to do custom resolvers for a type: https://gqlgen.com/getting-started/
Some more thoughts:
In the data model, it makes sense to store the "object type" fields by ID, rather than as the entire object. For example:
type Artist struct {
ID ArtistID `json:"id"`
Name string `json:"name"`
Albums []AlbumID `json:"albums"`
RelatedArtists []ArtistID `json:"relatedArtists"`
}
This necessitates separating the data model representation from the GraphQL API representation:
type Artist struct {
ID string `json:"id"`
Name string `json:"name"`
Albums []*Album `json:"albums"`
RelatedArtists []*Artist `json:"relatedArtists"`
}
The GraphQL resolvers are responsible for "resolving" an ID into the corresponding object.
To keep the DB interface small, perhaps we should just have a single method for each data type, which takes a struct of parameters that can be used to filter the returned objects. For example:
type ChordsDBv1 interface {
Artists(ArtistsFilters) []Artist
Albums(AlbumsFilters) []Album
Songs(SongsFilters) []Song
}
where ArtistFilters
contains all the possible ways we might want to filter artists, e.g.
type ArtistFilters struct {
id ArtistID
name string
album AlbumID
relatedTo ArtistID
song SongID
}
We now have a fully functional GraphQL API (for queries / GET requests only). Let's wire this up to the real server, then I think this is a good spot to merge.
Next logical steps are:
Create a v1 of the chords API using GraphQL.
The new API schema lives in
api.graphql
. The server-side code is generated using gqlgen.TODO
gqlgen/api.resolvers.go