我正在尝试在 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.
});
但是,我不想在查询中对“国家/地区”进行硬编码。我想将字符串传递到查询中,然后获取其他 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
}
}
`;
现在您可以提供一个新的variables
查询选项:
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.
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)