我目前正在研究包装 RestAPI 的 GraphQL/GraphCool,但是当我使用 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 平台上工作!