last day (16 days later) » 

11:46 PM
0
Q: Nested fetch, second one fails - React & GraphQL

pioupiou1211Here is how my app works: I fetch tweets from an API and write them locally in a json file in an Apollo Server. Then, I want to fetch those tweets from the Apollo server using GraphQL. Sadly, it seems that I can't chain a fetch and a refetch (from the Apollo GraphQL query). When I run the code be...

 
There's no reason fetch(...).then(refetch) shouldn't work. Where are you seeing the "Failed to load resource" error? Can you see in your network tab which request is failing? Is the fetch call itself blowing up?
 
@DanielRearden the error appears after the first fetch is done. It's the GraphQL refetch that fails, the first one always go through. It's really weird since it should indeed work, on paper. I see the error in the console.
 
If you comment out the refetch call, does that prevent the error from being thrown? I'm not sure why chaining the promises would matter here, but you are missing a return in front of res.json() as well.
 
@DanielRearden I added the return and the result is the same. If I comment the refetch() there's no error thrown. As I said, I can comment it, submit my form, physically wait 2 seconds and click a button to refetch and it will work. However, if I emulate the JavaScript and click the button as soon as the first fetch is done, the error will be thrown. I don't understand why it needs that delay.
 
Here's a simple sandbox using your code, modified to use some public APIs instead of the ones in your question: codesandbox.io/s/8zn78k1qj0 The refetch works as expected. So whatever the issue is, it's most likely outside this code.
 
11:46 PM
@DanielRearden You're right, I tried with a public Apollo Server and it works. I updated my post. Something must be going on with my server but I can't put the finger on it.
 
So from the screenshot, it looks like the GraphQL request is just flat out failing for some reason. Can you drill in and paste in what you're getting back as the response from server?
 
Here is what I'm getting from the server:

Summary
URL: http://localhost:4000/graphql
Status: —
Source: —

Request
Accept: */*
Content-Type: application/json
Origin: http://localhost:3000
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Safari/605.1.15

Response
No response headers

Request Data
MIME Type: application/json
Request Data:

Not much as you can see. It's like it can't reach the server for some reason while it's still up and running.
 
It's really weird because it works perfectly with my first API and a public Apollo server
 

  last day (16 days later) »