首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphQL/GraphCool为什么坐标不适用于浮点?

GraphQL/GraphCool为什么坐标不适用于浮点?
EN

Stack Overflow用户
提问于 2018-02-10 22:29:36
回答 1查看 221关注 0票数 0

我目前正在使用GraphQL/GraphCool包装一个RestAPI,但是当我用Float编写解析器以提取纬度和经度时,我会得到以下错误:

代码语言:javascript
复制
"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

代码语言:javascript
复制
type AbarrotesPayload {
  id: ID!
  nombre: String!
  latitud: Float!
  longitud: Float!
}

extend type Query {
  feed(distancia: Int!): [AbarrotesPayload!]!
}

解析函数

代码语言:javascript
复制
"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}`;
}

我的查询如下所示:

代码语言:javascript
复制
query {
  feed(distancia: 400) {
    id
    nombre
    latitud
    longitud
  }
}

顺便说一下,我在graph.cool平台上工作!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-12 08:17:19

这与GraphQL/GraphCool无关,我只需要将接收到的值作为字符串进行parseFloat(),然后将其推送到数组中。

代码语言:javascript
复制
latitud: parseFloat(data[item].Latitud),
longitud: parseFloat(data[item].Longitud)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48726241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档