【问题标题】:Node GraphQL resolve null data from API [duplicate]Node GraphQL从API解析空数据[重复]
【发布时间】:2017-10-28 06:52:51
【问题描述】:

我正在通过他们的官方website学习GraphQL。

我在此代码上的 graphql 的目的是作为我现有 REST API 的包装器。 为此,我已经有了一个带有此响应的 rest api:

GET: /people/:id
RESPONSE: 
{
  "person": [
    {
      "id": "1",
      "userName": "mark1",
      "firstName": "Mark",
      "lastName": "Zuckerberg",
      "email": "mark@facebook.com",
      "friends": [
        "/people/2",
        "/people/3"
      ]
    }
  ]
}

以下代码是我的 graphql 架构

import {
    GraphQLList,
    GraphQLObjectType,
    GraphQLString,
    GraphQLSchema
} from "graphql";

import fetch from "node-fetch";

const BASE_URL = "http://localhost:5000";

function getPersonByURL(relativeUrl) {
    return fetch(`${BASE_URL}${relativeUrl}`)
        .then(res => res.json())
        .then(json => json.person);
}

const PersonType = new GraphQLObjectType({
    name: "Person",
    type: "Somebody",
    fields: () => ({
        firstName: {
            type: GraphQLString,
            resolve: person => person.firstName
        },
        lastName: {
            type: GraphQLString,
            resolve: person => person.lastName
        },
        email: {
            type: GraphQLString
        },
        id: {
            type: GraphQLString
        },
        userName: {
            type: GraphQLString
        },
        friends: {
            type: new GraphQLList(PersonType),
            resolve: (person) => person.friends.map(getPersonByURL)
        }
    })
});

const QueryType = new GraphQLObjectType({
    name: "Query",
    description: "the root of all queries",
    fields: () => ({
        person: {
            type: PersonType,
            args: {
                id: { type: GraphQLString }
            },
            resolve: (root, args) => getPersonByURL(`/people/${args.id}`)
        }
    })
});

export default new GraphQLSchema({
    query: QueryType
});

当我使用 Graphiql 执行请求时,它在每个字段上返回 null。 我相信我在如何表示我的 json 响应或如何从 rest api 访问我的 json 响应时犯了错误。

这些是来自graphiql的请求和结果

REQUEST
{
  person(id: "1") {
    firstName
  }
}

RESPONSE
{
  "data": {
    "person": {
      "firstName": null
    }
  }
}

你能帮忙提示一下吗?

【问题讨论】:

    标签: json node.js rest graphql relay


    【解决方案1】:

    为了找到问题,我将您的 json 放在了要点上并修改了您的代码,如下所示:

    // ...
    firstName: {
      type: GraphQLString,
      resolve: person => JSON.stringify(person)
    },
    // ...
    

    运行以下查询后:

    {
      person(id: "1") {
        firstName
      }
    }
    

    结果如下:

    {
    "data": {
        "person": {
          "firstName": "[{\"id\":\"1\",\"userName\":\"mark1\",\"firstName\":\"Mark\",\"lastName\":\"Zuckerberg\",\"email\":\"mark@facebook.com\",\"friends\":[\"/people/2\",\"/people/3\"]}]"
        }
      }
    }
    

    您会注意到person 是一个数组,因此显然它没有firstName 和其他属性。您必须在每个字段解析器或​​根类型解析器中解包数组:

    resolve: (root, args) => getPersonByURL(`/people${args.id}.json`)
      .then(persons => persons[0])
    

    这里是带有工作代码的 GraphQL Launchpad:https://launchpad.graphql.com/j1v0kprrp

    【讨论】:

    • 是的,这就是答案,谢谢您的帮助
    猜你喜欢
    • 2018-07-13
    • 2017-01-22
    • 2020-03-04
    • 2021-03-21
    • 1970-01-01
    • 2017-06-05
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多