GqlCase (GqlCase v0.8.0)

Copy Markdown View Source

This module defines helper macros to work with Graphql and Guardian

Summary

Functions

Call this macro in the module you wish to load your GQL document from a file.

Call this macro in the module you wish to load your GQL document from a query string.

Execute a GraphQL query or mutation against the configured endpoint.

Functions

add_headers(conn, headers)

add_headers(conn, jwt_bearer_fn, opts \\ [])

load_gql_file(file_path)

(macro)

Call this macro in the module you wish to load your GQL document from a file.

It takes one argument, the path to a GQL file that contains a GraphQL query or mutation.

For example:

defmodule MyApp do
  load_gql_file "assets/js/queries/MyQuery.gql"
  # ...
end

load_gql_string(query_string)

(macro)

Call this macro in the module you wish to load your GQL document from a query string.

It takes one argument, a string containing a GraphQL query or mutation.

For example:

defmodule MyApp do
  load_gql_string """
  query {
    hello
  }
  """
  # ...
end

merge_headers_with_priority(header_lists)

query_gql(opts \\ [])

(macro)

Execute a GraphQL query or mutation against the configured endpoint.

The query is resolved from two sources, in priority order:

  1. The query: option (runtime) — used if provided
  2. The @_gql_query module attribute — fallback (set via load_gql_file or load_gql_string)

If neither is present, raises SetupError.

Whether a document was loaded is decided when query_gql/1 is expanded, so load_gql_file/1 or load_gql_string/1 must appear before the functions that call query_gql/1.

Options

  • query - a GraphQL query string (optional if load_gql_file/load_gql_string was used)
  • variables - a map of GraphQL variables (default: %{})
  • current_user - a user map for JWT authentication (optional)
  • headers - a list of {key, value} header tuples (optional)

Examples

# Per-call inline query
query_gql(query: "query { hello }", variables: %{})

# Using module-level query (legacy)
load_gql_file "queries/Hello.gql"
query_gql(variables: %{})