Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is white space significant in GraphQL or not?

Tags:

graphql

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?

like image 924
LearningFast Avatar asked Oct 15 '22 02:10

LearningFast


1 Answers

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.

like image 145
Daniel Rearden Avatar answered Oct 21 '22 09:10

Daniel Rearden