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.
graphqlcommand line executable that comes from github.com/Urigo/graphql-cli