GraphQL
具有
REST
终点,我确认每次我打电话
http://localhost:3001/graphql
它正在撞击
休息
端点,它正在返回
JSON
回应
图形
图形
服务器到
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}/`)
},
}),
});
答复
端点(我还尝试使用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
https://api.myjson.com/bins/8lwqk
编辑:
添加
node.js
标签,可能是承诺对象的问题。