Geo*_* Wu 4 meteor apollo meteor-blaze graphql
我正在尝试在 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.
});
Run Code Online (Sandbox Code Playgroud)
但是,我不想在查询中对“国家/地区”进行硬编码。我想将字符串传递到查询中,然后使用其他 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
}
}
`;
Run Code Online (Sandbox Code Playgroud)
现在您可以为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.
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2999 次 |
最近记录: |