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 adding descriptions to type extensions #1582

Merged
merged 1 commit into from
Mar 10, 2022

Conversation

pcmanus
Copy link
Contributor

@pcmanus pcmanus commented Mar 10, 2022

If a schema had both a type definition and a correspdonding type
extension, the printing code was mistakenly printing the description of
the type definition on the type extension as well. But since description
are not syntactically valid on type extension in graphQL, this leads to
syntactic errors.

More precisely, an input schema like:

""" I'm a type """
type T {
  a: Int
}

extend type T {
  b: Int
}

was (before this patch) mistakenly printed as:

""" I'm a type """
type T {
  a: Int
}

""" I'm a type """
extend type T {
  b: Int
}

which is syntactically invalid due to the description on the type
extension.

Note: the patch also fix a related additional issue where the presence
of type extension might have the type definition description ignored due
a lack of conditional when setting descriptions in buildSchema.

If a schema had both a type definition and a correspdonding type
extension, the printing code was mistakenly printing the description of
the type definition on the type extension as well. But since description
are not syntactically valid on type extension in graphQL, this leads to
syntactic errors.

More precisely, an input schema like:
```graphql
""" I'm a type """
type T {
  a: Int
}

extend type T {
  b: Int
}
```
was (before this patch) mistakenly printed as:
```graphql
""" I'm a type """
type T {
  a: Int
}

""" I'm a type """
extend type T {
  b: Int
}
```
which is syntactically invalid due to the description on the type
extension.

Note: the patch also fix a related additional issue where the presence
of type extension might have the type definition description ignored due
a lack of conditional when setting descriptions in `buildSchema`.
@codesandbox-ci
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link
Contributor

@clenfest clenfest left a comment

Choose a reason for hiding this comment

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

I left a couple of comments, but if you're ok with them, feel free to merge.

@@ -299,7 +299,9 @@ function buildNamedTypeInner(
const enumType = type as EnumType;
for (const enumVal of definitionNode.values ?? []) {
const v = enumType.addValue(enumVal.name.value);
v.description = enumVal.description?.value;
if (enumVal.description) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a change? Behavior looks the same to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See comment on types below. I actually don't think the change is necessary in that case of enum values, but just made it to mirror the other similar cases in the file.

@@ -315,7 +317,9 @@ function buildNamedTypeInner(
break;
}
buildAppliedDirectives(definitionNode, type, extension);
type.description = definitionNode.description?.value;
if (definitionNode.description) {
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's the part I'm referring to in the "Note" of the commit message: it's not the same behaviour in that this code may run multiple times for the same type, so if we had a description the first time (say on the type definition), then we were erasing it if there was no description the 2nd time (say on the type extension). The change makes it so we don't erase it anymore.

@@ -427,6 +427,10 @@ test('handling of descriptions', () => {
pages: Int @Important
}

extend type Book {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to check the result anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test ends by comparing the result of printSchema and the original input. So before this patch the test would fail due the bug solved here.

@pcmanus pcmanus merged commit 3efdfa3 into main Mar 10, 2022
@pcmanus pcmanus deleted the fix-description-on-extensions branch March 10, 2022 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants