GithubHelp home page GithubHelp logo

Comments (5)

Pkmmte avatar Pkmmte commented on May 26, 2024 15

How's the progress on this?

I have a GraphQL endpoint that requires client authentication with every call. We use JSON Web Tokens with a limited life span for authentication so we can't just reuse the same one. Creating a new client for every single request seems rather wasteful, in my opinion.

from graphql-request.

schickling avatar schickling commented on May 26, 2024 6

Hi @TinkGu. Thanks a lot for bringing this up. We'll consider this use case for the next version of graphql-request.

For now, a workaround is to re-instantiate a new GraphQLClient whenever you need to change your headers.

from graphql-request.

Dajust avatar Dajust commented on May 26, 2024

This has been properly done using the new apollo-boost package. See the request option in the configuration section.

from graphql-request.

christopher-caldwell avatar christopher-caldwell commented on May 26, 2024

Is there any way to do this now? I don't want to use Apollo. Is the only way to re-instantiate a client on each request?

Edit: Immediately after posting this I tried:

const client = new GraphQLClient(endPoint, { headers: { Authorization: INITIAL_TOKEN } })

const runQuery = async (query, variables) => {
  client.options.headers = {
    Authorization: NEW_TOKEN,
  }
  try {
    return await client.request(query, variables)
  } catch (error) {
    ...
  }
}

export default runQuery

In my case, I'm referencing a Vuex store, so the property is reactive. You could easily pass, or get the token within this function though.

from graphql-request.

moeHaydar avatar moeHaydar commented on May 26, 2024

@christopher-caldwell be careful with what you are doing.
If you are sharing the client, then you might end up in an unwanted state.

from graphql-request.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.