generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
params.go
42 lines (34 loc) · 1020 Bytes
/
params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package datastore
import (
"encoding/json"
"github.com/99designs/gqlgen/graphql"
)
// QueryParams object to use when limiting and sorting database query results
type QueryParams struct {
Page int `json:"page,omitempty"`
PageSize int `json:"page_size,omitempty"`
OrderByField string `json:"order_by_field,omitempty"`
SortDirection string `json:"sort_direction,omitempty"`
}
// MarshalQueryParams will marshal the custom type
func MarshalQueryParams(m QueryParams) graphql.Marshaler {
if m.Page == 0 && m.PageSize == 0 && m.OrderByField == "" && m.SortDirection == "" {
return graphql.Null
}
return graphql.MarshalAny(m)
}
// UnmarshalQueryParams will unmarshal the custom type
func UnmarshalQueryParams(v interface{}) (QueryParams, error) {
if v == nil {
return QueryParams{}, nil
}
data, err := json.Marshal(v)
if err != nil {
return QueryParams{}, err
}
var q QueryParams
if err = json.Unmarshal(data, &q); err != nil {
return QueryParams{}, err
}
return q, nil
}