apollographql / apollographql/federation
RemoteGraphQLDataSource: allow per-request control over url, method
- Dominant language
- TypeScript
- Stars
- 725
- Forks
- 276
- Avg merge
- 1h 47m
- Merged PRs (30d)
- 1
Description
@Cretezy filed https://github.com/apollographql/apollo-server/pull/5514 on the Apollo Server repo to make the `url` and `method` fields of the `Request` object mutable, so that RemoteGraphQLDataSource could set the url and method per-request based on context.
However, that would move our typings away from the Fetch API's typings, which sounds like the wrong direction.
Instead, let's just give subclassers of RemoteGraphQLDataSource the ability to customize Request construction.
eg, we could extract out
```
const headers = (request.http && request.http.headers) || new Headers();
headers.set('Content-Type', 'application/json');
request.http = {
method: 'POST',
url: this.url,
headers,
};
```
into something like
```
request.http = this.makeRequest({ request, context });
```
which you can override as you'd like.
(Note that #870 adds an option to `process` and we should probably just pass the full argument to `process` to this new hook.)
While we're at it, the code above has some weird properties. For one thing, it's odd that we *mutate* the incoming `headers` even though we otherwise copy stuff. Also, we ignore everything on `request.http` other than `headers`... even though `getServiceDefinitionsFromRemoteEndpoint` actually passes in an url and a method, and the executeQueryPlan.ts invocation has to use `http: any` to mask the fact that we're *not* sending in url and method.
This seems like good fodder for an externally contributed PR.
Contributor guide
Research direction
Start at RemoteGraphQLDataSource's request construction and inspect how getServiceDefinitionsFromRemoteEndpoint supplies the URL and method. Trace the invocation in executeQueryPlan.ts and the arguments passed to process. Done means subclassers can customize request construction per request without discarding supplied headers, URL, or method.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100