Apollo-link: Let the user of apollo-link-batch-http decide which request to batch

Created on 7 Jan 2018  ·  2Comments  ·  Source: apollographql/apollo-link

When using _apollo-link-batch-http_ all request in a specific time interval are called in batch. In our application most of the request are fast but we also we have some time consuming requests. Batching known slow request with other would result in a poorer user experience, as now all request have the same response time as the slowest one. That's why it would be nice if there would be a callback that can decide if a request should be batch ar fired immediately.

Most helpful comment

You can do it with split
__links.js__

const batchHttpLink = new BatchHttpLink({
  fetch: createApolloFetch({
    constructOptions,
    uri: process.env.REACT_APP_API_ENDPOINT,
  }),
})

const normalHttpLink = createHttpLink({
  uri: '/graphql',
  credentials: 'same-origin',
})

export split(({getContext}) => getContext().noBatch, normalHttpLink, batchHttpLink)

Make a query not use batch link

const ProfileWithData = graphql(CurrentUserForLayout, {
  options: { 
    context: { noBatch: true }
  },
})(Profile);

Here I use noBatch in context to control whether a query should be batched. By default all queries will be batched.

All 2 comments

You can do it with split
__links.js__

const batchHttpLink = new BatchHttpLink({
  fetch: createApolloFetch({
    constructOptions,
    uri: process.env.REACT_APP_API_ENDPOINT,
  }),
})

const normalHttpLink = createHttpLink({
  uri: '/graphql',
  credentials: 'same-origin',
})

export split(({getContext}) => getContext().noBatch, normalHttpLink, batchHttpLink)

Make a query not use batch link

const ProfileWithData = graphql(CurrentUserForLayout, {
  options: { 
    context: { noBatch: true }
  },
})(Profile);

Here I use noBatch in context to control whether a query should be batched. By default all queries will be batched.

Thank you for the example @charleyw - I will go ahead and close this since the question seems to have been answered.

Was this page helpful?
0 / 5 - 0 ratings