在Flutter中创建Graphql查询的语法错误

时间:2020-09-08 19:51:35

标签: flutter graphql

我正在尝试将变量传递给查询对象,但我认为我在某处存在语法错误:

当我有这样的字符串(未传递给查询小部件)时,api调用有效:

String fetchMissingConsignments = """ query MyQuery {
  getCountMissingConsignmentNotes(accountId: "66fdc17b-308d-4e71-85ee-0d4a77e3276e")
}""";

但是当我尝试将其传递给查询小部件时,如下所示:

   String fetchMissingConsignments = """ query MyQuery(\$accountId: String!){
          getCountMissingConsignmentNotes(where:{accountId:{ _eq:\$accountId}})
        }""";

我回来了...

和查询对象:

    @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
              child: Query(
            builder: (QueryResult result, {VoidCallback refetch, FetchMore fetchMore}) {
              final res = result.data['getCountMissingConsignmentNotes'];
    
              return Text(res.toString());
            },
            options: QueryOptions(
              
              documentNode: gql(fetchMissingConsignments),
              variables: {'accountId': '66fdc17b-308d-4e71-85ee-0d4a77e3276e'}, //pass variable here 
            ),
          )),
    );
  }

PS 我正在使用此graphql plugin

1 个答案:

答案 0 :(得分:0)

带有变量的查询将在函数/方法内部工作,查询中变量的值将由函数/方法的参数设置,如下所示:

  searchData(String name) {
    if (name.length > 2) {
      searchQuery = '''
      query myQuery  {
      launches(where: {
      name: "$name"
     })   {
        name 
        details  
    }
  }
  ''';

      setState(() {
        this.query = name;
      });
    }
  }