0

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

这不是很好。我一直在尝试不同的事情,例如将所有字段设为字符串,但没有任何效果。

4

1 回答 1

0

因此,我将 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),
            },

出于某种原因,如果您尝试执行这种类型的嵌套 go-graphql(或 graphql 无法弄清楚该做什么)。FWI:现在我的查询如下所示:

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

他们产生了所需的结果。

于 2021-01-03T20:27:21.693 回答