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

Make variableDefinitions not nullable #2405

Closed
wants to merge 1 commit into from

Conversation

brandon-leapyear
Copy link

@brandon-leapyear brandon-leapyear commented Jan 27, 2020

This is an old work account. Please reference @brandonchinn178 for all future communication


It seems like variableDefinitions will always be a non-null array.

parseOperationDefinition(): OperationDefinitionNode {
const start = this._lexer.token;
if (this.peek(TokenKind.BRACE_L)) {
return {
kind: Kind.OPERATION_DEFINITION,
operation: 'query',
name: undefined,
variableDefinitions: [],
directives: [],
selectionSet: this.parseSelectionSet(),
loc: this.loc(start),
};
}
const operation = this.parseOperationType();
let name;
if (this.peek(TokenKind.NAME)) {
name = this.parseName();
}
return {
kind: Kind.OPERATION_DEFINITION,
operation,
name,
variableDefinitions: this.parseVariableDefinitions(),
directives: this.parseDirectives(false),
selectionSet: this.parseSelectionSet(),
loc: this.loc(start),
};
}

parseVariableDefinitions(): Array<VariableDefinitionNode> {

@IvanGoncharov
Copy link
Member

Hi @brandon-leapyear

Thanks for PR 👍
AST format should have a healthy balance between the ability to easily process but at the same to easily construct AST nodes.
For example, at the moment you can easily extract part of the query into separate query:

return {
  kind: Kind.OPERATION_DEFINITION, 
  selectionSet: someSelectionSet,
};

And you don't need to add a bunch of empty arrays, moreover, it makes code less fragile when we add new properties to AST nodes.

But I agree it looks weird and creates complications, for example with testing see #2203
Personally I think a good long term solution would be to always return undefined instead of empty arrays since it's will improve performance and memory consumption.
Also, it would be great if someone researches what other JS parsers do e.g. Babel's parser, ESLint parser, etc.

Base automatically changed from master to main January 27, 2021 11:10
@IvanGoncharov IvanGoncharov added this to the post-16.0.0 milestone Aug 11, 2021
yaacovCR added a commit to yaacovCR/graphql-js that referenced this pull request Sep 27, 2024
@yaacovCR
Copy link
Contributor

closing this PR in favor of #4206 based on suggestion above in #2405 (comment)

@yaacovCR yaacovCR closed this Sep 27, 2024
yaacovCR added a commit that referenced this pull request Oct 14, 2024
see:
#2405 (comment)

Manually created ASTs always allowed undefined in place of empty arrays;
this change simply updates the parser to more closely follow the manual
approach. This is therefore not technically a breaking change, but
considering that there may be tools not aware of this, we have labelled
it a BREAKING_CHANGE to highlight it for consumers.

This closes #2203 as our tests now cover the undefined case by default
whenever there is an empty array.
erikwrede pushed a commit to erikwrede/graphql-js that referenced this pull request Oct 17, 2024
see:
graphql/graphql-js#2405 (comment)

Manually created ASTs always allowed undefined in place of empty arrays;
this change simply updates the parser to more closely follow the manual
approach. This is therefore not technically a breaking change, but
considering that there may be tools not aware of this, we have labelled
it a BREAKING_CHANGE to highlight it for consumers.

This closes #2203 as our tests now cover the undefined case by default
whenever there is an empty array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants