feat: add operationName to payload if defined in gql #280
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We use the operation name for tracking purposes on our API. However, as already discussed here: #64, the operationName is currently not send.
In contrast to issue #64, which is about allowing to define an operationName when sending multiple queries at once, this is about sending the
operationName
field in the payload sent to the API for "regular" queries/mutations like this:Where I want the server to use
allUsers
for tracking purposes. When we send theoperationName
field in our payload, like e.g. Apollo or some other GraphQL clients do, it can easily be used by GraphQL server implementations. This should be a completely backwards compatible change.I tried to build this in a completely backwards-compatible way by extracting the actual fetch call from
rawRequest
into a privatemakeRequest
function, which is called by bothrawRequest
andrequest
. This way, we can pass in the (optional)operationName
extracted from thequery
, if possible, and use that, without changing the signature of any public method.This will basically only do anything if sending a single operation with an operation name present. Otherwise, nothing will change. If not passing a DocumentNode as
query
, but just a string, it will also do nothing.I also moved the header-normalization out from the post/get methods to the request methods, to keep the get/post methods as minimal as possible.