apollographql / apollo-link-rest

Use existing REST endpoints with GraphQL
MIT License
791 stars 122 forks source link

SSR calling Rest on server and then again on the client #98

Open tomevans18 opened 6 years ago

tomevans18 commented 6 years ago

Hi all,

I'm new to Apollo, currently setting up my first project with a REST endpoint (as GraphQL isn't available at present)

Initially, I set this up with a GraphQL endpoint as a test, with SSR and persistent state. All worked correctly, with no requests made on the client side for the initial load (with content served initially via SSR).

When I introduced the REST endpoint I noticed that there is also network request to the REST endpoint on the client side. When I blocked this request the content still loaded as it was being served via the SSR.

For this reason, I am asking if this is actually a bug with the SSR option not being passed from Apollo client or if this is simply a setup issue. It could even be the expected behaviour due to it being a 3rd party REST URL but I'm unsure.

Thank in advance and any help would be appreciated.

// Setup REST
const restLink = new RestLink({
    uri: REST, // Server URL (must be absolute)
    credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers`
});

// Setup GraphQL
const httpLink = new HttpLink({
    uri: GRAPH_QL, // Server URL (must be absolute)
    credentials: 'same-origin' // Additional fetch() options like `credentials` or `headers`
});

// Combine the client state, REST and http end point to a single link
const link = ApolloLink.from([
    stateLink,
    restLink,
    httpLink
]);

// Apollo client setup
new ApolloClient({
        connectToDevTools: process.browser, // If not SSR then enable dev tools
        ssrMode: !process.browser, // Disables forceFetch on the server (so queries are only run once)
        cache,
        link
})
tomevans18 commented 6 years ago

So I have been digging into this a little more and wanted to update the ticket.

I have my project setup for SSR and I can see that the REST request isn't handled server side as it isn't part of the apollo.cache. Meaning when I run apollo.cache.extract() there is no data there.

I'm now trying to find a way to add this in on the server side and then disable the REST api request on the client when the data is already served via the server.

fbartho commented 6 years ago

I'm sorry I hadn't commented yet, I've been tangled up at work. -- I'm not currently doing SSR in my stuff, but I totally support your efforts in resolving this issue.

fbartho commented 2 years ago

Is this still a concern? Apollo Link Rest may need more SSR work