Back

graphql type error

  • 0
  • Databases
  • Flutter
  • GraphQL
Faisal7
3 Jan, 2024, 17:56

Hi everybody, i am trying to use the graphql endpoint so i am using the ferry client for that in my flutter app so according to the documentation i seted up the httpLink the following way and pass it to my ferry client

TypeScript
    Env.endpoint
    defaultHeaders: <String, String>{
      "X-Appwrite-Project": Env.appwriteProjectId
    }
  );

but i am getting this error TypeError (type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>?' in type cast) and this error comes from the package gql_http_link/src/link.dart where their is a function called Map<String, dynamic>? _defaultHttpResponseDecoder( http.Response httpResponse) => json.decode( utf8.decode( httpResponse.bodyBytes, ), ) as Map<String, dynamic>?;
i assume this can be the cause but how do i make it work in my app. I tested the query in Insomia it works but somehow with setting it up in flutter not working. Have anybody experience on that. I would be very happy for anykind of help.

TL;DR
User is experiencing a type error when using GraphQL in their Flutter app. The error occurs when using a post request in Insomnia and a get request in Flutter. They have tried setting the content type and request headers to match in both Insomnia and Flutter. They have also attempted to debug the package code but were unable to determine the issue. The user is using the ferry client for GraphQL requests in their Flutter app. They have set up the httpLink according to the documentation but are receiving a type error. The error is occurring in the `gql_http_link/src/link.dart` file. The user has tested the query in Insomnia and
Drake
3 Jan, 2024, 18:48

and what's being returned? what's the output of json.decode()?

Faisal7
3 Jan, 2024, 22:05

ok i somehow was not able to debug the package code to see whats in json.decode() is so i added a customHttpResponseDecoder in HttpLink like this static final Link _httpLink = HttpLink( FlavorConfig.instance.variables["apiUrl"], defaultHeaders: <String, String>{ "X-Appwrite-Project": Env.appwriteProjectId }, httpResponseDecoder: _customHttpResponseDecoder, ); and the content of _customHttpResponseDecoder is ``` static Map<String, dynamic>? _customHttpResponseDecoder(http.Response httpResponse) { try { final decodedBody = json.decode(utf8.decode(httpResponse.bodyBytes));

TypeScript
  if (decodedBody is Map<String, dynamic>?) {
    // Handle the case where the response is a Map
    return decodedBody;
  } else if (decodedBody is List<dynamic>) {
    // Handle the case where the response is a List
    // You may need to process the list elements individually
    if (decodedBody.isEmpty) {
      return {};
    }
    return {'data': decodedBody.first};
  }
} catch (error) {
  print('Error decoding response: $error');
}
return null; // Return null in case of decoding errors

}``` and the responce looks like this

Faisal7
3 Jan, 2024, 22:07
Drake
3 Jan, 2024, 22:08

interesting...were you able to make the same GraphQL some other way? maybe with the Appwrite SDK?

Drake
3 Jan, 2024, 22:08

or manually

Faisal7
3 Jan, 2024, 22:09

yes i do in insomia its working

Drake
3 Jan, 2024, 22:09

what was the response in insomnia?

Faisal7
3 Jan, 2024, 22:09

i get normal data from my collection

Drake
3 Jan, 2024, 22:10

would you please share the request and response using insomnia? And can you see what the request looks like in Flutter?

Drake
3 Jan, 2024, 22:11

and have you tried using the Appwrite SDK to make the GraphQL call?

ideclon
3 Jan, 2024, 22:13

The SDKs support GraphQL?!

Faisal7
3 Jan, 2024, 22:15

i dont know eaither but no i didnt try it

Faisal7
3 Jan, 2024, 22:32

ok i checked the request seems to be included everything same as insomia

Faisal7
4 Jan, 2024, 10:31

after a little investigation i found out that when i set the request type to get in insomia i get this error and the same error i get in my flutter app when i use post but not getting this error in insomia when i set the request to post in insomia and i dont get the other null type error anymore after setting the content type to defaultHeaders: <String, String>{ "X-Appwrite-Project": Env.appwriteProjectId, "Content-Type":"application/x-www-form-urlencoded" }, this.

Faisal7
4 Jan, 2024, 10:32

so i dont know what is happening.

Faisal7
6 Jan, 2024, 22:15

yah exactly i alread looked into it and didnt found anything more then in the screenshots their is also the same stuff.

Drake
6 Jan, 2024, 22:15

Can you share?

Faisal7
6 Jan, 2024, 22:17

yes of course here the request part

Faisal7
6 Jan, 2024, 22:17
Faisal7
6 Jan, 2024, 22:18

but is the query part looking normal?

Faisal7
6 Jan, 2024, 22:21

i mean i cannot see the query string fully.

Drake
6 Jan, 2024, 22:36

Shouldn't there be more to the query?

Drake
6 Jan, 2024, 22:37

It might be getting cut off?

Faisal7
6 Jan, 2024, 22:39

yes i think so as well. If i send the query like this i would definately get an error.

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