fix: gql-tag-operations generates invalid types on Windows #7362 #7369
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Related #7362
Problem summary
gql-tag-operations
generates invalid types on Windows, causing compile-time error when attempting to launch the application.Root cause
Source file is read by
@graphql/tools
usingfs.promises.readFile
, which means that the linebreaks are read as-is and the result will be different depending on the OS: it will contain LF (\n
) on Linux/MacOS and CRLF (\r\n
) on Windows.In most scenarios that would be OK. However,
gql-tag-operations
is using the resulting string as a TypeScript type. Which means that the string will be compared against a template literal, for example:According to clause 12.8.6.2 of ECMAScript Language Specification, when comparing strings, JavaScript doesn't care which linebreaks does the source file contain, any linebreak (CR, LF or CRLF) is LF from JavaScript standpoint (otherwise the result of the above comparison would be OS-dependent, which doesn't make sense).
The reason
gql-tag-operations
breaks on Windows is that it generates\r\n query a {\r\n a\r\n }\r\n
, which is NOT equal toSolution
@graphql/tools
is doing its job correctly. Also it probably doesn't make sense to make changes ontypescript
plugin level, asgql-tag-operations
is probably the only place where it matters. Therefore the solution is to replace\r\n
with\n
during the preset-specific processing.Type of change
How Has This Been Tested?
fix
commit (leaving onlytest
commit) and run the tests.gql-tag-operations
within this application manually on a Windows machine.Test Environment:
@graphql-codegen/typescript: ^2.2.2
:Checklist: