Skip to content

Commit

Permalink
unmarshal inside Query
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Norton committed Dec 27, 2023
1 parent 2cf3bc5 commit 5ec8247
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions policy/data/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func NewChainDataSource(links ...DataSource) *ChainDataSource {
}
}

func (ds ChainDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}) (*QueryResponse, error) {
func (ds ChainDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}, output interface{}) (*QueryResponse, error) {
for _, l := range ds.links {
res, err := l.Query(ctx, queryName, query, variables)
res, err := l.Query(ctx, queryName, query, variables, output)
if res != nil || err != nil {
return res, err
}
Expand Down
19 changes: 14 additions & 5 deletions policy/data/fixed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@ import (
"context"
)

type FixedDataSourceUnmarshaler func(data []byte, output interface{}) error

// FixedDataSource returns static data from responses passed in at construction time
type FixedDataSource struct {
data map[string][]byte
unmarshaler FixedDataSourceUnmarshaler
data map[string][]byte
}

func NewFixedDataSource(data map[string][]byte) FixedDataSource {
func NewFixedDataSource(unmarshaler FixedDataSourceUnmarshaler, data map[string][]byte) FixedDataSource {
return FixedDataSource{
data: data,
unmarshaler: unmarshaler,
data: data,
}
}

func (ds FixedDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}) (*QueryResponse, error) {
func (ds FixedDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}, output interface{}) (*QueryResponse, error) {
res, ok := ds.data[queryName]
if !ok {
return nil, nil
}

return &QueryResponse{Data: res}, nil
err := ds.unmarshaler(res, output)
if err != nil {
return nil, err
}

return &QueryResponse{}, nil
}
14 changes: 7 additions & 7 deletions policy/data/gql_async.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewAsyncDataSource(req skill.RequestContext, metadata string) AsyncDataSour
}
}

func (ds AsyncDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}) (*QueryResponse, error) {
func (ds AsyncDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}, output interface{}) (QueryResponse, error) {
ednVariables := map[edn.Keyword]interface{}{}
for k, v := range variables {
ednVariables[edn.Keyword(k)] = v
Expand All @@ -65,14 +65,14 @@ func (ds AsyncDataSource) Query(ctx context.Context, queryName string, query str

edn, err := edn.Marshal(request)
if err != nil {
return nil, err
return QueryResponse{}, err
}

ds.log.Infof("Async request: %s", string(edn))

req, err := http.NewRequest(http.MethodPost, ds.url, bytes.NewBuffer(edn))
if err != nil {
return nil, err
return QueryResponse{}, err
}

req.Header.Add("Content-Type", "application/edn")
Expand All @@ -82,17 +82,17 @@ func (ds AsyncDataSource) Query(ctx context.Context, queryName string, query str

r, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
return QueryResponse{}, err
}
if r.StatusCode >= 400 {
buf := new(strings.Builder)
_, _ = io.Copy(buf, r.Body)
body := buf.String()

return nil, fmt.Errorf("async request returned unexpected status %s: %s", r.Status, body)
return QueryResponse{}, fmt.Errorf("async request returned unexpected status %s: %s", r.Status, body)
}

return &QueryResponse{AsyncRequestMade: true}, nil
return QueryResponse{AsyncRequestMade: true}, nil
}

func UnwrapAsyncResponse(result map[edn.Keyword]edn.RawMessage) (DataSource, error) {
Expand All @@ -116,5 +116,5 @@ func UnwrapAsyncResponse(result map[edn.Keyword]edn.RawMessage) (DataSource, err
queryResponses[string(k)] = v
}

return NewFixedDataSource(queryResponses), nil
return NewFixedDataSource(edn.Unmarshal, queryResponses), nil
}
11 changes: 9 additions & 2 deletions policy/data/gql_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package data

import (
"context"
"encoding/json"
"net/http"

"golang.org/x/oauth2"
Expand Down Expand Up @@ -32,7 +33,7 @@ func NewSyncGraphqlDataSource(ctx context.Context, req skill.RequestContext) (Sy
}, nil
}

func (ds SyncGraphqlDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}) (*QueryResponse, error) {
func (ds SyncGraphqlDataSource) Query(ctx context.Context, queryName string, query string, variables map[string]interface{}, output interface{}) (*QueryResponse, error) {
log := ds.RequestContext.Log

log.Infof("Graphql endpoint: %s", ds.RequestContext.Event.Urls.Graphql)
Expand All @@ -45,5 +46,11 @@ func (ds SyncGraphqlDataSource) Query(ctx context.Context, queryName string, que
}

log.Infof("GraphQL query response: %s", string(res))
return &QueryResponse{Data: res}, nil

err = json.Unmarshal(res, output)
if err != nil {
return nil, err
}

return &QueryResponse{}, nil
}
3 changes: 1 addition & 2 deletions policy/data/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
)

type QueryResponse struct {
Data []byte
AsyncRequestMade bool
}

type DataSource interface {
Query(ctx context.Context, queryName string, query string, variables map[string]interface{}) (*QueryResponse, error)
Query(ctx context.Context, queryName string, query string, variables map[string]interface{}, output interface{}) (*QueryResponse, error)
}

func GqlContext(req skill.RequestContext) map[string]interface{} {
Expand Down

0 comments on commit 5ec8247

Please sign in to comment.