Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can you give an example of what you mean?


I mean, instead of

query { user { id name } }

why not

query: { user: { id: true, name: true, } }


Generally you'd use a library with much stricter conventions than JSON to generate and parse GraphQL queries, rather than writing them as a string. If you're using JS/ES/TS, you can even configure a linter[0] to validate your queries against a schema as you're writing them.

Insofar as JSON is a type system, it's a weaker one than GraphQL, and would be a lot more verbose. Consider the query

    query(id: Int, hasUser: Boolean) { 
      @include(if: $hasUser)
      user(id: $id) { 
        name 
      }
    }
It would be possible to represent this in JSON, but it would be a lot less elegant.

    {
      "query": {
        "args": {"id": "int", "hasUser": "boolean"},
        "fields": {
          "user": {
            "args": {"id": "$id"},
            "fields": {"name": true},
            "includeIf": "$hasUser"
          }
        }
      }
    }
The JSON still depends on stringly-typed values like "$hasUser", but it's much harder to read and has many more unnecessary characters.

EDIT: if you want to manipulate GraphQL queries, you need to parse them[1] and then look at the AST. This is not very well documented, so the best way of doing it is by experimenting with the values you've got, and then verifying your approach against graphql-js.

[0] https://github.com/apollographql/eslint-plugin-graphql

[1] https://github.com/apollographql/graphql-tag




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: