我正在尝试在 Meteor blaze 项目中使用 Apollo graphql。
我正在使用 swydo:blaze-apollo
中的软件包。
使用graphql查询从mongoDB获取数据就可以了。
// Using this one can get data
const LOCATION_COUNTRY_QUERY = gql`
{
locations(location_type: "Country"){
location_id
name
iso_code
}
}
`;
Template.home.onCreated(function(){
const country = this.gqlQuery({query: LOCATION_COUNTRY_QUERY}).get();
console.log(country.locations); // Which will show an array list of country.
});
但是,我不想在查询中对“Country”进行硬编码。我想将字符串传递到查询中,然后获取其他 location_type 的数据。但我找不到任何关于它的文章,并且 gql 语法只是阻止任何参数。
谁有类似经历,可以给点建议吗?
最佳答案
您可以使用 GraphQL 变量来完成这项工作。
首先,向您的 LOCATION_COUNTRY_QUERY
声明变量:
const LOCATION_COUNTRY_QUERY = gql`
query locationCountryQuery($locationType: String!){
locations(location_type: $locationType){
location_id
name
iso_code
}
}
`;
现在您可以为查询提供新的变量
选项:
Template.home.onCreated(function(){
const country = this.gqlQuery({query: LOCATION_COUNTRY_QUERY, variables: {locationType: "Country"}).get();
console.log(country.locations); // Which will show an array list of country.
});
关于meteor - 如何将参数传递给graphql查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41632152/