absinthe-graphql / absinthe

The GraphQL toolkit for Elixir
http://absinthe-graphql.org
Other
4.29k stars 527 forks source link

Proposal - add a GRAPHQL sigil #1284

Open dkuku opened 1 year ago

dkuku commented 1 year ago

I have not seen this anywhere in requests but by adding a GRAPHQL sigil, this way we can use the formatter to format inline queries and also tree sitter can be setup for highlighting inline queries. Example: https://maartenvanvliet.nl/2022/01/26/absinthe_formatter/

oliver-kriska commented 10 months ago

fyi I did it in our project:

defmodule GSigil do
  def sigil_G(string, []), do: string
end

somewhere where you want to use it

import GSigil

and plugin for formatter

defmodule GqlFormatter do
  @behaviour Mix.Tasks.Format

  def features(_opts) do
    [sigils: [:G]]
  end

  def format(contents, _opts \\ []) do
    Absinthe.Formatter.format(%Absinthe.Blueprint{input: contents})
  end
end

add plugin to formatter

 plugins: [GqlFormatter]

Usage

@cart_query_end_user ~G"""
  query cart {
    cart {
      id
      type
      totals {
        amountToPay {
          amount
          currency
        }
      }
      user {
        email
      }
      userId
      sessionUid
      items {
        id
        type
        productId
        productVersionId
        productVariantId
        productVariantVersionId
        quantity
      }
    }
  }
  """