-
Notifications
You must be signed in to change notification settings - Fork 23
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
Update RESTDataSource
and HTTPCache
for Apollo Server v4
#1
Conversation
} | ||
|
||
export interface RequestWithBody extends Omit<RequestOptions, 'body'> { | ||
method?: 'POST' | 'PUT' | 'PATCH' | 'DELETE'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think DELETE should allow a body
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's actually legit-ish https://stackoverflow.com/questions/299628/is-an-entity-body-allowed-for-an-http-delete-request
@@ -89,13 +86,14 @@ export abstract class RESTDataSource<TContext = any> extends DataSource { | |||
} | |||
|
|||
protected cacheOptionsFor?( | |||
response: Response, | |||
request: Request, | |||
url: string, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this sometimes a string and sometimes an URL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know what you think: bed450e
I think this mostly comes down to convenience and signature matching. As things get closer to the Fetcher
, using a string
is more natural.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Just thinking that for protected (overridable) methods it makes sense to be consistent rather than minimalist.
src/RESTDataSource.ts
Outdated
...request, | ||
body: undefined, | ||
}; | ||
if (!(modifiedRequest.params instanceof URLSearchParams)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two things maybe could use a comment saying "for convenience, make sure that the request always has params and headers so it's easy to add to it" or something (if that's the point?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. This took me down a little rabbit hole, curious what you think.
b25c880
src/RESTDataSource.ts
Outdated
|
||
protected resolveURL(request: RequestOptions): ValueOrPromise<URL> { | ||
let path = request.path; | ||
protected resolveURL(path: string): ValueOrPromise<URL> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is protected so maybe keeping the full RequestOptions is a good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you looking to not break the existing API here or just ensure the same information that was previously available continue to be? path
isn't on RequestOptions
anymore. I can merge to make a single object (preserve existing API), which I've kind of avoided doing throughout / in other places.
Here's what I have (which just ensures previous information continues to be available)
352c298
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latter (for subclasses), so that change looks good.
type URLSearchParamsInit = ConstructorParameters<typeof URLSearchParams>[0]; | ||
|
||
export type RequestOptions = FetcherRequestInit & { | ||
params?: URLSearchParamsInit; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to be clear: this is not "a feature of Fetch that we haven't bothered to mention in Fetcher
", but a feature of RESTDataSource itself, right?
It's a bit weird to mix "things that come from the Fetch spec" and "our own stuff" but I guess that's fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's correct, though kind of surprising as I look more closely.
It looks like the fetch API allows body
to be a URLSearchParams
?
https://developer.mozilla.org/en-US/docs/Web/API/Request#examples
Maybe I'm going about this wrong by creating a distinction? The two types that I originally split (GetRequest
and RequestWithBody
) would theoretically collapse to just the one type if I treated search params as a body.
Idk, that seems a little weird and overloaded to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that literally does put the URLSearchParams in the body, with an appropriate content-type of application/x-www-form-urlencoded
. https://fetch.spec.whatwg.org/#bodyinit-unions
This is actually the thing that POST
s do by default! https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-enctypeso not really relevant to controlling the actual URL params.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't super carefully reviewed the README but the rest looks good!
fix: add ability to skip checking http cache
Update the
RESTDataSource
class to be compatible with Apollo Server v4's new approach to datasources. This means initialization happens in the constructor (there's no more magic call toinitialize
done by Apollo Server).Migrate to our
Fetcher
interface (and related types). This is a bit "restrictive" in that it doesn't accept theRequest
class fromnode-fetch
, but the signature should be more cross compatible with other fetch implementation.Remove
apollo-server-env
completely, update tests to use nock.Fixes apollographql/apollo-server#6048