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

Fetch policy constants #1488

Closed

Conversation

NeoPhi
Copy link

@NeoPhi NeoPhi commented Mar 24, 2017

TODO:

  • If this PR is a new feature, reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests
  • Rebase your changes on master so that they can be merged easily
  • Make sure all tests and linter rules pass
  • Update CHANGELOG.md with your change
  • Add your name and email to the AUTHORS file (optional)
  • If this was a change that affects the external API, update the docs and post a link to the PR in the discussion

What to confirm the constant naming and approach for exporting in a namespace before updating any documentation.

this.options,
this.queryId,
);
this.startPolling(this.options.pollInterval);
Copy link
Author

Choose a reason for hiding this comment

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

Seemed a case of repeated logic so I opted to call the existing method instead.

Copy link
Contributor

@helfer helfer left a comment

Choose a reason for hiding this comment

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

Thanks @NeoPhi!

I think this change is only useful for JS users. As such it might be better if we limit it to the one type definitions file you created and just export it from there for the benefit of JS users. The reasoning is that using typescript and a good editor makes this unnecessary, because it will propose the different options to you. Even if you're using Apollo Client in Javascript, I suggest you try out VScode, because it will suggest / auto-complete the fetchPolicy strings for you!

That said, I'm not completely opposed to using constants in this way, even though it's not idiomatic typescript. But in that case we'd definitely have to use those constants when defining the type, otherwise it kind of defeats the whole point of using constants.

What do you think @stubailo?

/**
* fetchPolicy determines where the client may return a result from.
*/
export type FetchPolicy = 'cache-first' | 'cache-and-network' | 'network-only' | 'cache-only';
Copy link
Contributor

Choose a reason for hiding this comment

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

Constants are only useful for Javascript users. In Typescript they're not necessary at all. If we do use constants everywhere, this is the one line we definitely shouldn't skip!

Copy link
Author

Choose a reason for hiding this comment

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

Wasn't sure of a way in TypeScript to define a type using constants. Instead I defined the constants to be of the FetchPolicy type.

Copy link
Contributor

Choose a reason for hiding this comment

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

This sounds like a job for a TypeScript Enum - I think you can have TS compile that into a dictionary with values.

export const CACHE_FIRST: FetchPolicy = 'cache-first';

/**
* returns result from cache first (if it exists), then return network result once it's available.
Copy link
Contributor

Choose a reason for hiding this comment

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

extra 's' in returns.

@stubailo
Copy link
Contributor

I'm wondering what the desired benefit of constants is in this case over strings - is it because there's a worry we might later need to change the underlying values of these constants? If we are going to keep the same string values forever, it seems like we don't get a lot of value out of wrapping them in constants.

@NeoPhi
Copy link
Author

NeoPhi commented Mar 27, 2017

I couldn't find a way to do this cleanly in TypeScript with easy support for vanilla JS.

@NeoPhi NeoPhi closed this Mar 27, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants