question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird query lifecycle transitions

See original GitHub issue

Hey there! I discovered your library yesterday and decided to give it a try because I think its caching mechanism is superior to what graphql-hooks (what I’m currently using) offers.

What I’m doing

To make migration easier, I’m creating the following hook:

import { useGraphQL } from 'graphql-react';

const fetchOptionsOverride = options => {
  options.url = '/api/graphql';
};

const useQuery = (query, { variables } = {}) => {
  const {
    loading,
    cacheValue: { data, fetchError, httpError, parseError, graphQLErrors } = {},
  } = useGraphQL({
    fetchOptionsOverride,
    operation: {
      variables,
      query,
    },
  });

  const error = fetchError || httpError || parseError || graphQLErrors;

  console.log({ loading, error, data }); // <- Remember this call to console.log.

  return {
    loading,
    error,
    data,
  };
};

export default useQuery;

What I expect

Later, in my component I expect to be able to use useQuery like this:

const postQuery = /* GraphQL */ `...`;

const Post = ({ postId }) => {
  const { loading, error, data } = useQuery(postQuery, {
    variables: {
      postId,
    },
  });

  if (loading) {
    return 'Loading...';
  }

  if (error) {
    return 'Something went wrong.';
  }

  return (
    <article>
      <h1>{data.post.title}</h1>
      <p>{data.post.content}</p>
    </article>
  );
};

What actually happens

Blows up when trying to access data.post.title. I traced that down to the different values loading, error and data have during the query lifecycle:

  1. { loading: false, error: undefined, data: undefined }

All three values are falsy at the beginning, thus provoking the issue I just mentioned with my component’s code.

  1. { loading: true, error: undefined, data: undefined }

loading is set to true when load() is called inside useGraphQL.

  1. { loading: false, error: undefined, data: undefined }

When that finishes, loading returns to false, but neither error nor data are set (problematic, if you ask me).

  1. { loading: false, error: undefined, data: {…} }

Finally, data is populated with the result of the GraphQL query.

I would expect this sequence to be like this instead:

  1. { loading: true, error: undefined, data: undefined }
  2. { loading: false, error: undefined, data: {…} }

Am I doing something wrong in my useQuery hook? Is this the expected behavior?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
Grsmtocommented, Jan 28, 2020

Thanks for your quick reply.

internally two useState values are being set, one immediately after the other, which in React can result in two very fast “renders”

Actually as you can see in this codesandbox example, that’s not the case: https://codesandbox.io/s/green-architecture-023dc Doing 2 following setState still will trigger 1 single render with both setState applied at the same time (not one after the other).

Anyway I will try to have a look at the source code of the useGraphQL see if I find something. Good luck with your new contract!

1reaction
olisticcommented, Jun 2, 2019

Yeah, weird. Let me try to reproduce the issue in a codesandbox. If I’m doing something wrong I’ll probably find out then. I’ll keep you posted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transitioning objects using Amazon S3 Lifecycle
Amazon S3 supports the following lifecycle transitions between storage classes using an S3 Lifecycle configuration. You can transition from the following: The ...
Read more >
Strange Behavior with AWS S3 Lifecycle Policy - Stack Overflow
How do I only transition objects greater than 100MB to AWS Glacier from S3 Standard using AWS Lifecycle Management Policies? Hot Network ...
Read more >
How to Add Interactive Animations and Page Transitions to a ...
Each of the animation lifecycle props such as initial and animate allow us to define our animation's name as a variant.
Read more >
Illustrating lifecycle transitions in Amazon S3 - alexwlchan
If you start exploring lifecycle rules, you eventually end up in the Amazon docs, with a diagram that explains what transitions you can...
Read more >
Behavior changes: all apps - Android Developers
Behavior changes: all apps ; Performance. Restricted App Standby Bucket ; Activity lifecycle. Root launcher activities are no longer finished on Back press....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found