forked from cerc-io/laconicd-deprecated
gql schema for ipld objects
This commit is contained in:
parent
98cc47a1a4
commit
98b7e41988
@ -1,12 +1,5 @@
|
||||
# Reference to another record.
|
||||
type Reference {
|
||||
id: String! # ID of linked record.
|
||||
}
|
||||
|
||||
# Reference to another record.
|
||||
input ReferenceInput {
|
||||
id: String!
|
||||
}
|
||||
scalar Link
|
||||
|
||||
# Bonds contain funds that are used to pay rent on record registration and renewal.
|
||||
type Bond {
|
||||
@ -37,44 +30,71 @@ type Account {
|
||||
balance: [Coin!] # Current balance for each coin type.
|
||||
}
|
||||
|
||||
# Value of a given type.
|
||||
type Value {
|
||||
null: Boolean
|
||||
# Value describes a DAG-JSON compatible value.
|
||||
union Value =
|
||||
BooleanValue
|
||||
| IntValue
|
||||
| FloatValue
|
||||
| StringValue
|
||||
| BytesValue
|
||||
| LinkValue
|
||||
| ArrayValue
|
||||
| MapValue
|
||||
|
||||
int: Int
|
||||
float: Float
|
||||
string: String
|
||||
boolean: Boolean
|
||||
json: String
|
||||
|
||||
reference: Reference
|
||||
|
||||
values: [Value]
|
||||
type BooleanValue {
|
||||
value: Boolean!
|
||||
}
|
||||
# Value of a given type used as input to queries.
|
||||
input ValueInput {
|
||||
null: Boolean
|
||||
|
||||
int: Int
|
||||
float: Float
|
||||
string: String
|
||||
boolean: Boolean
|
||||
type IntValue {
|
||||
value: Int!
|
||||
}
|
||||
|
||||
reference: ReferenceInput
|
||||
type FloatValue {
|
||||
value: Float!
|
||||
}
|
||||
|
||||
values: [ValueInput]
|
||||
type StringValue {
|
||||
value: String!
|
||||
}
|
||||
|
||||
type BytesValue {
|
||||
value: String!
|
||||
}
|
||||
|
||||
type ArrayValue {
|
||||
value: [Value]!
|
||||
}
|
||||
|
||||
type LinkValue {
|
||||
value: Link!
|
||||
}
|
||||
|
||||
type MapValue {
|
||||
value: [Attribute!]!
|
||||
}
|
||||
|
||||
# Key/value pair.
|
||||
type KeyValue {
|
||||
type Attribute {
|
||||
key: String!
|
||||
value: Value!
|
||||
value: Value
|
||||
}
|
||||
|
||||
# Value of a given type used as input to queries.
|
||||
# Note: GQL doesn't allow union input types.
|
||||
input ValueInput {
|
||||
int: Int
|
||||
float: Float
|
||||
string: String
|
||||
boolean: Boolean
|
||||
link: Link
|
||||
array: [ValueInput]
|
||||
map: [KeyValueInput!]
|
||||
}
|
||||
|
||||
# Key/value pair for inputs.
|
||||
input KeyValueInput {
|
||||
key: String!
|
||||
value: ValueInput!
|
||||
value: ValueInput
|
||||
}
|
||||
|
||||
# Status information about a node (https://docs.tendermint.com/master/rpc/#/Info/status).
|
||||
@ -155,7 +175,7 @@ type Record {
|
||||
createTime: String! # Record create time.
|
||||
expiryTime: String! # Record expiry time.
|
||||
owners: [String!] # Addresses of record owners.
|
||||
attributes: [KeyValue] # Record attributes.
|
||||
attributes: [Attribute!] # Record attributes.
|
||||
references: [Record] # Record references.
|
||||
}
|
||||
|
||||
@ -195,7 +215,7 @@ type Query {
|
||||
getBondsByIds(ids: [String!]): [Bond]
|
||||
|
||||
# Query bonds.
|
||||
queryBonds(attributes: [KeyValueInput]): [Bond]
|
||||
queryBonds(attributes: [KeyValueInput!]): [Bond]
|
||||
|
||||
# Query bonds by owner.
|
||||
queryBondsByOwner(ownerAddresses: [String!]): [OwnerBonds]
|
||||
@ -210,7 +230,7 @@ type Query {
|
||||
# Query records.
|
||||
queryRecords(
|
||||
# Multiple attribute conditions are in a logical AND.
|
||||
attributes: [KeyValueInput]
|
||||
attributes: [KeyValueInput!]
|
||||
|
||||
# Whether to query all records, not just named ones (false by default).
|
||||
all: Boolean
|
||||
|
9860
gql/generated.go
9860
gql/generated.go
File diff suppressed because it is too large
Load Diff
@ -10,3 +10,8 @@ model:
|
||||
resolver:
|
||||
filename: resolver.go
|
||||
type: Resolver
|
||||
|
||||
models:
|
||||
Link:
|
||||
model:
|
||||
- github.com/cerc-io/laconicd/gql.Link
|
||||
|
@ -2,6 +2,10 @@
|
||||
|
||||
package gql
|
||||
|
||||
type Value interface {
|
||||
IsValue()
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
Address string `json:"address"`
|
||||
PubKey *string `json:"pubKey"`
|
||||
@ -10,6 +14,17 @@ type Account struct {
|
||||
Balance []*Coin `json:"balance"`
|
||||
}
|
||||
|
||||
type ArrayValue struct {
|
||||
Value []Value `json:"value"`
|
||||
}
|
||||
|
||||
func (ArrayValue) IsValue() {}
|
||||
|
||||
type Attribute struct {
|
||||
Key string `json:"key"`
|
||||
Value Value `json:"value"`
|
||||
}
|
||||
|
||||
type Auction struct {
|
||||
ID string `json:"id"`
|
||||
Status string `json:"status"`
|
||||
@ -53,21 +68,52 @@ type Bond struct {
|
||||
Balance []*Coin `json:"balance"`
|
||||
}
|
||||
|
||||
type BooleanValue struct {
|
||||
Value bool `json:"value"`
|
||||
}
|
||||
|
||||
func (BooleanValue) IsValue() {}
|
||||
|
||||
type BytesValue struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func (BytesValue) IsValue() {}
|
||||
|
||||
type Coin struct {
|
||||
Type string `json:"type"`
|
||||
Quantity string `json:"quantity"`
|
||||
}
|
||||
|
||||
type KeyValue struct {
|
||||
Key string `json:"key"`
|
||||
Value *Value `json:"value"`
|
||||
type FloatValue struct {
|
||||
Value float64 `json:"value"`
|
||||
}
|
||||
|
||||
func (FloatValue) IsValue() {}
|
||||
|
||||
type IntValue struct {
|
||||
Value int `json:"value"`
|
||||
}
|
||||
|
||||
func (IntValue) IsValue() {}
|
||||
|
||||
type KeyValueInput struct {
|
||||
Key string `json:"key"`
|
||||
Value *ValueInput `json:"value"`
|
||||
}
|
||||
|
||||
type LinkValue struct {
|
||||
Value Link `json:"value"`
|
||||
}
|
||||
|
||||
func (LinkValue) IsValue() {}
|
||||
|
||||
type MapValue struct {
|
||||
Value []*Attribute `json:"value"`
|
||||
}
|
||||
|
||||
func (MapValue) IsValue() {}
|
||||
|
||||
type NameRecord struct {
|
||||
Latest *NameRecordEntry `json:"latest"`
|
||||
History []*NameRecordEntry `json:"history"`
|
||||
@ -102,18 +148,10 @@ type Record struct {
|
||||
CreateTime string `json:"createTime"`
|
||||
ExpiryTime string `json:"expiryTime"`
|
||||
Owners []string `json:"owners"`
|
||||
Attributes []*KeyValue `json:"attributes"`
|
||||
Attributes []*Attribute `json:"attributes"`
|
||||
References []*Record `json:"references"`
|
||||
}
|
||||
|
||||
type Reference struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type ReferenceInput struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type Status struct {
|
||||
Version string `json:"version"`
|
||||
Node *NodeInfo `json:"node"`
|
||||
@ -125,6 +163,12 @@ type Status struct {
|
||||
DiskUsage string `json:"disk_usage"`
|
||||
}
|
||||
|
||||
type StringValue struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
func (StringValue) IsValue() {}
|
||||
|
||||
type SyncInfo struct {
|
||||
LatestBlockHash string `json:"latest_block_hash"`
|
||||
LatestBlockHeight string `json:"latest_block_height"`
|
||||
@ -138,23 +182,12 @@ type ValidatorInfo struct {
|
||||
ProposerPriority *string `json:"proposer_priority"`
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
Null *bool `json:"null"`
|
||||
Int *int `json:"int"`
|
||||
Float *float64 `json:"float"`
|
||||
String *string `json:"string"`
|
||||
Boolean *bool `json:"boolean"`
|
||||
JSON *string `json:"json"`
|
||||
Reference *Reference `json:"reference"`
|
||||
Values []*Value `json:"values"`
|
||||
}
|
||||
|
||||
type ValueInput struct {
|
||||
Null *bool `json:"null"`
|
||||
Int *int `json:"int"`
|
||||
Float *float64 `json:"float"`
|
||||
String *string `json:"string"`
|
||||
Boolean *bool `json:"boolean"`
|
||||
Reference *ReferenceInput `json:"reference"`
|
||||
Values []*ValueInput `json:"values"`
|
||||
Link *Link `json:"link"`
|
||||
Array []*ValueInput `json:"array"`
|
||||
Map []*KeyValueInput `json:"map"`
|
||||
}
|
||||
|
33
gql/scalar.go
Normal file
33
gql/scalar.go
Normal file
@ -0,0 +1,33 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Represents an IPLD link. Links are generally but not necessarily implemented as CIDs
|
||||
type Link string
|
||||
|
||||
func (l Link) String() string {
|
||||
return string(l)
|
||||
}
|
||||
|
||||
// UnmarshalGQLContext implements the graphql.ContextUnmarshaler interface
|
||||
func (l *Link) UnmarshalGQLContext(_ context.Context, v interface{}) error {
|
||||
s, ok := v.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("Link must be a string")
|
||||
}
|
||||
*l = Link(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalGQLContext implements the graphql.ContextMarshaler interface
|
||||
func (l Link) MarshalGQLContext(_ context.Context, w io.Writer) error {
|
||||
encodable := map[string]string{
|
||||
"/": l.String(),
|
||||
}
|
||||
return json.NewEncoder(w).Encode(encodable)
|
||||
}
|
247
gql/util.go
247
gql/util.go
@ -2,15 +2,15 @@ package gql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect" // #nosec G702
|
||||
"fmt" // #nosec G702
|
||||
"strconv"
|
||||
|
||||
auctiontypes "github.com/cerc-io/laconicd/x/auction/types"
|
||||
bondtypes "github.com/cerc-io/laconicd/x/bond/types"
|
||||
registrytypes "github.com/cerc-io/laconicd/x/registry/types"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/ipld/go-ipld-prime"
|
||||
"github.com/ipld/go-ipld-prime/codec/dagjson"
|
||||
)
|
||||
|
||||
// OwnerAttributeName denotes the owner attribute name for a bond.
|
||||
@ -61,13 +61,21 @@ func getGQLRecord(ctx context.Context, resolver QueryResolver, record registryty
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
recordType := record.ToReadableRecord()
|
||||
attributes, err := getAttributes(&recordType)
|
||||
node, err := ipld.Decode(record.Attributes, dagjson.Decode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node.Kind() != ipld.Kind_Map {
|
||||
return nil, fmt.Errorf("invalid record attributes")
|
||||
}
|
||||
|
||||
var links []string
|
||||
attributes, err := resolveIPLDNode(node, &links)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
references, err := getReferences(ctx, resolver, &recordType)
|
||||
references, err := resolver.GetRecordsByIds(ctx, links)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -79,11 +87,96 @@ func getGQLRecord(ctx context.Context, resolver QueryResolver, record registryty
|
||||
ExpiryTime: record.GetExpiryTime(),
|
||||
Owners: record.GetOwners(),
|
||||
Names: record.GetNames(),
|
||||
Attributes: attributes,
|
||||
Attributes: attributes.(MapValue).Value,
|
||||
References: references,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func resolveIPLDNode(node ipld.Node, links *[]string) (Value, error) {
|
||||
switch node.Kind() {
|
||||
case ipld.Kind_Map:
|
||||
var entries []*Attribute
|
||||
for itr := node.MapIterator(); !itr.Done(); {
|
||||
k, v, err := itr.Next()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if k.Kind() != ipld.Kind_String {
|
||||
return nil, fmt.Errorf("invalid record attribute key type: %s", k.Kind())
|
||||
}
|
||||
s, err := k.AsString()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
val, err := resolveIPLDNode(v, links)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries = append(entries, &Attribute{
|
||||
Key: s,
|
||||
Value: val,
|
||||
})
|
||||
}
|
||||
return MapValue{entries}, nil
|
||||
case ipld.Kind_List:
|
||||
var values []Value
|
||||
for itr := node.ListIterator(); !itr.Done(); {
|
||||
_, v, err := itr.Next()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
val, err := resolveIPLDNode(v, links)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, val)
|
||||
}
|
||||
return ArrayValue{values}, nil
|
||||
case ipld.Kind_Null:
|
||||
return nil, nil
|
||||
case ipld.Kind_Bool:
|
||||
val, err := node.AsBool()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return BooleanValue{val}, nil
|
||||
case ipld.Kind_Int:
|
||||
val, err := node.AsInt()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// TODO: handle bigger ints
|
||||
return IntValue{int(val)}, nil
|
||||
case ipld.Kind_Float:
|
||||
val, err := node.AsFloat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return FloatValue{val}, nil
|
||||
case ipld.Kind_String:
|
||||
val, err := node.AsString()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return StringValue{val}, nil
|
||||
case ipld.Kind_Bytes:
|
||||
val, err := node.AsBytes()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return BytesValue{string(val)}, nil
|
||||
case ipld.Kind_Link:
|
||||
val, err := node.AsLink()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
*links = append(*links, val.String())
|
||||
return LinkValue{Link(val.String())}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid node kind")
|
||||
}
|
||||
}
|
||||
|
||||
func getGQLNameRecord(record *registrytypes.NameRecord) (*NameRecord, error) {
|
||||
if record == nil {
|
||||
return nil, fmt.Errorf("got nil record")
|
||||
@ -163,136 +256,64 @@ func GetGQLAuction(auction *auctiontypes.Auction, bids []*auctiontypes.Bid) (*Au
|
||||
return &gqlAuction, nil
|
||||
}
|
||||
|
||||
func getReferences(ctx context.Context, resolver QueryResolver, r *registrytypes.RecordEncodable) ([]*Record, error) {
|
||||
var ids []string
|
||||
func parseRequestValue(value *ValueInput) *registrytypes.QueryListRecordsRequest_ValueInput {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
var val registrytypes.QueryListRecordsRequest_ValueInput
|
||||
|
||||
// #nosec G705
|
||||
for key := range r.Attributes {
|
||||
//nolint: all
|
||||
switch r.Attributes[key].(type) {
|
||||
case interface{}:
|
||||
if obj, ok := r.Attributes[key].(map[string]interface{}); ok {
|
||||
if _, ok := obj["/"]; ok && len(obj) == 1 {
|
||||
if _, ok := obj["/"].(string); ok {
|
||||
ids = append(ids, obj["/"].(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if value.String != nil {
|
||||
val.String_ = *value.String
|
||||
val.Type = "string"
|
||||
}
|
||||
|
||||
return resolver.GetRecordsByIds(ctx, ids)
|
||||
}
|
||||
|
||||
func getAttributes(r *registrytypes.RecordEncodable) ([]*KeyValue, error) {
|
||||
return mapToKeyValuePairs(r.Attributes)
|
||||
}
|
||||
|
||||
func mapToKeyValuePairs(attrs map[string]interface{}) ([]*KeyValue, error) {
|
||||
kvPairs := []*KeyValue{}
|
||||
|
||||
trueVal := true
|
||||
falseVal := false
|
||||
|
||||
// #nosec G705
|
||||
for key, value := range attrs {
|
||||
kvPair := &KeyValue{
|
||||
Key: key,
|
||||
Value: &Value{},
|
||||
if value.Int != nil {
|
||||
val.Int = int64(*value.Int)
|
||||
val.Type = "int"
|
||||
}
|
||||
|
||||
switch val := value.(type) {
|
||||
case nil:
|
||||
kvPair.Value.Null = &trueVal
|
||||
case int:
|
||||
kvPair.Value.Int = &val
|
||||
case float64:
|
||||
kvPair.Value.Float = &val
|
||||
case string:
|
||||
kvPair.Value.String = &val
|
||||
case bool:
|
||||
kvPair.Value.Boolean = &val
|
||||
case interface{}:
|
||||
if obj, ok := value.(map[string]interface{}); ok {
|
||||
if _, ok := obj["/"]; ok && len(obj) == 1 {
|
||||
if _, ok := obj["/"].(string); ok {
|
||||
kvPair.Value.Reference = &Reference{
|
||||
ID: obj["/"].(string),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
bytes, err := json.Marshal(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if value.Float != nil {
|
||||
val.Float = *value.Float
|
||||
val.Type = "float"
|
||||
}
|
||||
|
||||
jsonStr := string(bytes)
|
||||
kvPair.Value.JSON = &jsonStr
|
||||
}
|
||||
}
|
||||
if value.Boolean != nil {
|
||||
val.Boolean = *value.Boolean
|
||||
val.Type = "boolean"
|
||||
}
|
||||
|
||||
if kvPair.Value.Null == nil {
|
||||
kvPair.Value.Null = &falseVal
|
||||
if value.Link != nil {
|
||||
reference := ®istrytypes.QueryListRecordsRequest_ReferenceInput{
|
||||
Id: value.Link.String(),
|
||||
}
|
||||
|
||||
valueType := reflect.ValueOf(value)
|
||||
if valueType.Kind() == reflect.Slice {
|
||||
bytes, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
val.Reference = reference
|
||||
val.Type = "reference"
|
||||
}
|
||||
|
||||
jsonStr := string(bytes)
|
||||
kvPair.Value.JSON = &jsonStr
|
||||
// handle arrays
|
||||
if value.Array != nil {
|
||||
values := []*registrytypes.QueryListRecordsRequest_ValueInput{}
|
||||
for _, v := range value.Array {
|
||||
val := parseRequestValue(v)
|
||||
values = append(values, val)
|
||||
}
|
||||
val.Values = values
|
||||
val.Type = "array"
|
||||
}
|
||||
|
||||
kvPairs = append(kvPairs, kvPair)
|
||||
}
|
||||
|
||||
return kvPairs, nil
|
||||
return &val
|
||||
}
|
||||
|
||||
func parseRequestAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput {
|
||||
kvPairs := []*registrytypes.QueryListRecordsRequest_KeyValueInput{}
|
||||
|
||||
for _, value := range attrs {
|
||||
parsedValue := parseRequestValue(value.Value)
|
||||
kvPair := ®istrytypes.QueryListRecordsRequest_KeyValueInput{
|
||||
Key: value.Key,
|
||||
Value: ®istrytypes.QueryListRecordsRequest_ValueInput{},
|
||||
Value: parsedValue,
|
||||
}
|
||||
|
||||
if value.Value.String != nil {
|
||||
kvPair.Value.String_ = *value.Value.String
|
||||
kvPair.Value.Type = "string"
|
||||
}
|
||||
|
||||
if value.Value.Int != nil {
|
||||
kvPair.Value.Int = int64(*value.Value.Int)
|
||||
kvPair.Value.Type = "int"
|
||||
}
|
||||
|
||||
if value.Value.Float != nil {
|
||||
kvPair.Value.Float = *value.Value.Float
|
||||
kvPair.Value.Type = "float"
|
||||
}
|
||||
|
||||
if value.Value.Boolean != nil {
|
||||
kvPair.Value.Boolean = *value.Value.Boolean
|
||||
kvPair.Value.Type = "boolean"
|
||||
}
|
||||
|
||||
if value.Value.Reference != nil {
|
||||
reference := ®istrytypes.QueryListRecordsRequest_ReferenceInput{
|
||||
Id: value.Value.Reference.ID,
|
||||
}
|
||||
|
||||
kvPair.Value.Reference = reference
|
||||
kvPair.Value.Type = "reference"
|
||||
}
|
||||
|
||||
// TODO: Handle arrays.
|
||||
|
||||
kvPairs = append(kvPairs, kvPair)
|
||||
}
|
||||
|
||||
|
@ -362,8 +362,6 @@ func (k Keeper) SetAttributeMapping(ctx sdk.Context, key []byte, recordID string
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot unmarshal byte array, error, %w", err)
|
||||
}
|
||||
} else {
|
||||
recordIds = []string{}
|
||||
}
|
||||
recordIds = append(recordIds, recordID)
|
||||
bz, err := json.Marshal(recordIds)
|
||||
|
Loading…
Reference in New Issue
Block a user