-
Notifications
You must be signed in to change notification settings - Fork 28
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
Allow customization of GraphQL fetch #42
Comments
Simply changing const fetchGraphQL = (graphQLUrl, url, credentials, app, query) => {
const mSearchBody = JSON.stringify(query.map(item => JSON.stringify(item)));
const body = JSON.stringify({
query: `{ elastic72 { msearch ( index: "${app}", body: ${mSearchBody} ) } }`
});
return fetch(graphQLUrl, {
method: 'POST',
body,
headers: {
'Content-Type': 'application/json',
},
})
.then(res => res.json())
.then(jsonRes => jsonRes.data.elastic72.msearch)
.catch((error) => {
console.error(error);
});
};
export default fetchGraphQL; I would need to make use of the host url and credentials parameter and see if they were provided, if yes build a url that should be provided as host parameter to the query in order to include all functionality. Would be interesting to see if the GraphQL proxy you are using would also accept the query in this encoding, if yes I could prepare a PR. |
I had the same issue. Forking was not an ideal solution since there are multiple packages involved and keeping in sync would have been painful. I use apollo-server as a backend. So here is what I did: I used xspy module which allows to modify requests before they are sent, and did this : import xspy from 'xspy';
xspy.onRequest(request => {
// Intercept the good request.
if (
request.ajaxType === 'xhr' &&
request.method === 'POST' &&
request.headers['content-type'] !== 'application/json' &&
request.body.includes('elastic50')
) {
request.headers.accept = '*/*';
request.headers['content-type'] = 'application/json';
// Here you could inject authorization headers.
request.headers.authorization = `Bearer ${window.localStorage.getItem(
'token'
)}`;
request.body = JSON.stringify({
// My field is named "elastic" so we rename it here, and strip host argument.
query: request.body.replace(/elastic50\(.*\)/, 'elastic50:elastic')
});
}
}); |
The GraphQL fetch should allow for more custumization:
Currently the fetch call is fixed to query
elastic50
, feels weird to have to use elastic50 as my query name even if I'm using 7.2enforces the usage of a
host
parameter(which I dont use as the graphql proxy takes care of the host + credentials, therefore resulting in an error for me)Uses content type
application/graphql
while lots of implementations like Apollo seem to preferapplication/json
ReactiveBase could take a transformRequest parameter, which seems to be ignored in case
graphQLUrl
is used.The text was updated successfully, but these errors were encountered: