Changing requests endpoint and headers
It is often necessary to set additional headers on the GraphQL requests or change the endpoint to where the requests are made. By default Relate has the following configuration:
1 | { |
You can change these defaults in two ways: using a reducer factory method or sending them in the initial state of the reducer.
Reducer Factory
This is a helper method to include your reducer, to which you can pass a settings which is merged with the defaults above. To do this, instead of including the reducer in your combine reducers like this:
1 | import {combineReducers} from 'redux'; |
You do it with the reducer factory, like so:
1 | import {combineReducers} from 'redux'; |
In the example above we’re changing the default endpoint but the headers remain the default ones. But if you change the headers it won’t merge with the defaults. So if you do this:
1 | relateReducerInit({ |
Your final configuration will be like this:
1 | { |
Changing Reducer Initial State
You can also define your configuration when creating the Redux store by passing configuration through the initialState
.
1 | const initialState = { |
Note that none of the values are merged with the defaults. So if you only want to change the endpoint you’d have to set the initial state to:
1 | const initialState = { |
Mutating Headers and Endpoint
You also might want to set new headers or change the endpoint for different resources. Relate allows you to modify these using standard Redux actions from anywhere in your application.
Here’s an example of setting an authorization header (for example, setting a bearer token after successfully logging in).
1 | import {setHeader} from 'relate-js'; |
If no longer needed, a header can be removed:
1 | import {removeHeader} from 'relate-js'; |
You can also change the endpoint:
1 | import {setEndpoint} from 'relate-js'; |