Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to pass headers argument as function #19

Open
hedselu opened this issue Sep 11, 2024 · 0 comments
Open

Possibility to pass headers argument as function #19

hedselu opened this issue Sep 11, 2024 · 0 comments

Comments

@hedselu
Copy link

hedselu commented Sep 11, 2024

First of all, thanks for creating this repository.

In the project I'm working on I'd like to switch from passing an Auth Token as URL parameter to header. Currently we keep the client's initialisation in custom React Hook that returns both client and setter function, so that we can set the token from outside.
This works due to nature of createConsumer - it accepts both a value or a function (here, websocketUrl) that is being lazily evaluated, when the token is already granted. Changing to header-based authentication won't work currently, as headers require immediate object value.

export default function useCreateApolloClient {
   const tokenRef = useRef(null)
   const [token, setToken] = useState(tokenRef.current)
   // ...

   if (!connection.current) {
    connection.current = createClientWithAuth(tokenRef)
   }

   return [connection.current.client, setToken]
  }


function createClientWithAuth(tokenRef) {
  const actionCable = ActionCable.createConsumer(websocketUrl(tokenRef))
  // ...
  return { client, actionCable }
}

const websocketUrl = (tokenRef) => () =>
  tokenRef.current ? `${URL}?token=${tokenRef.current}` : URL

I'm not that familiar with React philosophy and it might sound like specific use case, but would it be valuable (especially for React Hooks) to also have a possibility to lazily evaluate headers same way URL argument does?

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

No branches or pull requests

1 participant