我的一些go-graphql查询的字段返回null,而其他字段返回正常,其他类似的查询一起工作



我一直在尝试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
}
]
}
}

这不太好。我一直在尝试不同的方法,比如让所有的字段都变成字符串,但都没有成功。

因此,我将graphql模式更改为:

var blockType = graphql.NewObject(
graphql.ObjectConfig{
Name: "block",
Fields: graphql.Fields{
"header" :&graphql.Field{
Type: headerType,
},
"payload": &graphql.Field{
Type: graphql.NewList(payloadType),
},
},
})
var payloadType = 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 headerType = 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),
},

出于某种原因,如果您尝试进行这种类型的嵌套,请使用graphql(或者graphql不知道该做什么(。FWI:现在我的查询如下:

query{
blocks{header{parent,number,nonce,time,miner},payload{from,to,value,nonce,data,time,signature}}
}

并且它们产生所需的结果。

最新更新