节点 GraphQL 从 API 解析空数据
Node GraphQL resolve null data from API
我正在通过他们的官方学习 GraphQL website。
我的 graphql 在此代码上的目的是作为我现有 REST API 的包装器。
为此,我已经休息 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 回复或如何从其他 api.
访问我的 json 回复时犯了错误
这些是 graphiql
的请求和结果
REQUEST
{
person(id: "1") {
firstName
}
}
RESPONSE
{
"data": {
"person": {
"firstName": null
}
}
}
你能帮忙提示一下吗?
为了找到问题,我将您的 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
我正在通过他们的官方学习 GraphQL website。
我的 graphql 在此代码上的目的是作为我现有 REST API 的包装器。 为此,我已经休息 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 回复或如何从其他 api.
这些是 graphiql
REQUEST
{
person(id: "1") {
firstName
}
}
RESPONSE
{
"data": {
"person": {
"firstName": null
}
}
}
你能帮忙提示一下吗?
为了找到问题,我将您的 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