我正在使用zomato api制作带有React,node和Graphql的餐厅评论应用程序。根据文档,我正在传递查询参数,但是在graphiql工具中,我得到的是“未定义”。
这是错误:
这是api文档参考:
curl -X GET --header "Accept: application/json" --header "user-key: abc" "https://developers.zomato.com/api/v2.1/restaurant?res_id=2"
我的查询是如何使用graphiql工具中的查询参数测试api?还让我知道如何更新api中的查询参数以捕获用户输入并相应显示结果。
下面是schema.js的代码,该代码定义了graphql模式
const axios = require("axios");
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
GraphQLList,
GraphQLFloat,
GraphQLSchema
} = require("graphql");
const RestaurantType = new GraphQLObjectType({
name: "Restaurant",
fields: () => ({
res_id: {
type: GraphQLInt
},
name: {
type: GraphQLString
},
url: {
type: GraphQLString
},
photos_url: {
type: GraphQLString
},
menu_url: {
type: GraphQLString
},
rating: {
type: UserRatingType
},
location: {
type: LocationType
}
})
});
const UserRatingType = new GraphQLObjectType({
name: "UserRating",
fields: () => ({
aggregate_rating: {
type: GraphQLFloat
}
})
});
const LocationType = new GraphQLObjectType({
name: "Location",
fields: () => ({
address: {
type: GraphQLString
},
locality: {
type: GraphQLString
},
zipcode: {
type: GraphQLInt
}
})
});
const userKey = 'abc';
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
restaurants: {
type: new GraphQLList(RestaurantType),
resolve(parent, args) {
return axios
.get(`https://developers.zomato.com/api/v2.1/restaurant`, {
headers: {
'user-key': {userKey}
}})
.then(res => res.data)
.catch((error) => {
console.log('error is ' + error);
});
}
},
restaurant: {
type: RestaurantType,
args: {
res_id: {
type: GraphQLInt
}
},
resolve(parent, args) {
return axios
.get(`https://developers.zomato.com/api/v2.1/restaurant?
res_id`, {
params: {
res_id
},
headers: {
'user-key': {
userKey
}
}
})
.then(res => {
console.log(res.data)})
.catch((error) => {
console.log('error is ' + error);
});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});