【发布时间】:2017-06-05 09:28:12
【问题描述】:
我按照博文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
【问题讨论】:
-
在日志语句中,我可以看到 API 返回的正确 JSON,但在 GraphiQL UI 中为空
标签: graphql graphql-js