query

package
v0.0.0-...-0087722 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2020 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthQuery = graphql.Fields{
	"auth": &graphql.Field{
		Type: structql.GenerateType(oauth2.Token{}),
		Args: graphql.FieldConfigArgument{"code": &graphql.ArgumentConfig{Type: graphql.String}},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			code, ok := p.Args["code"].(string)
			if !ok {
				return oauth2.Token{}, errors.New("arg code not found")
			}
			token, err := tools.Auth().Exchange(code)
			if err != nil {
				log.Println(9999)
				return token, err
			}
			return token, nil
		},
	},
}
View Source
var CategoriesPaginatedQuery = graphql.Fields{
	"categoriesPaginated": &graphql.Field{
		Type: graphql.NewObject(graphql.ObjectConfig{
			Name: "CategoriesPaginated",
			Fields: graphql.Fields{
				"cursor": &graphql.Field{Type: graphql.Int},
				"total":  &graphql.Field{Type: graphql.Int},
				"categories": &graphql.Field{Type: graphql.NewList(
					structql.GenerateType(spotify.Category{
						Icons: []spotify.Image{{}},
					}))},
			},
		}),
		Args: graphql.FieldConfigArgument{
			"cursor": &graphql.ArgumentConfig{Type: graphql.Int},
			"pace":   &graphql.ArgumentConfig{Type: graphql.Int},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyPublicClient()
			if err != nil {
				return MySongsPaginated{}, err
			}
			pace, ok := p.Args["pace"].(int)
			if !ok {
				return MySongsPaginated{}, errors.New("arg pace not found")
			}
			cursor, ok := p.Args["cursor"].(int)
			if !ok {
				return "", errors.New("arg state not found")
			}
			localTracks, err := client.GetCategoriesOpt(&spotify.Options{
				Limit:  &pace,
				Offset: &cursor,
			}, "pt")
			if err != nil {
				return localTracks, err
			}
			return CategoriesPaginated{
				Total:      localTracks.Total,
				Cursor:     cursor + len(localTracks.Categories),
				Categories: localTracks.Categories,
			}, nil
		},
	},
}
View Source
var FollowPlaylist = graphql.Fields{
	"followPlaylist": &graphql.Field{
		Type: graphql.Boolean,
		Args: graphql.FieldConfigArgument{
			"playlistId": &graphql.ArgumentConfig{Type: graphql.String},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			playlistId, ok := p.Args["playlistId"].(string)
			if !ok {
				return PlaylistInfo{}, errors.New("arg playlistId not found")
			}
			client, err := tools.SpotifyClientFromContext(p.Context)
			if err != nil {
				return false, nil
			}
			playlists, err := client.CurrentUsersPlaylists()
			if err != nil {
				return false, nil
			}
			for _, playlist := range playlists.Playlists {
				if playlist.ID == spotify.ID(playlistId) {
					return true, nil
				}
			}
			return false, nil
		},
	},
}
View Source
var LikedSongs = graphql.Fields{
	"likedSongs": &graphql.Field{
		Type: graphql.NewList(graphql.String),
		Args: graphql.FieldConfigArgument{
			"ids": &graphql.ArgumentConfig{Type: graphql.NewList(graphql.String)},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			ids := []string{}
			idsReq := p.Args["ids"].([]interface{})
			for _, v := range idsReq {
				elems := v.(string)
				ids = append(ids, elems)
			}
			log.Println(len(ids))
			client, err := tools.SpotifyClientFromContext(p.Context)
			if err != nil {
				return ids, err
			}
			all, err := client.CurrentUsersTracks()
			if err != nil {
				return ids, err
			}
			log.Println(len(all.Tracks))
			result := []string{}
			for _, track := range all.Tracks {
				if tools.Contains(ids, track.ID.String()) {
					result = append(result, track.ID.String())
				}

			}
			log.Println(len(result))
			return result, nil
		},
	},
}
View Source
var LoggedQuery = graphql.Fields{
	"isLogged": &graphql.Field{
		Type: graphql.Boolean,
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyClientFromContext(p.Context)
			if err != nil {
				return false, nil
			}
			_, err = client.CurrentUsersTracks()
			if err != nil {
				return false, nil
			}
			return true, nil
		},
	},
}
View Source
var LoginQuery = graphql.Fields{
	"login": &graphql.Field{
		Type: graphql.String,
		Args: graphql.FieldConfigArgument{"state": &graphql.ArgumentConfig{Type: graphql.String}},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			state, ok := p.Args["state"].(string)
			if !ok {
				return "", errors.New("arg state not found")
			}
			return tools.Auth().AuthURL(state), nil
		},
	},
}
View Source
var MyDevicesQuery = graphql.Fields{
	"myDevices": &graphql.Field{
		Type: graphql.NewList(structql.GenerateType(spotify.PlayerDevice{})),
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyClientFromContext(p.Context)
			if err != nil {
				return []spotify.PlayerDevice{}, err
			}
			devices, err := client.PlayerDevices()
			if err != nil {
				return []spotify.PlayerDevice{}, err
			}
			return devices, nil
		},
	},
}
View Source
var MySongsPaginatedQL = graphql.NewObject(graphql.ObjectConfig{
	Name: "MySongsPaginated",
	Fields: graphql.Fields{
		"cursor": &graphql.Field{Type: graphql.Int},
		"total":  &graphql.Field{Type: graphql.Int},
		"songs":  &graphql.Field{Type: graphql.NewList(types.SavedTrack)},
	},
})
View Source
var MySongsPaginatedQuery = graphql.Fields{
	"mySongsPaginated": &graphql.Field{
		Type: MySongsPaginatedQL,
		Args: graphql.FieldConfigArgument{
			"cursor": &graphql.ArgumentConfig{Type: graphql.Int},
			"pace":   &graphql.ArgumentConfig{Type: graphql.Int},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			var paceArgNotFound = errors.New("arg pace not found")
			var cursorArgNotFound = errors.New("arg state not found")
			client, err := tools.SpotifyClientFromContext(p.Context)
			if err != nil {
				return MySongsPaginated{}, err
			}
			pace, ok := p.Args["pace"].(int)
			if !ok {
				return MySongsPaginated{}, paceArgNotFound
			}
			cursor, ok := p.Args["cursor"].(int)
			if !ok {
				return MySongsPaginated{}, cursorArgNotFound
			}
			localTracks, err := client.CurrentUsersTracksOpt(&spotify.Options{
				Offset: &cursor,
				Limit:  &pace,
			})
			if err != nil {
				return MySongsPaginated{}, err
			}
			return MySongsPaginated{
				Total:  localTracks.Total,
				Cursor: cursor + len(localTracks.Tracks),
				Songs:  localTracks.Tracks,
			}, nil
		},
	},
}
View Source
var NowPlayingQuery = graphql.Fields{
	"nowPlaying": &graphql.Field{
		Type: structql.GenerateType(spotify.PlayerState{
			CurrentlyPlaying: spotify.CurrentlyPlaying{
				Item: &spotify.FullTrack{
					SimpleTrack: spotify.SimpleTrack{
						Artists: []spotify.SimpleArtist{{}},
					},
					Album: spotify.SimpleAlbum{
						AvailableMarkets: []string{""},
						Images:           []spotify.Image{{}},
					},
				},
			},
		}),
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyClientFromContext(p.Context)
			if err != nil {
				return spotify.PlayerState{}, err
			}
			CurrentlyPlaying, err := client.PlayerState()
			if err != nil {
				log.Println(err.Error())
				return EmptyResponseMeansNotListening(errors.New("204"))
			}
			return CurrentlyPlaying, nil
		},
	},
}
View Source
var PlaylistInfoQuery = graphql.Fields{
	"playlistInfo": &graphql.Field{
		Type: graphql.NewObject(graphql.ObjectConfig{
			Name: "PlaylistInfo",
			Fields: graphql.Fields{
				"id":          &graphql.Field{Type: graphql.String},
				"name":        &graphql.Field{Type: graphql.String},
				"description": &graphql.Field{Type: graphql.String},
				"images":      &graphql.Field{Type: graphql.NewList(structql.GenerateType(spotify.Image{}))},
			},
		}),
		Args: graphql.FieldConfigArgument{
			"playID": &graphql.ArgumentConfig{Type: graphql.String},
			"owner":  &graphql.ArgumentConfig{Type: graphql.String},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyPublicClient()
			if err != nil {
				return PlaylistInfo{}, err
			}
			owner, ok := p.Args["owner"].(string)
			if !ok {
				return PlaylistInfo{}, errors.New("arg owner not found")
			}
			playID, ok := p.Args["playID"].(string)
			if !ok {
				return PlaylistInfo{}, errors.New("arg playID not found")
			}

			localTracks, err := client.GetPlaylistOpt(owner, spotify.ID(playID), "")
			if err != nil {
				return localTracks, err
			}
			return PlaylistInfo{
				ID:          localTracks.ID,
				Name:        localTracks.Name,
				Description: localTracks.Description,
				Images:      localTracks.Images,
			}, nil
		},
	},
}
View Source
var PlaylistSongsPaginatedQuery = graphql.Fields{
	"playlistSongsPaginated": &graphql.Field{
		Type: graphql.NewObject(graphql.ObjectConfig{
			Name: "PlaylistSongsPaginated",
			Fields: graphql.Fields{
				"cursor": &graphql.Field{Type: graphql.Int},
				"total":  &graphql.Field{Type: graphql.Int},
				"songs":  &graphql.Field{Type: graphql.NewList(types.SavedTrack)},
			},
		}),
		Args: graphql.FieldConfigArgument{
			"cursor": &graphql.ArgumentConfig{Type: graphql.Int},
			"pace":   &graphql.ArgumentConfig{Type: graphql.Int},
			"playID": &graphql.ArgumentConfig{Type: graphql.String},
			"owner":  &graphql.ArgumentConfig{Type: graphql.String},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyPublicClient()
			if err != nil {
				return MySongsPaginated{}, err
			}
			owner, ok := p.Args["owner"].(string)
			if !ok {
				return MySongsPaginated{}, errors.New("arg owner not found")
			}
			playID, ok := p.Args["playID"].(string)
			if !ok {
				return MySongsPaginated{}, errors.New("arg playID not found")
			}
			pace, ok := p.Args["pace"].(int)
			if !ok {
				return MySongsPaginated{}, errors.New("arg pace not found")
			}
			cursor, ok := p.Args["cursor"].(int)
			if !ok {
				return "", errors.New("arg state cursor not found")
			}
			localTracks, err := client.GetPlaylistTracksOpt(owner, spotify.ID(playID), &spotify.Options{
				Limit:  &pace,
				Offset: &cursor,
			}, "")
			if err != nil {
				return localTracks, err
			}
			return MySongsPaginated{
				Total:  localTracks.Total,
				Cursor: cursor + len(localTracks.Tracks),
				Songs:  mapSongsPlaylist(localTracks.Tracks),
			}, nil
		},
	},
}
View Source
var PlaylistsPaginatedQuery = graphql.Fields{
	"playlistsPaginated": &graphql.Field{
		Type: graphql.NewObject(graphql.ObjectConfig{
			Name: "PlaylistsPaginated",
			Fields: graphql.Fields{
				"cursor": &graphql.Field{Type: graphql.Int},
				"total":  &graphql.Field{Type: graphql.Int},
				"playlists": &graphql.Field{Type: graphql.NewList(
					structql.GenerateType(spotify.SimplePlaylist{
						Images: []spotify.Image{{}},
					}))},
			},
		}),
		Args: graphql.FieldConfigArgument{
			"cursor": &graphql.ArgumentConfig{Type: graphql.Int},
			"pace":   &graphql.ArgumentConfig{Type: graphql.Int},
			"catID":  &graphql.ArgumentConfig{Type: graphql.String},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyPublicClient()
			if err != nil {
				return MySongsPaginated{}, err
			}
			catID, ok := p.Args["catID"].(string)
			if !ok {
				return MySongsPaginated{}, errors.New("arg catID not found")
			}
			pace, ok := p.Args["pace"].(int)
			if !ok {
				return MySongsPaginated{}, errors.New("arg pace not found")
			}
			cursor, ok := p.Args["cursor"].(int)
			if !ok {
				return "", errors.New("arg state not found")
			}
			localTracks, err := client.GetCategoryPlaylistsOpt(catID, &spotify.Options{
				Limit:  &pace,
				Offset: &cursor,
			})
			if err != nil {
				return localTracks, err
			}
			return PlaylistsPaginated{
				Total:     localTracks.Total,
				Cursor:    cursor + len(localTracks.Playlists),
				Playlists: localTracks.Playlists,
			}, nil
		},
	},
}
View Source
var ProfileQuery = graphql.Fields{
	"profile": &graphql.Field{
		Type: graphql.NewObject(graphql.ObjectConfig{
			Name: "Profile",
			Fields: graphql.Fields{
				"name":   &graphql.Field{Type: graphql.String},
				"email":  &graphql.Field{Type: graphql.String},
				"images": &graphql.Field{Type: graphql.NewList(structql.GenerateType(spotify.Image{}))},
			},
		}),
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyClientFromContext(p.Context)
			if err != nil {
				return Profile{}, err
			}
			user, err := client.CurrentUser()
			if err != nil {
				return Profile{}, err
			}
			return Profile{
				Name:   user.DisplayName,
				Email:  user.Email,
				Images: user.Images,
			}, nil
		},
	},
}
View Source
var PublicSongsPaginatedQuery = graphql.Fields{
	"publicSongsPaginated": &graphql.Field{
		Type: MySongsPaginatedQL,
		Args: graphql.FieldConfigArgument{
			"query":  &graphql.ArgumentConfig{Type: graphql.String},
			"cursor": &graphql.ArgumentConfig{Type: graphql.Int},
			"pace":   &graphql.ArgumentConfig{Type: graphql.Int},
		},
		Resolve: func(p graphql.ResolveParams) (interface{}, error) {
			client, err := tools.SpotifyPublicClient()
			if err != nil {
				return MySongsPaginated{}, err
			}
			query, ok := p.Args["query"].(string)
			if !ok {
				return MySongsPaginated{}, errors.New("arg query not found")
			}
			pace, ok := p.Args["pace"].(int)
			if !ok {
				return MySongsPaginated{}, errors.New("arg pace not found")
			}
			cursor, ok := p.Args["cursor"].(int)
			if !ok {
				return "", errors.New("arg state not found")
			}
			localTracks, err := client.SearchOpt(query, spotify.SearchTypeTrack, &spotify.Options{
				Limit:  &pace,
				Offset: &cursor,
			})
			if err != nil {
				return localTracks, err
			}
			return MySongsPaginated{
				Total:  localTracks.Tracks.Total,
				Cursor: cursor + len(localTracks.Tracks.Tracks),
				Songs:  mapSongs(localTracks.Tracks.Tracks),
			}, nil
		},
	},
}

Functions

func EmptyResponseMeansNotListening

func EmptyResponseMeansNotListening(err error) (interface{}, error)

Types

type CategoriesPaginated

type CategoriesPaginated struct {
	Cursor     int                `json:"cursor"`
	Total      int                `json:"total"`
	Categories []spotify.Category `json:"categories"`
}

type MySongsPaginated

type MySongsPaginated struct {
	Cursor int                  `json:"cursor"`
	Total  int                  `json:"total"`
	Songs  []spotify.SavedTrack `json:"songs"`
}

type PlaylistInfo

type PlaylistInfo struct {
	Name        string          `json:"name"`
	Description string          `json:"description"`
	Images      []spotify.Image `json:"images"`
	ID          spotify.ID      `json:"id"`
}

type PlaylistsPaginated

type PlaylistsPaginated struct {
	Cursor    int                      `json:"cursor"`
	Total     int                      `json:"total"`
	Playlists []spotify.SimplePlaylist `json:"playlists"`
}

type Profile

type Profile struct {
	Name   string          `json:"name"`
	Email  string          `json:"email"`
	Images []spotify.Image `json:"images"`
}

type SavedTrack

type SavedTrack struct {
	Name string `json:"name"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL