question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set headers after client creation, or per request?

See original GitHub issue

Hope this is not too much of an edge case:

Trying to use Villus in a Vuex store module in a Nuxt app.

This works, if I do not need to pass Authorization headers:

// myStoreModule.js
import { createClient } from 'villus'

const client = createClient({
  url: 'http://localhost:4000/graphql'
})
// ...
export const actions = {
  async fetchData({ commit, dispatch }) {
    const data = await client.executeQuery({
      query: BOARD_QUERY
    })
    commit('SET_BOARD', data)
  }
}

However, I do need to pass Authorization headers. But in the Vuex store, I can only access the original headers from the http.request object inside of actions. Therefore, I cannot do:

const client = createClient({
  url: 'http://localhost:4000/graphql',
  context: () => {
    return {
      fetchOptions: {
        headers: {
          Authorization: TOKEN
        }
      }
    };
  }
})

…because I do not have access to TOKEN outside of my actions.

Therefore, I would like to do something like this:

export const actions = {
  async fetchData({ commit, dispatch }, { req }) {
	// first get TOKEN from original request
    const token = getToken(req.headers)
	// the set the token to the previously created client
    client.context = () => {
      return {
        fetchOptions: {
          headers: {
            Authorization: TOKEN
          }
        }
      }
    }
	//...
}

But this does not work. So, how would you go around this issue?

With Axios, I could easily modify the headers on each request. Would something like that be possible with Villus client?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
logaretmcommented, Jan 17, 2020

Your method should work, I’m not sure about the problem. I’m overhauling the extension API, I haven’t settled yet on the final draft but It looks like this:

client.beforeFetch(({ headers }) => {
  headers.Authorization = 'Bearer ....';
});

client.afterFetch(({ response }) => {
  // Do something with the response.
});

My goals are to allow retries, caching, error handling with simple API like that.

1reaction
altitudemscommented, Feb 11, 2020

I would like to add to this request by asking that the context function support promises. This way we could first get an access token asynchronously, and then update headers and execute the query.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Can't set headers after they are sent to the client
When a POST request is sent to /api/route1 it will run every line in the callback. A Can't set headers after they are...
Read more >
Cannot set headers after they are sent to client [SOLVED]
The primary cause of the error, "Error: Cannot set headers after they are sent to client" is sending multiple HTTP responses per request....
Read more >
Using Axios to set request headers
In this article, we examined how to set HTTP request headers with Axios by passing an object, creating a specific Axios instance, and...
Read more >
Request Options - Guzzle Documentation
You can customize requests created and transferred by a client using request options. Request options control various aspects of a request including, headers, ......
Read more >
cannot set headers after they are sent to the client nextjs
HTTP uses a cycle that requires one response per request. When the client sends a request (e.g. POST or GET) the server should...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found