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
Demonstrate Apollo-Server context usage with-apollo-xxx examples #10413
Comments
cc @adamsoffer @lfades I believe you guys can contribute something useful to this discussion. |
I chose to review and approve PRs to update the examples, I've been disconnected from Apollo for a while now, and because it has been changing so much, the community is the one deciding the state of the examples. If the approach you're mentioning is better and the proof is there, you're welcome to create a PR with the changes. |
I was just finding this same issue with the apollo Auth example. Context will likely want more than just the standard ctx - for example, id like to check JWT authentication token from the cookie in the context function and apply user ID to the context or throw an auth error, so i would recommend always using a context function rather than only when empty, the cache sounds correct though.
|
@glenarama your code won't work by the reasons I described above. I'm pretty sure you didn't completely understand my post. Please read Two calls section thoroughly.
This is exactly the reason I propose tne above. |
I should have noted that the getContext returns an empty object if no req is passed, this works fine for me.
|
In that case we're on the same page! |
@ivan-kleshnin I fixed your issue ( Since the example currently doesn't implement a server schema I never added |
Hi, just to give feedback we use Having worked with isomorphic code and SSR for a while, we now tend to consider calls like This is because server-side code tend to diverge slowly from client-side code, and it becomes unmanageable in an app that is multiple years old (unclear bundling process, An important step is to unify context between REST calls (here the API routes), graphQL calls server side, and graphQL calls client-side. For some reason using a context in queries is not that common in traditional Node app. As far as I understand that's the point of this issue. |
Btw I do not agree with the "good-first-issue" tag, this is trickier than it seems and raises questions about state-of-the-art patterns like isomorphic code. There is no clear consensus about how to manage such issues. |
…ons/next (#42771) Closes #42769 ## Description This PR address #42769 by updating the `api-routes-apollo-server`, `api-routes-apollo-server-and-client` and `api-routes-apollo-server-and-client-auth` examples to use Apollo Server 4 and [@as-integrations/next](https://github.com/apollo-server-integrations/apollo-server-integration-next), which is the Apollo Server Next integration package. The PR also updates the three examples to use Typescript. The functionality of the examples is the same. ## Documentation / Examples - [X] Make sure the linting passes by running `pnpm build && pnpm lint` - [X] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md) closes #33545 closes #30082 closes #21984 closes #10413
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Feature request
Current
with-apollo-
examples do not demonstrate how to use Apollo-Server withcontext
resolver. It doesn't work out of the box and it's not at all straightforward for Apollo newbies (like myself) how to enable it.Note: this is a request for help/consulting for Apollo experts.
1.
createIsomorphLink
createIsomorphLink
implementations recommended by Apollo Team imply the use ofSchemaLink
on server andHttpLink
on client.SchemaLink
is said to be more performantbecause it avoids HTTP layer alltogether:
2.
SchemaLink
SchemaLink
does not initiate an HTTP request. The side effect is that Apollo-Servercontext
function is never called. This function is crucial as it usually fetches or polyfills all common resolver's data including current user/visitor, etc. Currentwith-apollo-
examples demonstrate how to read user data in local (leaf) resolvers which is not production-like.Luckily,
SchemaLink
accepts acontext
argument where we can provide the samecontext
resolver as to thenew ApolloServer({context: ...})
.3. Two calls
Now the tricky part is that
createIsomorphLink
is called twice. Once with{req, res}
context data (coming fromgetInitialProps
) and another – with no context (coming from these:lines).
context
function of Apollo-Server must not be called with empty context as it, in most cases, readsctx.req
.The second invocation of Apollo-Client will reuse cache so resolvers, including
context
won't be called. It seems to me that an update like:should be enough to enable Server's
context
usage. Given thatapollo/schema.js
exports standalonecontext
function of course.I can make a PR but, before that, it would be great to get a feedback from someone more experienced with Apollo and NextJS.
Are my reasoning correct? Is my proposal valid?
Another option it to always use
HttpLink
like it's done in https://github.com/adamsoffer/next-apollo and https://github.com/lfades/next-with-apollo For some reason the authors of both packages decided to not rely onSchemaLink
...The text was updated successfully, but these errors were encountered: