首页 > 解决方案 > 从 CryptoCompare API 获取数据时,GraphQL 错误:“预期可迭代,但未找到用于字段查询的对象”

问题描述

我正在使用 GraphQL/express/express-graphql/axios 从 CryptoCompare API 检索特定的硬币数据。

我有两个端点:

1) https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,LTC,BCH,NEO,ETC,XMR&tsyms=USD

From endpoint 1, I want to retrieve the following in USD for 8 coins:
  - FROMSYMBOL, CHANGEPCT24HOUR, PRICE, MKTCAP, TOTALVOLUME24HTO

2) https://min-api.cryptocompare.com/data/coin/generalinfo?fsyms=BTC&tsym=USD

From endpoint 2, I want to retrieve the following just for Bitcoin/BTC:
  - Id, FullName, ImageUrl

我已经用两个文件设置了我的后端服务器,并使用 graphiql 测试了查询。

文件 1 - server.js

const express = require("express")
const graphqlHTTP = require("express-graphql")
const cors = require("cors")
const schema = require("./schema")

const app = express()

app.use(cors())

app.use(
  "/graphql",
  graphqlHTTP({
    schema,
    graphiql: true
  })
)

const PORT = process.env.PORT || 4000

app.listen(PORT, console.log(`✅  Listening to port ${PORT}`))

文件 2 - schema.js

const {
  GraphQLObjectType,
  GraphQLList,
  GraphQLID,
  GraphQLInt,
  GraphQLString,
  GraphQLSchema
} = require("graphql")
const axios = require("axios")

const CoinDataType = new GraphQLObjectType({
  name: "CoinData",
  fields: () => ({
    FROMSYMBOL: { type: GraphQLString },
    CHANGEPCT24HOUR: { type: GraphQLInt },
    PRICE: { type: GraphQLInt },
    MKTCAP: { type: GraphQLInt },
    TOTALVOLUME24HTO: { type: GraphQLInt }
  })
})

const CoinInfoType = new GraphQLObjectType({
  name: "CoinInfo",
  fields: () => ({
    Id: { type: GraphQLID },
    FullName: { type: GraphQLString },
    ImageUrl: { type: GraphQLString }
  })
})

const Query = new GraphQLObjectType({
  name: "Query",
  fields: {
    CoinData: {
      type: new GraphQLList(CoinDataType),
      resolve(parent, args) {
        return axios
          .get(
            "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=BTC,ETH,LTC,BCH,NEO,ETC,XMR&tsyms=USD"
          )
          .then(res => res.data)
      }
    },
    CoinInfo: {
      type: new GraphQLList(CoinInfoType),
      resolve(parent, args) {
        return axios
          .get(
            "https://min-api.cryptocompare.com/data/coin/generalinfo?fsyms=BTC&tsym=USD"
          )
          .then(res => res.data)
      }
    }
  }
})

module.exports = new GraphQLSchema({ query: Query })

当我使用 graphiql 来测试我的查询时:

{
  CoinData {
    FROMSYMBOL
  }
  CoinInfo {
    Id
  }
}

...我收到此错误:

{
  "errors": [
    {
      "message": "Expected Iterable, but did not find one for field Query.CoinInfo.",
      "locations": [
        {
          "line": 5,
          "column": 3
        }
      ],
      "path": [
        "CoinInfo"
      ]
    },
    {
      "message": "Expected Iterable, but did not find one for field Query.CoinData.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "CoinData"
      ]
    }
  ],
  "data": {
    "CoinData": null,
    "CoinInfo": null
  }
}

如何解决此错误?谢谢。

标签: graphqlexpress-graphqlgraphiql

解决方案


推荐阅读