Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overcome endless looping when executing useQuery (ApolloClient) by defining a new client

I need to define "client" when running "useQuery", but I get endless looping.

I wrote the code as below:

const QueryKTP = gql`
  query {
    documents(transactionId:"${transactionId}", input: [
      {documentType:"KTP"}
    ]){
      documentResponses{
        documentType
        documentBase64
      }
      responseDescription
      responseCode
     message
    }
  }`

const anotherClient = new ApolloClient({
  uri: "https://my-url/online-service/graphql"
});

const { data, loading } = useQuery(QueryKTP, {client: anotherClient});

If I change the script above to be like below (remove new client), looping no longer occurs.

const { data, loading } = useQuery(QueryKTP);

What do I need to fix? Thank you

like image 429
Wisnu Avatar asked Oct 29 '25 08:10

Wisnu


1 Answers

Finally, I've figured out. I faced the same problem and I solved it by excluding new ApolloClient from render function.

Actually, I don't see render function from your code, but in my case, it was something like this:

Before

export default function MyComponent () {
  const anotherClient = new ApolloClient({
    uri: "https://my-url/online-service/graphql"
  });
  const { data, loading } = useQuery(QueryKTP, {client: anotherClient});
}

After

const anotherClient = new ApolloClient({
  uri: "https://my-url/online-service/graphql"
});
export default function MyComponent () {
  const { data, loading } = useQuery(QueryKTP, {client: anotherClient});
}

In my case, it helped. You should know, that in similar cases just look to new keyword. For example, guys often meet the same bug with an infinite loop, when they use new Date() in the render function

like image 58
Kamil Ismagilov Avatar answered Oct 30 '25 23:10

Kamil Ismagilov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!