In GraphQL, white space appears to be significant since it separates tokens:
{
human(id: "1000") {
name
height
}
}
However, the spec says that white space is not significant, and doesn't specify how tokens, names, and so forth are separated.
I think newlines might be critical in separating name
and height
in the above example. Is it valid to delete the newline and have them on the same line, separated by a space or tab? I assume the indentation is insignificant? Is this valid GraphQL, and is it exactly equivalent to the above?:
{human(id: "1000") {name height}}
Obviously, it would break if we eliminated the space between name
and height
, so spaces seem to matter. Since GraphQL doesn't use commas, should the spec specify that white space is significant?
From the spec (emphasis mine):
White space is used to improve legibility of source text and act as separation between tokens, and any amount of white space may appear before or after any token. White space between tokens is not significant to the semantic meaning of a GraphQL Document, however white space characters may appear within a String or Comment token.
In other words, white space (or any other ignored token) needs to be used to separate tokens. But it does not have semantic meaning itself and is therefore not significant.
Spaces, tabs, new lines and commas are all ignored tokens and can be used interchangeably to separate tokens. The following snippets are semantically equivalent and all valid syntax:
name height
name height
name,height
name
height
name , ,,,,
,, height
What token you use is ultimately up to you, however you should always keep readability in mind.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With