GraphQL错误:从CryptoCompare API提取数据时,“预期可迭代,但未为字段查询找到一个”

时间:2019-03-22 13:59:13

标签: graphql express-graphql graphiql

我正在使用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
  }
}

如何解决此错误?谢谢。

0 个答案:

没有答案