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

fix: ensure variance of types matches how values are used #2786

Merged
merged 6 commits into from Oct 9, 2020

Conversation

ForbesLindesay
Copy link
Contributor

I've written up detailed reasoning in dotansimha/graphql-typed-document-node#38

I didn't remove the "FIXME" comment, as I'm not sure what you would prefer to do with it. Nominal typing wouldn't actually solve anything here, because the variables and responses are plain objects, not nominal types. What's needed is just accurate structural typing. I really don't view this as a hack because:

  1. We're not lying about any of the types that exist at runtime
  2. The __apiType property does accurately document/describe the API behavour
  3. The type errors TypeScript would produce if you use these TypedQueryDocumentNodes incorrectly matches up with the issues you would see at runtime.

I've written up detailed reasoning in dotansimha/graphql-typed-document-node#38

I didn't remove the "FIXME" comment, as I'm not sure what you would prefer to do with it. Nominal typing wouldn't actually solve anything here, because the variables and responses are plain objects, not nominal types. What's needed is just accurate structural typing. I really don't view this as a hack because:

1. We're not lying about any of the types that exist at runtime
2. The `__apiType` property does accurately document/describe the API behavour
3. The type errors TypeScript would produce if you use these `TypedQueryDocumentNode`s incorrectly matches up with the issues you would see at runtime.
@IvanGoncharov
Copy link
Member

IvanGoncharov commented Sep 3, 2020

I really don't view this as a hack because:

@ForbesLindesay I'm ok with merging it but if we declare a non-void property that is never set it is a clear hack in my book 😄

Nominal typing wouldn't actually solve anything here, because the variables and responses are plain objects, not nominal types.

In Nominal type systems types with different template arguments treated as incompatible types, e.g. C++, Java, etc.

__apiType

The name of the field should reflect its purpose so if I get an error message I don't need to look into graphql-js sources to understand what's the purpose of this field.
I don't think a long name is a problem for a field that you should never use in your code so it is better to have an extremely description field.

@IvanGoncharov IvanGoncharov added the PR: bug fix 🐞 requires increase of "patch" version number label Sep 3, 2020
@IvanGoncharov
Copy link
Member

The type errors TypeScript would produce if you use these TypedQueryDocumentNodes incorrectly matches up with the issues you would see at runtime.

@ForbesLindesay Please add test for this here:
https://github.com/graphql/graphql-js/blob/master/integrationTests/ts/index.ts#L74

@IvanGoncharov
Copy link
Member

@dotansimha this bugfix blocks 15.4.0 release since we can't release TypedQueryDocumentNode until we are sure we implemented it correctly.
Can you please work together with @ForbesLindesay to address review comments and also ensure this functionality is compatible with graphql-code-generator. It would great if you prepare matching PR for graphql-code-generator and test it together with other GraphQL packages that it's intended to be used.

@dotansimha
Copy link
Member

Hi @IvanGoncharov , sorry for the delay! I will look into this tomorrow :)

@dotansimha
Copy link
Member

@ForbesLindesay I think you are right here, I did some checks and it seems like the covariant might cause inconsistency here.
I tested this with your change and it seems to work perfectly :)

@IvanGoncharov From my point of view, I think it's a better way to declare the usage of the generics, and it doesn't really matter since it's unused, same as the existing solution.

@IvanGoncharov
Copy link
Member

@dotansimha @ForbesLindesay Can you please rename __apiType to something more descriptive as explained here:
#2786 (comment)
And add test as described here:
#2786 (comment)
I can't merge it without tests since I don't understand what is fixed by this PR.
This issue blocks 15.4.0 release, so if you don't have time to address review comments please ping me so we can figure out the next steps.

@ardatan
Copy link
Member

ardatan commented Oct 7, 2020

@ForbesLindesay I created a PR for your branch that includes what @IvanGoncharov asked for.
ForbesLindesay#1

@ardatan
Copy link
Member

ardatan commented Oct 9, 2020

@ForbesLindesay This is another PR for linting errors :)
ForbesLindesay#2

@ardatan
Copy link
Member

ardatan commented Oct 9, 2020

@IvanGoncharov I think it is ready to merge?

@IvanGoncharov IvanGoncharov merged commit 711425e into graphql:master Oct 9, 2020
@IvanGoncharov
Copy link
Member

@ardatan Thanks a lot 👍
Merged 🎉

tgriesser added a commit to tgriesser/graphql-js that referenced this pull request Nov 13, 2020
…ription

* master: (211 commits)
  Update deps (graphql#2844)
  resources: use named groups in RegExp (graphql#2840)
  TS: exclude integration tests from root tsconfig.json (graphql#2838)
  Flow: remove support for measuring Flow coverage (graphql#2837)
  CI: test on node 15 (graphql#2836)
  Update deps (graphql#2835)
  build: add support for experimental releases (graphql#2831)
  15.4.0
  Update deps (graphql#2827)
  Update deps (graphql#2825)
  integrationTests: add Flow test (graphql#2819)
  fix: ensure variance of types matches how values are used (graphql#2786)
  Cleanup '__fixtures__' leftovers (graphql#2818)
  Convert fixtures to be JS files (graphql#2816)
  Update deps (graphql#2815)
  benchmark: extract benchmarks into separate folder and run them on NPM package
  Update deps (graphql#2810)
  Update outdated documentation (graphql#2806)
  Make print() break arguments over multiple lines (graphql#2797)
  Added check for specific symbols in polyfills/symbols (graphql#2804)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: bug fix 🐞 requires increase of "patch" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants