@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Formatting GraphQL query with prettier

brew install prettier

query.gql

query { repository(owner: "OWNER", name: "REPO") { name owner { login } issues(first: 10) { nodes { title body author { login } labels(first: 5) { nodes { name } } comments(first: 5) { nodes { body author { login } } } } } pullRequests(first: 10) { nodes { title body author { login } labels(first: 5) { nodes { name } } comments(first: 5) { nodes { body author { login } } } } } releases(first: 5) { nodes { name tagName author { login } description } } discussions(first: 5) { nodes { title body author { login } comments(first: 5) { nodes { body author { login } } } } } projectsV2(first: 5) { nodes { title items(first: 5) { nodes { content { ... on Issue { title body author { login } } ... on PullRequest { title body author { login } } } } } } } } }

整形は prettier file で OK

prettier -w file だと整形して上書き保存する

❯ prettier query.gql
query {
  repository(owner: "OWNER", name: "REPO") {
    name
    owner {
      login
    }
    issues(first: 10) {
      nodes {
        title
        body
        author {
          login
        }
        labels(first: 5) {
          nodes {
            name
          }
        }
        comments(first: 5) {
          nodes {
            body
            author {
              login
            }
          }
        }
      }
    }
    pullRequests(first: 10) {
      nodes {
        title
        body
        author {
          login
        }
        labels(first: 5) {
          nodes {
            name
          }
        }
        comments(first: 5) {
          nodes {
            body
            author {
              login
            }
          }
        }
      }
    }
    releases(first: 5) {
      nodes {
        name
        tagName
        author {
          login
        }
        description
      }
    }
    discussions(first: 5) {
      nodes {
        title
        body
        author {
          login
        }
        comments(first: 5) {
          nodes {
            body
            author {
              login
            }
          }
        }
      }
    }
    projectsV2(first: 5) {
      nodes {
        title
        items(first: 5) {
          nodes {
            content {
              ... on Issue {
                title
                body
                author {
                  login
                }
              }
              ... on PullRequest {
                title
                body
                author {
                  login
                }
              }
            }
          }
        }
      }
    }
  }
}

ファイル名(拡張子)から整形すべきフォーマットを類推するので、たとえば query.gql.orig というファイル名だとフォーマットに失敗する。そういう場合は--parserオプションを明示する。

prettier --parser=graphql query.gql.orig