Skip to content

Instantly share code, notes, and snippets.

@Cerberus
Created September 24, 2017 18:16
Show Gist options
  • Save Cerberus/c3fab759b681d14c41bea53f70791d10 to your computer and use it in GitHub Desktop.
Save Cerberus/c3fab759b681d14c41bea53f70791d10 to your computer and use it in GitHub Desktop.
const hasSubscriptionOperation = ({ query: { definitions } }) =>
definitions.some(
({ kind, operation }) =>
kind === 'OperationDefinition' && operation === 'subscription',
)
const link = ApolloLink.split(
hasSubscriptionOperation,
new WebSocketLink({
uri:
'wss://hostname/__path__',
options: { reconnect: true },
}),
new Link({
uri: 'https://hostname/__path__',
}),
)
const client = new ApolloClient({
link,
cache: new Cache(window.__APOLLO_STATE__),
})
ReactDOM.render(
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
document.getElementById('root'),
)
@japrogramer
Copy link

japrogramer commented Dec 14, 2017

Hello I would like to use this pattern, so subscriptions go to websockets, but I also need to pass a header with my request .. could you help me make this .. work with .split(..)

  18  const httpLink = new HttpLink({                                                                                                                                                                                                                                                                                                                                                                                                     
    1   uri: `/graphql`,                                                                                                                                                                                                                                                                                                                                                                                                                  
    2   credentials: 'same-origin',                                                                                                                                                                                                                                                                                                                                                                                                       
    3 });                                                                                                                                                                                                                                                                                                                                                                                                                                 
    4                                                                                                                                                                                                                                                                                                                                                                                                                                     
    5 function filtercookie(name) {                                                                                                                                                                                                                                                                                                                                                                                                       
    6   var filter = new RegExp(name + '=([^;]*)(;|$)');                                                                                                                                                                                                                                                                                                                                                                                  
    7   let cookies = document.cookie;                                                                                                                                                                                                                                                                                                                                                                                                    
    8   let found = cookies.match(filter);                                                                                                                                                                                                                                                                                                                                                                                                
    9   if (found) {                                                                                                                                                                                                                                                                                                                                                                                                                      
   10     return found[1];                                                                                                                                                                                                                                                                                                                                                                                                                
   11   }                                                                                                                                                                                                                                                                                                                                                                                                                                 
   12   return false;                                                                                                                                                                                                                                                                                                                                                                                                                     
   13 }                                                                                                                                                                                                                                                                                                                                                                                                                                   
   14                                                                                                                                                                                                                                                                                                                                                                                                                                     
+  15 // TODO: create a split in the link for websockets https://cdn-images-1.medium.com/max/2000/1*GDMbcu8be9mHdCDgAGi7VA.png                                                                                                                                                                                                                                                                                                            
+  16 // https://dev-blog.apollodata.com/apollo-client-2-0-beyond-graphql-apis-888807b53afe                                                                                                                                                                                                                                                                                                                                               
+  17                                                                                                                                                                                                                                                                                                                                                                                                                                     
   18 const csrfMiddleware = new ApolloLink((operation, forward) => {                                                                                                                                                                                                                                                                                                                                                                     
   19   // add the authorization to the headers                                                                                                                                                                                                                                                                                                                                                                                           
   20   operation.setContext({                                                                                                                                                                                                                                                                                                                                                                                                            
   21     headers: {                                                                                                                                                                                                                                                                                                                                                                                                                      
   22       'X-CSRFTOKEN': filtercookie('csrftoken') ? filtercookie('csrftoken') : null,                                                                                                                                                                                                                                                                                                                                                  
   23     },                                                                                                                                                                                                                                                                                                                                                                                                                              
   24   });                                                                                                                                                                                                                                                                                                                                                                                                                               
   25                                                                                                                                                                                                                                                                                                                                                                                                                                     
   26   return forward(operation);                                                                                                                                                                                                                                                                                                                                                                                                        
   27 });                                                                                                                                                                                                                                                                                                                                                                                                                                 
   28                                                                                                                                                                                                                                                                                                                                                                                                                                     
   29 export const client = new ApolloClient({                                                                                                                                                                                                                                                                                                                                                                                            
   30   link: concat(csrfMiddleware, httpLink),                                                                                                                                                                                                                                                                                                                                                                                           
   31   cache: new InMemoryCache().restore(window.__APOLLO_STATE__),                                                                                                                                                                                                                                                                                                                                                                      
   32 });

edit: Oh, I figured it out :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment