我一直在尝试使用 go-grapqhql,并且能够成功地创建一些模式,例如:
type Artist struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
Instrument []Instrument `json:"instrument"`
}
type Instrument struct{
Name string `json:"name"`
}
var artists = []Artist{
{
ID: "1",
Name: "Taylor Swift",
Type: "artist",
Instrument: []Instrument{
{Name:"violin"},
},
},
}
var instrumentType = graphql.NewObject(graphql.ObjectConfig{
Name: "Instrument",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
},
})
var artistType = graphql.NewObject(graphql.ObjectConfig{
Name: "Artist",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"name": &graphql.Field{
Type: graphql.String,
},
"type": &graphql.Field{
Type: graphql.String,
},
"instrument": &graphql.Field{
Type: graphql.NewList(instrumentType),
},
},
})
但是,当我尝试使用类似的结构做同样的事情时......
type Blk struct {
Header Header `json:"header,omitempty"`
Payload []Payload `json:"payload,omitempty"`
}
type Header struct {
Parent string `json:"parent,omitempty"`
Number int `json:"number,omitempty"`
Nonce int `json:"nonce,omitempty"`
Time int `json:"time,omitempty"`
Miner string `json:"miner,omitempty"`
}
type Payloads []Payloads
type Payload struct {
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Value int `json:"value,omitempty"`
Nonce int `json:"nonce,omitempty"`
Data string `json:"data,omitempty"`
Time int `json:"time,omitempty"`
Signature string `json:"signature,omitempty"`
}
var blk = []Blk{
{Header: Header{
Parent: "0000000000000000000000000000000000000000000000000000000000000000",
Number: 0,
Nonce: 1548399560,
Time: 1609374088,
Miner: "0x699ecb24665b9734c420db0f75ed9677a8615eb1",
},
Payload: []Payload{
{
From: "0x699ecb24665b9734c420db0f75ed9677a8615eb1",
To: "0x4adb5430e58171aa233a12d95b89d4ff2f211bcb",
Value: 100000,
Nonce: 1,
Data: "",
Time: 1609374076,
Signature: "tzRWXErhQwR9wHje9gT6tNTftZ07KN9G+MHgh0Ock5YgDMXkCL611D4xruq9pULQfS+j1HPkbo9VyCWxSxDfMwA=",
},
},
},
}
var transactionType = graphql.NewObject(
graphql.ObjectConfig{
Name: "payload",
Fields: graphql.Fields{
"from": &graphql.Field{
Type: graphql.String,
},
"to": &graphql.Field{
Type: graphql.String,
},
"value": &graphql.Field{
Type: graphql.Int,
},
"nonce": &graphql.Field{
Type: graphql.Int,
},
"data": &graphql.Field{
Type: graphql.String,
},
"time": &graphql.Field{
Type: graphql.Int,
},
"signature": &graphql.Field{
Type: graphql.String,
},
},
})
var blocksType = graphql.NewObject(
graphql.ObjectConfig{
Name: "header",
Fields: graphql.Fields{
"parent": &graphql.Field{
Type: graphql.String,
},
"number": &graphql.Field{
Type: graphql.Int,
},
"nonce": &graphql.Field{
Type: graphql.Int,
},
"time": &graphql.Field{
Type: graphql.Int,
},
"miner": &graphql.Field{
Type: graphql.String,
},
"payload": &graphql.Field{
Type: graphql.NewList(transactionType),
},
},
},
)
我的查询只返回 null 即。查询
query{
artists{id,instrument{name}}
}
返回
{
"data": {
"artists": [
{
"id": "1",
"instrument": [
{
"name": "violin"
}
]
}
]
}
}
这很好,但查询
query{
blocks{parent,number,nonce,time,miner, payload{from, to,value, nonce, data, time, signature}}
}
返回
{
"data": {
"blocks": [
{
"miner": null,
"nonce": null,
"number": null,
"parent": null,
"payload": [
{
"data": "",
"from": "0x699ecb24665b9734c420db0f75ed9677a8615eb1",
"nonce": 1,
"signature": "tzRWXErhQwR9wHje9gT6tNTftZ07KN9G+MHgh0Ock5YgDMXkCL611D4xruq9pULQfS+j1HPkbo9VyCWxSxDfMwA=",
"time": 1609374076,
"to": "0x4adb5430e58171aa233a12d95b89d4ff2f211bcb",
"value": 100000
}
],
"time": null
}
]
}
}
这不是很好。我一直在尝试不同的事情,例如将所有字段设为字符串,但没有任何效果。