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

RESTDataSource - fetch timeout #1594

Closed
shinyaohira opened this issue Aug 30, 2018 · 4 comments
Closed

RESTDataSource - fetch timeout #1594

shinyaohira opened this issue Aug 30, 2018 · 4 comments
Labels
⛲️ feature New addition or enhancement to existing solutions

Comments

@shinyaohira
Copy link
Contributor

I am using RESTDataSource to fetch data and want to set timeout for it. But RequestInit in apollo-server-env doesn't have timeout option.

@ghost ghost added the ⛲️ feature New addition or enhancement to existing solutions label Aug 30, 2018
@sbrichardson
Copy link
Contributor

sbrichardson commented Aug 31, 2018

I haven't checked it out yet, but it looks like it could be supported, since I believe apollo-server-env uses "node-fetch": "^2.1.2",. It looks like timeout is an extension on node-fetch,

  • This comment for node-fetch provides some insight.

  • This comment on github/fetch

From the node-fetch library (source):

let inputBody =
  init.body != null
    ? init.body
    : isRequest(input) && input.body !== null
      ? clone(input)
      : null

Body.call(this, inputBody, {
  timeout: init.timeout || input.timeout || 0,
  size: init.size || input.size || 0,
})

And the apollo-server-env (source)

export declare class Request extends Body {
  constructor(input: Request | string, init?: RequestInit);

  readonly method: string;
  readonly url: string;
  readonly headers: Headers;

  clone(): Request;
}

export interface RequestInit {
  method?: string;
  headers?: HeadersInit;
  body?: BodyInit;
  mode?: RequestMode;
  credentials?: RequestCredentials;
  cache?: RequestCache;
  redirect?: RequestRedirect;
  referrer?: string;
  referrerPolicy?: ReferrerPolicy;
  integrity?: string;
  // Cloudflare Workers accept a `cf` property to control Cloudflare features
  // See https://developers.cloudflare.com/workers/reference/cloudflare-features/
  cf?: {
    [key: string]: any;
  };
}

Here's a link to some tests (node-fetch) for the timeout extension. Haven't checked it out yet though.

@shinyaohira
Copy link
Contributor Author

@sbrichardson I've confirmed it works. Thank you!
I'm using TypeScript so I submitted PR #1602 for node-fetch extensions typings.

@sbrichardson
Copy link
Contributor

@shinyaohira Awesome, would you mind sharing an example of the timeout setting you used, or the config object? Thanks

@shinyaohira
Copy link
Contributor Author

@sbrichardson I'm using it like this:

import { RequestInit } from "apollo-server-env";
...
      const requestInit: RequestInit = {
        timeout: 1000,
        compress: true,
      };
...
      const result = await this.get<Item>("item", params, requestInit);

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
⛲️ feature New addition or enhancement to existing solutions
Projects
None yet
Development

No branches or pull requests

2 participants