Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

How to use the "client" option on the server. #1232

Closed
j opened this issue Oct 12, 2017 · 5 comments
Closed

How to use the "client" option on the server. #1232

j opened this issue Oct 12, 2017 · 5 comments

Comments

@j
Copy link

j commented Oct 12, 2017

See this comment: #1232 (comment)

I noticed when using multiple clients, it's a pain on the server side to pass around client instances per request. This is managed with a single client b/c it's in the context.

A comment suggests to use it like so:

import MyOtherClient from "../other-client"

export default graphql(MyQuery, {
  client: MyOtherClient,
})(MyComponent);

However, on the server, this is a lot harder. You'd have to have a container hoc that stores the extra clients and passes them down as a prop to the graphql wrapped component which seems kind of ugly.

What's a clean way to do this?

@j j changed the title SSR with multiple clients doesn't fetch when using "client" option. SSR with multiple clients doesn't fetch when using "client" option & New proposed way? Oct 12, 2017
@j
Copy link
Author

j commented Oct 26, 2017

Just got back to this issue. What I'm finding a pain is that the graphql hoc re-uses client form the ApolloProvider context. So on server render, it's easy to pass the client + cache down to my wrapped component. The way to get around it is to create my own wrapped component and pass my second client in the context and use recompose or so to add it to the props then use that prop in the client options configuration.. This just seems way harder and more painful to do than just having it be like:

// Store.js
export default graphql(gql(`...`))(Products); // uses default client

// Repositories.js
export default graphql(gql(`...`), {
  options: {
    client: 'github'
  }
})(Repos);

// App.js
<ApolloProvider clients={{ shopify, github }} defaultClient={shopify}>
  <App>
    <Store />
    <Repositories />
  </App>
</ApolloProvider>

In comments, I see this example:

import MyOtherClient from "../other-client"

export default graphql(MyQuery, {
  client: MyOtherClient,
})(MyComponent);

which is great and clean... until you want to do this on the server side. Thoughts?

/cc @jbaxleyiii @flexzuu

@j j changed the title SSR with multiple clients doesn't fetch when using "client" option & New proposed way? How to use the "client" option on the server. Oct 26, 2017
@j
Copy link
Author

j commented Oct 26, 2017

I created this PR to see if this method fixes my SSR issues and it does: #1292

@flexzuu
Copy link
Contributor

flexzuu commented Nov 13, 2017

Hey I just saw your mention so sry for the late reply. At my company we don't use SSR so i have Thoughts to give. But i think the most versatile would be to set the client via props. Then you can add it via a contex hoc or whatever you desire.

@stale
Copy link

stale bot commented Dec 4, 2017

This issue has been automatically labled because it has not had recent activity. If you have not received a response from anyone, please mention the repository maintainer (most likely @jbaxleyiii). It will be closed if no further activity occurs. Thank you for your contributions to React Apollo!

@hwillson
Copy link
Member

Since there hasn't been any recent activity here, and this issue was opened for React Apollo 1.x, we'll close this for now. Please try using a 2.x version of React Apollo and post back if this is still an issue. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants