Hacker News new | ask | show | jobs
by thelazydogsback 3243 days ago
Why doesn't GraphQL just define semantics over what is JSON syntactically so existing tools and parsers are sufficient? It's already so close to JSON, why use parens and strings containing emedded JSON? E.g., instead of:

  {
    "query": "repository(owner:\"zapier\", name:\"transformer\") {
      id
      description
    }"
 }
use something like:

  {
    "query": {target: "repository", owner:"zapier", name:"transformer",  [
      "id",
      "description"
    ]}}
  }
2 comments

Maybe they want to be, to some extent, serialization-format independent?
That's correct. See https://facebook.github.io/graphql/#sec-Serialization-Format.

> GraphQL does not require a specific serialization format. However, clients should use a serialization format that supports the major primitives in the GraphQL response.

JSON is preferred, but not required.

The section of the document you refer to is regarding the response, not the query-language itself. From the document: "A GraphQL document is defined as a syntactic grammar" -- therefore GraphGL has a (one) preferred syntactic embodiment. The document goes on (in A BNF style) to define the syntax of the language. The language defined here is very close, but not quite, JSON - if it were JSON, they could have defined the semantics of GraphQL based on its structure (which could be surfaced also in XML, etc.) rather than defining any surface/syntactic form at all. If this domain specific language had a huge value-add over plain JSON, it would be another matter, but the notational convenience is small given the all the tooling that you lose by not having be in a standard format.
GraphQL queries supports other things like fragments, which don't map very well to JSON.