我正在GraphQL
与REST
端点连接,我确认每当我调用http://localhost:3001/graphql
它时它都会击中REST
端点并且它正在JSON
向服务器返回响应GraphQL
,但是我从GraphQL
服务器得到一个空响应,GUI
如下所示:
{
"data": {
"merchant": {
"id": null
}
}
}
查询(手动解码):
http://localhost:3001/graphql?query={
merchant(id: 1) {
id
}
}
下面是我的GraphQLObjectType
样子:
const MerchantType = new GraphQLObjectType({
name: 'Merchant',
description: 'Merchant details',
fields : () => ({
id : {
type: GraphQLString // ,
// resolve: merchant => merchant.id
},
email: {type: GraphQLString}, // same name as field in REST response, so resolver is not requested
mobile: {type: GraphQLString}
})
});
const QueryType = new GraphQLObjectType({
name: 'Query',
description: 'The root of all... queries',
fields: () => ({
merchant: {
type: merchant.MerchantType,
args: {
id: {type: new GraphQLNonNull(GraphQLID)},
},
resolve: (root, args) => rest.fetchResponseByURL(`merchant/${args.id}/`)
},
}),
});
来自端点的响应REST
(我也尝试使用 JSON 中的单个对象而不是 JSON 数组):
[
{
"merchant": {
"id": "1",
"email": "a@b.com",
"mobile": "1234567890"
}
}
]
REST调用使用node-fetch
function fetchResponseByURL(relativeURL) {
return fetch(`${config.BASE_URL}${relativeURL}`, {
method: 'GET',
headers: {
Accept: 'application/json',
}
})
.then(response => {
if (response.ok) {
return response.json();
}
})
.catch(error => { console.log('request failed', error); });
}
const rest = {
fetchResponseByURL
}
export default rest
GitHub:https
://github.com/vishrantgupta/graphql
JSON端点(虚拟):https ://api.myjson.com/bins/8lwqk
编辑:添加node.js
标签,可能是 promise 对象的问题。