I'm currently working on GraphQL/GraphCool wrapping a RestAPI, but when I write my resolver with Float in order to extract latitude and longitude as floats I get the following error:
"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"
if I use String there is no problem!
RESOLVER SDL
type AbarrotesPayload {
id: ID!
nombre: String!
latitud: Float!
longitud: Float!
}
extend type Query {
feed(distancia: Int!): [AbarrotesPayload!]!
}
RESOLVER FUNCTION
"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}`;
}
And my Query is the following:
query {
feed(distancia: 400) {
id
nombre
latitud
longitud
}
}
By the way, im working on the graph.cool platform!
This had nothing to do with GraphQL/GraphCool, I just needed to parseFloat()
the values which I'm receiving as a string, and then push it to the array.
latitud: parseFloat(data[item].Latitud),
longitud: parseFloat(data[item].Longitud)