Skip to content
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

Merged
merged 16 commits into from
Aug 18, 2022

Conversation

trevor-scheer
Copy link
Member

@trevor-scheer trevor-scheer commented Aug 8, 2022

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 to initialize done by Apollo Server).

Migrate to our Fetcher interface (and related types). This is a bit "restrictive" in that it doesn't accept the Request class from node-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

}

export interface RequestWithBody extends Omit<RequestOptions, 'body'> {
method?: 'POST' | 'PUT' | 'PATCH' | 'DELETE';
Copy link
Member Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

package.json Show resolved Hide resolved
src/RESTDataSource.ts Show resolved Hide resolved
@@ -89,13 +86,14 @@ export abstract class RESTDataSource<TContext = any> extends DataSource {
}

protected cacheOptionsFor?(
response: Response,
request: Request,
url: string,
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

...request,
body: undefined,
};
if (!(modifiedRequest.params instanceof URLSearchParams)) {
Copy link
Member

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?)

Copy link
Member Author

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


protected resolveURL(request: RequestOptions): ValueOrPromise<URL> {
let path = request.path;
protected resolveURL(path: string): ValueOrPromise<URL> {
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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;
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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-enctype

so not really relevant to controlling the actual URL params.

src/index.ts Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
Copy link
Member

@glasser glasser left a 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!

@trevor-scheer trevor-scheer merged commit 55b6b10 into main Aug 18, 2022
@trevor-scheer trevor-scheer deleted the trevor/update-datasource-and-cache branch August 18, 2022 21:34
@github-actions github-actions bot mentioned this pull request Aug 18, 2022
nmrj pushed a commit to northwesternmutual/datasource-rest that referenced this pull request May 28, 2024
fix: add ability to skip checking http cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move apollo-datasource-rest to its own repository
2 participants