zino-hofmann / graphql-flutter

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package.
https://zino-hofmann.github.io/graphql-flutter
MIT License
3.24k stars 612 forks source link

Loop happening with useQuery hook #1289

Closed sandrocsimas closed 1 year ago

sandrocsimas commented 1 year ago

I'm trying to use GraphQL Flutter and I have the following code. This should be a very simple use case where I query a list of posts from my server. In this example I'm not displaying them, but I'm printing the result. Something very weird is happening because the build function runs indefinitely.

class Home extends HookWidget {
  const Home({super.key});

  @override
  Widget build(BuildContext context) {
    QueryHookResult<Posts> listPostsQuery = useQuery<Posts>(
      QueryOptions<Posts>(
        document: gql('''
          query ListPosts {
            posts {
              id
              author {
                id
                firstName
                lastName
                username
              }
              authorId
              text
              likesCount
              commentsCount
              createdAt
            }
          }
        '''),
        fetchPolicy: FetchPolicy.networkOnly,
        parserFn: (Map<String, dynamic> data) => Posts.fromJson(data),
      )
    );

    print("--------------------------------------");
    print(listPostsQuery.result.data);
    print(listPostsQuery.result.isLoading);
    print(listPostsQuery.result.hasException);
    print(listPostsQuery.result.exception);
    print("--------------------------------------");

    return Scaffold(
      appBar: AppBar(
        title: const Text('App'),
      ),
      body: Container(
        color: Colors.blueGrey.shade100,
        child: const Center(
          child: Padding(
            padding: EdgeInsets.all(10),
            child: CircularProgressIndicator(),
          ),
        ),
      ),
      bottomNavigationBar: BottomAppBar(
        shape: const CircularNotchedRectangle(),
        child: Container(height: 50),
      ),
    );
  }
}

This is printing the following:

I/flutter (23045): {__typename: Query, posts: [{__typename: Post, id: 2d9e1dfa-de65-4ce7-8371-2e4c731a2a23, author: {__typename: User, id: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, firstName: Sandro, lastName: Simas, username: null}, authorId: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, text: This is my first post!, likesCount: 0, commentsCount: 11, createdAt: 2023-01-22T15:06:40.284Z}]}
I/flutter (23045): false
I/flutter (23045): false
I/flutter (23045): null
I/flutter (23045): --------------------------------------
I/flutter (23045): --------------------------------------
I/flutter (23045): null
I/flutter (23045): true
I/flutter (23045): false
I/flutter (23045): null
I/flutter (23045): --------------------------------------
I/flutter (23045): --------------------------------------
I/flutter (23045): null
I/flutter (23045): true
I/flutter (23045): false
I/flutter (23045): null
I/flutter (23045): --------------------------------------
I/flutter (23045): --------------------------------------
I/flutter (23045): {__typename: Query, posts: [{__typename: Post, id: 2d9e1dfa-de65-4ce7-8371-2e4c731a2a23, author: {__typename: User, id: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, firstName: Sandro, lastName: Simas, username: null}, authorId: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, text: This is my first post!, likesCount: 0, commentsCount: 11, createdAt: 2023-01-22T15:06:40.284Z}]}
I/flutter (23045): false
I/flutter (23045): false
I/flutter (23045): null
I/flutter (23045): --------------------------------------
I/flutter (23045): --------------------------------------
I/flutter (23045): {__typename: Query, posts: [{__typename: Post, id: 2d9e1dfa-de65-4ce7-8371-2e4c731a2a23, author: {__typename: User, id: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, firstName: Sandro, lastName: Simas, username: null}, authorId: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, text: This is my first post!, likesCount: 0, commentsCount: 11, createdAt: 2023-01-22T15:06:40.284Z}]}
I/flutter (23045): false
I/flutter (23045): false
I/flutter (23045): null
I/flutter (23045): --------------------------------------
I/flutter (23045): --------------------------------------
I/flutter (23045): null
I/flutter (23045): true
I/flutter (23045): false
I/flutter (23045): null
I/flutter (23045): --------------------------------------
I/flutter (23045): --------------------------------------
I/flutter (23045): {__typename: Query, posts: [{__typename: Post, id: 2d9e1dfa-de65-4ce7-8371-2e4c731a2a23, author: {__typename: User, id: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, firstName: Sandro, lastName: Simas, username: null}, authorId: 7c8e8d4f-e0c6-42e8-b28d-f34c31ac0d47, text: This is my first post!, likesCount: 0, commentsCount: 11, createdAt: 2023-01-22T15:06:40.284Z}]}
I/flutter (23045): false
I/flutter (23045): false
I/flutter (23045): null
I/flutter (23045): --------------------------------------

As you can see, the posts are returned correctly and no errors seems to be happening. I'm using the latest version of the libraries:

flutter_hooks: 0.18.5+1
graphql_flutter: 5.1.2

Do you have any idea why this is happening?

sl1mpshady commented 7 months ago

how did you fixed this @sandrocsimas ?

sandrocsimas commented 7 months ago

Sorry, @sl1mpshady, I don't remember how I fixed that. I think I fixed it, but in the end, I decided to use infinite_scroll_pagination for pagination.