onCompleted of Apollo’s useLazyQuery does not get called when the execute function resolves

onCompleted of Apollo’s useLazyQuery does not get called when the execute function resolves


0

I am facing a problem with the following snippet:

const [execute] = useLazyQuery(gql`
  query Example($id: String!) {
    node(id: $id) {
      ... on Train {
        id
        passengers(first: 1) {
          totalCount
        }
      }
    }
  }
`)

useEffect(() => {
  execute({ onCompleted: () => console.log("Hello world") })
}, [])
interface Node {
  id: ID!
}

type PassengerConnection {
  totalCount: Int!
}

type Train {
  id: ID!
  passengers(first: Int): PassengerConnection!
}

type Query {
  node(id: String!): Node
}

Observing the network tab tells that the server do return a response.
The execute function indeed returns a promise and this promise resolves that response.

But onCompleted is not called at any circumstances

Expect

onCompleted gets run

What I’ve been doing

result.data &&
previousResult?.networkStatus !== result.networkStatus &&
result.networkStatus === NetworkStatus.ready

The above snippet is used by Apollo to evaluate if it will run onCompleted.
Using the debug tool, I’ve gotten to know that it will be called 3 times, the first time with { loading: true, networkStatus: 1 }, the second time with { loading: false, networkStatus: 7 } and the third time with { data: {...}, loading: false, networkStatus: 7 } while theoretically it should’ve been called 2 times

Please be mindful that adding notifyOnNetworkStatusChange does help 🙁

I’d like to ask what might be the cause to this
Thank you

New contributor

hieupham is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *