Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Avoid importing graphql/language/printer for getKey. #992

Merged
merged 2 commits into from Mar 18, 2019

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Mar 18, 2019

We have been slowly working to remove all uses of the graphql-js print function from Apollo client packages, since the printer weighs in at 4.86KB minified (without gzip); and, while that's not terribly huge, printing large queries takes precious time that we can usually avoid.

This particular usage is relatively easy to replace with JSON.stringify. This should be faster than actually printing the query, and we can serialize everything (query, variables, and operationName) with just one call to JSON.stringify, without manually joining the fragments with '|'.

It's reasonable to be concerned about the stability of JSON.stringify, since equivalent queries could have slightly different property ordering, but the official GraphQL printer doesn't make any guarantees about the stability or canonicality of the resulting string, either. The important thing is that === identical queries have the same string form, and that's definitely true here.

After this change, there is just one remaining call to print in apollo-link-http-common, which seems a bit harder to replace, but at least you have the option of using something other than apollo-link-http if you really care about not reprinting the query before sending it to the server.

We have been slowly working to remove all uses of the graphql-js print
function from Apollo client packages, since the printer weighs in at
4.86KB minified (without gzip); and, while that's not terribly huge,
printing large queries takes precious time that we can usually avoid.

This particular usage is relatively easy to replace with JSON.stringify.
This should be faster than actually printing the query, and we can
serialize everything (query, variables, and operation name) with just one
call to JSON.stringify, without manually joining the fragments with '|'.

It's reasonable to be concerned about the stability of JSON.stringify,
since equivalent queries could have slightly different property ordering,
but the official GraphQL printer doesn't make any guarantees about the
stability or canonicality of the resulting string, either. The important
thing is that === identical queries have the same string form, and that's
definitely true here.

After this change, there is just one remaining call to print in
apollo-link-http-common, which seems a bit harder to replace, but you have
the option of using something other than apollo-link-http if you really
care about not reprinting the query before sending it to the server:
https://github.com/apollographql/apollo-link/blob/ed1800b4261566e91906cdb9cc45c7d004393175/packages/apollo-link-http-common/src/index.ts#L239
@codecov-io
Copy link

codecov-io commented Mar 18, 2019

Codecov Report

Merging #992 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #992   +/-   ##
=======================================
  Coverage   95.19%   95.19%           
=======================================
  Files          22       22           
  Lines        1020     1020           
  Branches      103      103           
=======================================
  Hits          971      971           
  Misses         44       44           
  Partials        5        5
Impacted Files Coverage Δ
packages/apollo-link/src/linkUtils.ts 98% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ed1800b...ce096f9. Read the comment docs.

Copy link
Contributor

@JoviDeCroock JoviDeCroock left a comment

Choose a reason for hiding this comment

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

Looks good to me, and 22 Bytes less in a package of this size is pretty good!

@benjamn
Copy link
Member Author

benjamn commented Mar 18, 2019

22 Bytes less in a package of this size is pretty good!

Agreed, though I definitely was not expecting that!

@benjamn benjamn merged commit 517d50a into master Mar 18, 2019
@benjamn benjamn deleted the avoid-graphql/language/printer-in-getKey branch March 18, 2019 19:38
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants