-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@rest
Directive doesn't work on SSR environment
#182
Comments
A little more digging, I'm just trying to execute the query in NodeJS, without worrieng about SSR etc, and have this code: client
.query({query: Site, variables: {domain: "demo.example.com"}})
.then(
() => console.log("Fullfilled"),
({graphQLErrors, networkError, message, extraInfo}) =>
console.error("Rejected", graphQLErrors, networkError, message,extraInfo)
) With this result:
export const concatHeadersMergePolicy: RestLink.HeadersMergePolicy = (
...headerGroups: Headers[]
): Headers => {
return headerGroups.reduce((accumulator, current) => {
if (!current) {
return accumulator;
}
if (!current.forEach) {
current = normalizeHeaders(current);
}
current.forEach((value, key) => { // <-- This line here
accumulator.append(key, value);
});
return accumulator;
}, new Headers());
}; https://github.com/apollographql/apollo-link-rest/blob/master/src/restLink.ts#L671 |
Also, if I replace the Object.keys(current).forEach(key => {
accumulator.append(key, current[key])
}) Everything works again... The challenge, I dont understand the purpose of the lines above:
Since this would always fail, and always execute |
I'm going to go out on a limb here and guess that Node is getting upset about the use of Your Next step debugging wise...what is the result of As far as I'm aware, I don't believe SSR has been tested fully (see #155) but @fbartho can probably shed more light on this. |
SSR is not a directly tested or supported feature at this time. Rationale: if you have SSR, you can deploy your own Apollo GraphQL server, and you don’t need link-rest Some people have figured it out however. And I’m not objecting to PRs that improve the state of the world there. It seems to me that we maybe just need an SSR tutorial. |
Additionally, it’s relatively common to need to polyfill Headers depending on your runtime environment. Have you tried that yet? @Richard87 @tombarton’s response is correct: the purpose of that code is to convert header hashes into the appropriate Headers class. |
Hi all! Thanks for the attention to my problem ;) The bottom part of my if (global.Headers == null) {
global.Headers = require("fetch-headers");
}
// Set up babel to do its thing... env for the latest toys, react-app for CRA
// Notice three plugins: the first two allow us to use import rather than require, the third is for code splitting
// Polyfill is required for Babel 7, polyfill includes a custom regenerator runtime and core-js
require('@babel/polyfill');
require('@babel/register')({
ignore: [/\/(build|node_modules)\//],
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-instanceof',
]
});
// Now that the nonsense is over... load up the server entry point
require('./server'); So I'm pretty sure I have the Header polyfill (also without the I already have a GraphQL server running tighly integrated in my PHP Application (API-Platform + https://webonyx.github.io/graphql-php/) So I don't want to install an extra Apollo GraphQL Server, I just want to load the React app faster :D I have a hard time debugging restLink.ts, but I have put in a few strategic Edit...
Prints out "Header" twice...
|
My PHPStorm tells me const Headers = require("fetch-headers");
const headers = new Headers();
console.log(headers)
console.log(headers.forEach)
console.log(!headers.forEach)
headers.forEach(h => console.log(h)) Output:
|
Adding this in my bootstrap file solved all my problems :D if (global.Headers == null) {
const fetch = require('node-fetch');
global.Headers = fetch.Headers;
} notice using |
This should be added to docs of apollo-link-rest . To which file should I raise PR? https://github.com/apollographql/apollo-link-rest/blob/master/README.md , https://github.com/apollographql/apollo-link-rest/blob/master/docs/rest.md or https://github.com/apollographql/apollo-link/blob/master/docs/source/links/rest.md ? |
Hi!
I have a
@rest
graphql query that works perfectly in development/browser, but when I run the same code on the server (SSR), it fails with a long cryptic error message.(if I disable the
@rest
directive, everything else works flawlessly, both in browser and in SSR).The stack trace:
Please advice :/
The text was updated successfully, but these errors were encountered: