Back

Flutter GraphQL Batch Query NOT WORKING

  • 0
  • Flutter
Tawfek
8 Jul, 2024, 14:49

Hi,

I finally caught the error in flutter sdk! It happens after 4 months of struggling. Basically, graphql has two usage methods. The first one is limited and can't get two documents for example in one shot

The other method is better as it allows that.

First method in flutter is working fine.

Second method doesn't work at all because it needs the query as a map and not as a list of queries.

I fixed this in the sdk but don't know how to make that a permanent solution that isn't wiped with every pub get.

TL;DR
Developers were experiencing difficulties with Flutter GraphQL batch queries. The issue was due to the query method requiring a List instead of a Map. By changing the required parameter to List in the query method, batch queries worked successfully. Permanent solution requires updating the SDK.
Tawfek
8 Jul, 2024, 14:49

////-------------Before :------------////

TypeScript
part of '../appwrite.dart';

/// The GraphQL API allows you to query and mutate your Appwrite server using
/// GraphQL.
class Graphql extends Service {
  /// Initializes a [Graphql] service
  Graphql(super.client);

  /// GraphQL endpoint
  ///
  /// Execute a GraphQL mutation.
  Future query({required List query}) async {
    const String apiPath = '/graphql';

    final Map<String, dynamic> apiParams = {
      'query': query,
    };

    final Map<String, String> apiHeaders = {
      'x-sdk-graphql': 'true',
      'content-type': 'application/json',
    };

    final res = await client.call(HttpMethod.post,
      path: apiPath, params: apiParams, headers: apiHeaders,);

    return res.data;
  }

  /// GraphQL endpoint
  ///
  /// Execute a GraphQL mutation.
  Future mutation({required Map query}) async {
    const String apiPath = '/graphql/mutation';

    final Map<String, dynamic> apiParams = {
      'query': query,
    };

    final Map<String, String> apiHeaders = {
      'x-sdk-graphql': 'true',
      'content-type': 'application/json',
    };

    final res = await client.call(HttpMethod.post,
        path: apiPath, params: apiParams, headers: apiHeaders);

    return res.data;
  }
}
Tawfek
8 Jul, 2024, 14:50

|

////-------------After :------------////

|

TypeScript
part of '../appwrite.dart';

/// The GraphQL API allows you to query and mutate your Appwrite server using
/// GraphQL.
class Graphql extends Service {
  /// Initializes a [Graphql] service
  Graphql(super.client);

  /// GraphQL endpoint
  ///
  /// Execute a GraphQL mutation.
  Future query({required List query}) async {
    const String apiPath = '/graphql';

    final Map<String, dynamic> apiParams = {
      'query': query,
    };

    final Map<String, String> apiHeaders = {
      'x-sdk-graphql': 'true',
      'content-type': 'application/json',
    };

    final res = await client.call(HttpMethod.post,
      path: apiPath, params: apiParams, headers: apiHeaders,);

    return res.data;
  }

  /// GraphQL endpoint
  ///
  /// Execute a GraphQL mutation.
  Future mutation({required List query}) async {
    const String apiPath = '/graphql/mutation';

    final Map<String, dynamic> apiParams = {
      'query': query,
    };

    final Map<String, String> apiHeaders = {
      'x-sdk-graphql': 'true',
      'content-type': 'application/json',
    };

    final res = await client.call(HttpMethod.post,
        path: apiPath, params: apiParams, headers: apiHeaders);

    return res.data;
  }
}
Tawfek
8 Jul, 2024, 14:50

One change: {required Map query} to {required List query}

And it works to batch queries with this change

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more