sorare/graphql/params.go

68 lines
1.2 KiB
Go

package graphql
import (
"reflect"
gql "github.com/llehouerou/go-graphql-client"
)
type EmptyParams struct {
}
type SlugParams struct {
Slug string `graphql:"slug"`
}
type SlugsParams struct {
Slugs []string `graphql:"slugs"`
}
type IdStringParams struct {
Id string `graphql:"id"`
}
type IdStringsParams struct {
Ids []string `graphql:"ids"`
}
type IdParams struct {
Id gql.ID `graphql:"id"`
}
type IdsParams struct {
Ids []gql.ID `graphql:"ids"`
}
type AssetIdParams struct {
AssetId string `graphql:"assetId"`
}
type AssetIdsParams struct {
AssetIds []string `graphql:"assetIds"`
}
type BlockchainIdParams struct {
BlockchainId string `graphql:"blockchainId"`
}
func convertParamsToMap[Params any](params Params) map[string]interface{} {
v := reflect.ValueOf(params)
paramsMap := make(map[string]interface{})
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
value := v.Field(i)
key := field.Tag.Get("graphql")
if key == "" {
key = field.Name
}
if value.Kind() == reflect.Slice {
if value.Len() > 0 {
paramsMap[key] = value.Interface()
}
} else if !value.IsZero() {
paramsMap[key] = value.Interface()
}
}
return paramsMap
}