我目前正在使用GraphQL/GraphCool包装一个RestAPI,但是当我用Float编写解析器以提取纬度和经度时,我会得到以下错误:
"code": 5004,
"message": "Returned JSON Object does not match the GraphQL type. The field 'latitud' should be of type Float \n\n Json:
{\n \"id\": \"6115\",\n \"nombre\": \"ABARROTES LA SOLEDAD\",\n \"latitud\": \"21.85779823\",\n \"longitud\": \"-102.28161261\"\n}\n\n"如果我使用字符串,就没有问题了!
解析器SDL
type AbarrotesPayload {
id: ID!
nombre: String!
latitud: Float!
longitud: Float!
}
extend type Query {
feed(distancia: Int!): [AbarrotesPayload!]!
}解析函数
"use strict";
const fetch = require("node-fetch");
const API_TOKEN = "d3bfd48a-bced-4a58-a58b-4094da934cf1";
module.exports = event => {
const distancia = event.data.distancia;
return fetch(getRestEndpoint(distancia, API_TOKEN))
.then(response => response.json())
.then(data => {
const abarrotes = [];
for (let item in data) {
abarrotes.push({
id: data[item].Id,
nombre: data[item].Nombre,
latitud: data[item].Latitud,
longitud: data[item].Longitud
});
}
console.log(abarrotes);
return { data: abarrotes };
});
};
function getRestEndpoint(query) {
return `http://www3.inegi.org.mx/sistemas/api/denue/v1/consulta/buscar/abarrotes/21.85717833,-102.28487238/${query}/${API_TOKEN}`;
}我的查询如下所示:
query {
feed(distancia: 400) {
id
nombre
latitud
longitud
}
}顺便说一下,我在graph.cool平台上工作!
发布于 2018-02-12 08:17:19
这与GraphQL/GraphCool无关,我只需要将接收到的值作为字符串进行parseFloat(),然后将其推送到数组中。
latitud: parseFloat(data[item].Latitud),
longitud: parseFloat(data[item].Longitud)https://stackoverflow.com/questions/48726241
复制相似问题