运行 基于 REST 的 GraphQL API returns 空数据
Running GraphQL over REST API returns null data
我已按照博客中的说明浏览了此博客 post http://graphql.org/blog/rest-api-graphql-wrapper/
在我自己的 REST API 上创建一个 graphQL 端点。如果我在控制台中记录调用,我可以看到生成了正确的响应,但 GraphiQL IDE 中的数据始终为 NULL。可能是什么原因?
这是我的代码:
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
} from 'graphql'
import fetch from 'node-fetch'
const BASE_URL = 'http://localhost/my.test.web/api/v1/customer/91/reservation'
const ReservationType = new GraphQLObjectType({
name: 'Reservation',
description: 'This is reservation details',
fields: () => ({
id: {type: GraphQLString},
confirmationNumber: {
type: GraphQLString,
resolve: (reservation) => reservation.confirmationNumber
},
status: {
type: GraphQLString,
resolve: (reservation) => reservation.status
}
})
});
const QueryType = new GraphQLObjectType(
{
name: "query",
description: "This is query by Id",
fields: () => ({
reservation: {
type: ReservationType,
args: {
id: {type: GraphQLString}
},
resolve: (root, args) => {
var url = BASE_URL+ '/' + args.id;
console.log(url);
var options = {
headers: {
'Accept': 'application/json',
'Accept-Language':'en-US'
}
};
fetch(url,options)
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
return json;
});
}
}
}
)
});
export default new GraphQLSchema(
{
query: QueryType,
}
)
当我 运行 使用 graphiQL 和 express 时,我可以看到日志是由这部分代码正确生成的 -
.then(function(json) {
console.log(json);
return json;
}
但在 GraphiQL 中 UI 数据为空
GraphiQL IDE query screenshot
最后我找到了原因 - 这是语法而不是返回的 JSON。请注意每个块末尾的“,”,并删除 resolve 周围的包装器:
QueryType 应该定义如下,它的工作原理非常棒
const QueryType = new GraphQLObjectType({
name: "query",
description: "This is person query by Id",
fields: () => ({
person: {
type: PersonType,
args: {
id: { type: GraphQLString },
},
resolve: (root, args) =>
fetch(BASE_URL +'/people/' +args.id)
.then(function(res) {
return res.json()
})
.then(function(json) {
console.log(json)
return json
}),
},
}),
});
我已按照博客中的说明浏览了此博客 post http://graphql.org/blog/rest-api-graphql-wrapper/ 在我自己的 REST API 上创建一个 graphQL 端点。如果我在控制台中记录调用,我可以看到生成了正确的响应,但 GraphiQL IDE 中的数据始终为 NULL。可能是什么原因?
这是我的代码:
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
} from 'graphql'
import fetch from 'node-fetch'
const BASE_URL = 'http://localhost/my.test.web/api/v1/customer/91/reservation'
const ReservationType = new GraphQLObjectType({
name: 'Reservation',
description: 'This is reservation details',
fields: () => ({
id: {type: GraphQLString},
confirmationNumber: {
type: GraphQLString,
resolve: (reservation) => reservation.confirmationNumber
},
status: {
type: GraphQLString,
resolve: (reservation) => reservation.status
}
})
});
const QueryType = new GraphQLObjectType(
{
name: "query",
description: "This is query by Id",
fields: () => ({
reservation: {
type: ReservationType,
args: {
id: {type: GraphQLString}
},
resolve: (root, args) => {
var url = BASE_URL+ '/' + args.id;
console.log(url);
var options = {
headers: {
'Accept': 'application/json',
'Accept-Language':'en-US'
}
};
fetch(url,options)
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
return json;
});
}
}
}
)
});
export default new GraphQLSchema(
{
query: QueryType,
}
)
当我 运行 使用 graphiQL 和 express 时,我可以看到日志是由这部分代码正确生成的 -
.then(function(json) {
console.log(json);
return json;
}
但在 GraphiQL 中 UI 数据为空 GraphiQL IDE query screenshot
最后我找到了原因 - 这是语法而不是返回的 JSON。请注意每个块末尾的“,”,并删除 resolve 周围的包装器:
QueryType 应该定义如下,它的工作原理非常棒
const QueryType = new GraphQLObjectType({
name: "query",
description: "This is person query by Id",
fields: () => ({
person: {
type: PersonType,
args: {
id: { type: GraphQLString },
},
resolve: (root, args) =>
fetch(BASE_URL +'/people/' +args.id)
.then(function(res) {
return res.json()
})
.then(function(json) {
console.log(json)
return json
}),
},
}),
});