From d56245c5cf4f1d4942d9a468ecda16aea28d8854 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sun, 23 Jul 2023 20:27:37 -0600 Subject: [PATCH] Added ExtractList to extract list of type from ResolveParams --- gql_resolvers.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/gql_resolvers.go b/gql_resolvers.go index 4421ae6..9d4be7a 100644 --- a/gql_resolvers.go +++ b/gql_resolvers.go @@ -13,6 +13,7 @@ func PrepResolve(p graphql.ResolveParams) (*ResolveContext, error) { return resolve_context, nil } +// TODO: Make composabe by checkinf if K is a slice, then recursing in the same way that ExtractList does func ExtractParam[K interface{}](p graphql.ResolveParams, name string) (K, error) { var zero K arg_if, ok := p.Args[name] @@ -28,6 +29,26 @@ func ExtractParam[K interface{}](p graphql.ResolveParams, name string) (K, error return arg, nil } +func ExtractList[K interface{}](p graphql.ResolveParams, name string) ([]K, error) { + var zero K + + arg_list, err := ExtractParam[[]interface{}](p, name) + if err != nil { + return nil, err + } + + ret := make([]K, len(arg_list)) + for i, val := range(arg_list) { + val_conv, ok := arg_list[i].(K) + if ok == false { + return nil, fmt.Errorf("Failed to cast arg %s[%d](%+v) to %+v", name, i, val, reflect.TypeOf(zero)) + } + ret[i] = val_conv + } + + return ret, nil +} + func ExtractID(p graphql.ResolveParams, name string) (NodeID, error) { id_str, err := ExtractParam[string](p, name) if err != nil {