Я использую GraphQL / express / express-graphql / axios для извлечения определенных данных монет из API CryptoCompare.
У меня есть две конечные точки:
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
}
}
Как мне обойти эту ошибку? Благодарю.