gql schema for ipld objects

This commit is contained in:
Roy Crihfield 2023-09-21 20:56:15 +08:00
parent 13ca800ebc
commit cdbb267520
7 changed files with 6381 additions and 3985 deletions

View File

@ -1,12 +1,5 @@
# Reference to another record. # Reference to another record.
type Reference { scalar Link
id: String! # ID of linked record.
}
# Reference to another record.
input ReferenceInput {
id: String!
}
# Bonds contain funds that are used to pay rent on record registration and renewal. # Bonds contain funds that are used to pay rent on record registration and renewal.
type Bond { type Bond {
@ -37,44 +30,71 @@ type Account {
balance: [Coin!] # Current balance for each coin type. balance: [Coin!] # Current balance for each coin type.
} }
# Value of a given type. # Value describes a DAG-JSON compatible value.
type Value { union Value =
null: Boolean BooleanValue
| IntValue
| FloatValue
| StringValue
| BytesValue
| LinkValue
| ArrayValue
| MapValue
int: Int type BooleanValue {
float: Float value: Boolean!
string: String
boolean: Boolean
json: String
reference: Reference
values: [Value]
} }
# Value of a given type used as input to queries.
input ValueInput {
null: Boolean
int: Int type IntValue {
float: Float value: Int!
string: String }
boolean: Boolean
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. # Key/value pair.
type KeyValue { type Attribute {
key: String! 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. # Key/value pair for inputs.
input KeyValueInput { input KeyValueInput {
key: String! key: String!
value: ValueInput! value: ValueInput
} }
# Status information about a node (https://docs.tendermint.com/master/rpc/#/Info/status). # Status information about a node (https://docs.tendermint.com/master/rpc/#/Info/status).
@ -155,7 +175,7 @@ type Record {
createTime: String! # Record create time. createTime: String! # Record create time.
expiryTime: String! # Record expiry time. expiryTime: String! # Record expiry time.
owners: [String!] # Addresses of record owners. owners: [String!] # Addresses of record owners.
attributes: [KeyValue] # Record attributes. attributes: [Attribute!] # Record attributes.
references: [Record] # Record references. references: [Record] # Record references.
} }
@ -195,7 +215,7 @@ type Query {
getBondsByIds(ids: [String!]): [Bond] getBondsByIds(ids: [String!]): [Bond]
# Query bonds. # Query bonds.
queryBonds(attributes: [KeyValueInput]): [Bond] queryBonds(attributes: [KeyValueInput!]): [Bond]
# Query bonds by owner. # Query bonds by owner.
queryBondsByOwner(ownerAddresses: [String!]): [OwnerBonds] queryBondsByOwner(ownerAddresses: [String!]): [OwnerBonds]
@ -210,7 +230,7 @@ type Query {
# Query records. # Query records.
queryRecords( queryRecords(
# Multiple attribute conditions are in a logical AND. # Multiple attribute conditions are in a logical AND.
attributes: [KeyValueInput] attributes: [KeyValueInput!]
# Whether to query all records, not just named ones (false by default). # Whether to query all records, not just named ones (false by default).
all: Boolean all: Boolean

File diff suppressed because it is too large Load Diff

View File

@ -10,3 +10,8 @@ model:
resolver: resolver:
filename: resolver.go filename: resolver.go
type: Resolver type: Resolver
models:
Link:
model:
- github.com/cerc-io/laconicd/gql.Link

View File

@ -2,6 +2,10 @@
package gql package gql
type Value interface {
IsValue()
}
type Account struct { type Account struct {
Address string `json:"address"` Address string `json:"address"`
PubKey *string `json:"pubKey"` PubKey *string `json:"pubKey"`
@ -10,6 +14,17 @@ type Account struct {
Balance []*Coin `json:"balance"` 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 { type Auction struct {
ID string `json:"id"` ID string `json:"id"`
Status string `json:"status"` Status string `json:"status"`
@ -53,21 +68,52 @@ type Bond struct {
Balance []*Coin `json:"balance"` 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 Coin struct {
Type string `json:"type"` Type string `json:"type"`
Quantity string `json:"quantity"` Quantity string `json:"quantity"`
} }
type KeyValue struct { type FloatValue struct {
Key string `json:"key"` Value float64 `json:"value"`
Value *Value `json:"value"`
} }
func (FloatValue) IsValue() {}
type IntValue struct {
Value int `json:"value"`
}
func (IntValue) IsValue() {}
type KeyValueInput struct { type KeyValueInput struct {
Key string `json:"key"` Key string `json:"key"`
Value *ValueInput `json:"value"` 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 { type NameRecord struct {
Latest *NameRecordEntry `json:"latest"` Latest *NameRecordEntry `json:"latest"`
History []*NameRecordEntry `json:"history"` History []*NameRecordEntry `json:"history"`
@ -102,18 +148,10 @@ type Record struct {
CreateTime string `json:"createTime"` CreateTime string `json:"createTime"`
ExpiryTime string `json:"expiryTime"` ExpiryTime string `json:"expiryTime"`
Owners []string `json:"owners"` Owners []string `json:"owners"`
Attributes []*KeyValue `json:"attributes"` Attributes []*Attribute `json:"attributes"`
References []*Record `json:"references"` References []*Record `json:"references"`
} }
type Reference struct {
ID string `json:"id"`
}
type ReferenceInput struct {
ID string `json:"id"`
}
type Status struct { type Status struct {
Version string `json:"version"` Version string `json:"version"`
Node *NodeInfo `json:"node"` Node *NodeInfo `json:"node"`
@ -125,6 +163,12 @@ type Status struct {
DiskUsage string `json:"disk_usage"` DiskUsage string `json:"disk_usage"`
} }
type StringValue struct {
Value string `json:"value"`
}
func (StringValue) IsValue() {}
type SyncInfo struct { type SyncInfo struct {
LatestBlockHash string `json:"latest_block_hash"` LatestBlockHash string `json:"latest_block_hash"`
LatestBlockHeight string `json:"latest_block_height"` LatestBlockHeight string `json:"latest_block_height"`
@ -138,23 +182,12 @@ type ValidatorInfo struct {
ProposerPriority *string `json:"proposer_priority"` 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 { type ValueInput struct {
Null *bool `json:"null"`
Int *int `json:"int"` Int *int `json:"int"`
Float *float64 `json:"float"` Float *float64 `json:"float"`
String *string `json:"string"` String *string `json:"string"`
Boolean *bool `json:"boolean"` Boolean *bool `json:"boolean"`
Reference *ReferenceInput `json:"reference"` Link *Link `json:"link"`
Values []*ValueInput `json:"values"` Array []*ValueInput `json:"array"`
Map []*KeyValueInput `json:"map"`
} }

33
gql/scalar.go Normal file
View 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)
}

View File

@ -2,15 +2,15 @@ package gql
import ( import (
"context" "context"
"encoding/json" "fmt" // #nosec G702
"fmt"
"reflect" // #nosec G702
"strconv" "strconv"
auctiontypes "github.com/cerc-io/laconicd/x/auction/types" auctiontypes "github.com/cerc-io/laconicd/x/auction/types"
bondtypes "github.com/cerc-io/laconicd/x/bond/types" bondtypes "github.com/cerc-io/laconicd/x/bond/types"
registrytypes "github.com/cerc-io/laconicd/x/registry/types" registrytypes "github.com/cerc-io/laconicd/x/registry/types"
sdk "github.com/cosmos/cosmos-sdk/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. // 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 return nil, nil
} }
recordType := record.ToReadableRecord() node, err := ipld.Decode(record.Attributes, dagjson.Decode)
attributes, err := getAttributes(&recordType) 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 { if err != nil {
return nil, err return nil, err
} }
references, err := getReferences(ctx, resolver, &recordType) references, err := resolver.GetRecordsByIds(ctx, links)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -79,11 +87,96 @@ func getGQLRecord(ctx context.Context, resolver QueryResolver, record registryty
ExpiryTime: record.GetExpiryTime(), ExpiryTime: record.GetExpiryTime(),
Owners: record.GetOwners(), Owners: record.GetOwners(),
Names: record.GetNames(), Names: record.GetNames(),
Attributes: attributes, Attributes: attributes.(MapValue).Value,
References: references, References: references,
}, nil }, 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) { func getGQLNameRecord(record *registrytypes.NameRecord) (*NameRecord, error) {
if record == nil { if record == nil {
return nil, fmt.Errorf("got nil record") return nil, fmt.Errorf("got nil record")
@ -163,136 +256,64 @@ func GetGQLAuction(auction *auctiontypes.Auction, bids []*auctiontypes.Bid) (*Au
return &gqlAuction, nil return &gqlAuction, nil
} }
func getReferences(ctx context.Context, resolver QueryResolver, r *registrytypes.RecordEncodable) ([]*Record, error) { func parseRequestValue(value *ValueInput) *registrytypes.QueryListRecordsRequest_ValueInput {
var ids []string if value == nil {
return nil
}
var val registrytypes.QueryListRecordsRequest_ValueInput
// #nosec G705 if value.String != nil {
for key := range r.Attributes { val.String_ = *value.String
//nolint: all val.Type = "string"
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))
}
}
}
}
} }
return resolver.GetRecordsByIds(ctx, ids) if value.Int != nil {
} val.Int = int64(*value.Int)
val.Type = "int"
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{},
} }
switch val := value.(type) { if value.Float != nil {
case nil: val.Float = *value.Float
kvPair.Value.Null = &trueVal val.Type = "float"
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
} }
jsonStr := string(bytes) if value.Boolean != nil {
kvPair.Value.JSON = &jsonStr val.Boolean = *value.Boolean
} val.Type = "boolean"
}
} }
if kvPair.Value.Null == nil { if value.Link != nil {
kvPair.Value.Null = &falseVal reference := &registrytypes.QueryListRecordsRequest_ReferenceInput{
Id: value.Link.String(),
} }
valueType := reflect.ValueOf(value) val.Reference = reference
if valueType.Kind() == reflect.Slice { val.Type = "reference"
bytes, err := json.Marshal(value)
if err != nil {
return nil, err
} }
jsonStr := string(bytes) // handle arrays
kvPair.Value.JSON = &jsonStr 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 &val
}
return kvPairs, nil
} }
func parseRequestAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput { func parseRequestAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput {
kvPairs := []*registrytypes.QueryListRecordsRequest_KeyValueInput{} kvPairs := []*registrytypes.QueryListRecordsRequest_KeyValueInput{}
for _, value := range attrs { for _, value := range attrs {
parsedValue := parseRequestValue(value.Value)
kvPair := &registrytypes.QueryListRecordsRequest_KeyValueInput{ kvPair := &registrytypes.QueryListRecordsRequest_KeyValueInput{
Key: value.Key, Key: value.Key,
Value: &registrytypes.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 := &registrytypes.QueryListRecordsRequest_ReferenceInput{
Id: value.Value.Reference.ID,
}
kvPair.Value.Reference = reference
kvPair.Value.Type = "reference"
}
// TODO: Handle arrays.
kvPairs = append(kvPairs, kvPair) kvPairs = append(kvPairs, kvPair)
} }

View File

@ -362,8 +362,6 @@ func (k Keeper) SetAttributeMapping(ctx sdk.Context, key []byte, recordID string
if err != nil { if err != nil {
return fmt.Errorf("cannot unmarshal byte array, error, %w", err) return fmt.Errorf("cannot unmarshal byte array, error, %w", err)
} }
} else {
recordIds = []string{}
} }
recordIds = append(recordIds, recordID) recordIds = append(recordIds, recordID)
bz, err := json.Marshal(recordIds) bz, err := json.Marshal(recordIds)