【问题标题】:Flutter graphql query with variable带有变量的 Flutter graphql 查询
【发布时间】:2021-12-01 05:11:45
【问题描述】:

我是第一次测试flutter graphql。这是我正在使用 graphql explorer 的查询和变量。

我的问题是在我传递查询和queryVariables之前,我需要将queryVariables中的1001替换为参数值。我可以知道如何替换它吗?

const String query = r'''
  query Albums($where: AlbumWhereInput) {
  albums(where: $where) {
    id
    title
    artist {
      name
    }
  }
}
''';

const String queryVariables = r'''
{
  "where": {
    "artist_id": 1001
  }
}
''';

Future<QueryResult> performQuery(String query,
  {required Map<String, dynamic> variables}) async {
    QueryOptions options =
        QueryOptions(document: gql(query), variables: variables);

    final result = await _client.query(options);

    return result;
 }

【问题讨论】:

    标签: flutter dart graphql


    【解决方案1】:

    您尚未指定您使用的软件包,因此我无法具体说明什么是最好的方法。但通常,您可以使用字符串插值之类的方法:

    final artistId = 1001;
    final queryVariables = '''
    {
      "where": {
        "artist_id": $artistId
      }
    }
    ''';
    
    

    请注意我们如何使用 ''' 的普通字符串而不是原始字符串 (r''') 以便能够通过 $ 进行插值。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 2018-01-12
    • 1970-01-01
    • 2019-05-17
    • 2023-03-12
    • 2018-03-02
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多