3

I have a GraphQL query in GetPerson.gql that is all scrunched up on a single line:

{ getPerson(id: 42) { name, age } }

How can I pretty-print or beautify this text using the command line?

$ magical-command GetPerson.gql
{
  getPerson(id: 42) {
    name,
    age
  }
}
6
  • That looks somewhat like json. Thus, maybe a json reformatter would work. Commented Apr 12, 2023 at 17:22
  • 1
    It does look similar to json, but is in fact not valid json so a json formatter will not work. Commented Apr 12, 2023 at 17:23
  • 1
    @RonJohn That's not JSON. Commented Apr 12, 2023 at 17:23
  • What OS are you on? Commented Apr 12, 2023 at 17:24
  • I'm on macOS. And I have a graphql command line executable that comes from github.com/Urigo/graphql-cli Commented Apr 12, 2023 at 17:30

2 Answers 2

5

Using prettier installed via brew install prettier on macOS:

$ cat file.gql
{ getPerson(id: 42) { name, age } }
$ prettier file.gql
{
  getPerson(id: 42) {
    name
    age
  }
}

(The same output is had if using --parser graphql in this example.)

The prettier tool can do in-place editing with -w or --write. See also prettier --help.

4

So, first of all, if you have any contacts at meta, tell them how bad a design decision it was to merge the well-working graphql utilities all into the GraphiQL IDE monorepo. Like, why? Makes everything harder, but great, now you can make more buggy releases in shorter time.

Anyways, what you want is to use the Language Server Protocol (LSP) server for GraphQL, which things like VS Code and the totally necessary GraphiQL IDE use internally to indent code, understand the symbols under the cursor, autocomplete,…

In case of GraphQL, that's the formerly-available-as-standalone-project graphql-language-service. Try installing it via npm: npm i -g graphql-language-service-server.

Then, you'd want to use a client that can take a command to format the file. In this case, neovim seems to be your best bet. You need to set it up such that automatically starts the LSP server as needed, and connects to it, when a graphql file is loaded. Then, you issue the command to open a file, set the file type to GraphQL (:set ft=graphql), and run :lua vim.lsp.buf.format(), and save the resulting file (:w), and quit. So, as a command line, something like

nvim -c ':set ft=graphql' -c ':lua  vim.lsp.buf.format()' -c ':w' -c ':q' yourfile

Now, I haven't tested this, so I'm sure the devil's in the details of setting up neovim + LSP for graphql (I use astronvim to get a useful base configuration, by the way), so I'm afraid I'll leave this level of working things out for you.

Upside is that you can use neovim as client to any LSP, be it for C++, Rust, JavaScript, or whatever more obscure languages come with an LSP implementation these days.

2
  • note that curiosity led me to actually trying, and now it's actually not working. Sadly, I have no more private time allotted for investigatons today… Commented Apr 12, 2023 at 18:35
  • following up on vi.se Commented Apr 13, 2023 at 6:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.