GraphqlQuery.Parser (graphql_query v0.6.2)

View Source

Utilities for parsing and analyzing GraphQL query strings.

Provides functionality to detect dynamic parts in queries and format validation errors with proper location information.

Summary

Functions

Builds a line map for a document with its fragments.

Enriches validation error messages that lack detail.

Finds the line in the query where a fragment spread (...FragmentName) appears.

Formats a validation error with proper location information.

Checks if a GraphQL query string contains dynamic interpolation parts.

Resolves which source (query or fragment) an error line belongs to.

Functions

build_line_map(document)

Builds a line map for a document with its fragments.

Returns a map with:

  • :segments — list of {source, start_line, end_line} tuples where source is either :query or {:fragment, name}. Lines are 1-indexed and refer to positions in the combined validation string (query + appended fragments).
  • :spreads — map of %{fragment_name => line_in_query} indicating where each fragment spread (...FragmentName) appears in the query text.

enrich_error_message(error, query_text)

Enriches validation error messages that lack detail.

Apollo-compiler's UnsupportedValueType diagnostic produces messages like "expected value of type ID, found a variable" without naming the variable or its declared type. This function detects that pattern and enhances the message using the query text and error location.

find_spread_line(arg1, fragment_name)

Finds the line in the query where a fragment spread (...FragmentName) appears.

Returns the line number (1-indexed) or nil if the spread is not found.

format_error(error, warn_location, prefix)

Formats a validation error with proper location information.

Combines error location with warning location to provide accurate error positioning in the source file.

Parameters

  • error - Validation error with message and location
  • warn_location - Source location information (line, column, file, etc.)
  • prefix - String prefix or function that generates error prefix

Examples

error = %GraphqlQuery.ValidationError{
  message: "Unused variable",
  locations: [%GraphqlQuery.Location{line: 2, column: 5}]
}

location = [line: 10, file: "query.ex"]
formatted = GraphqlQuery.Parser.format_error(error, location, "Validation error:")

# Returns: %{message: "[GraphqlQuery] Validation error: Unused variable", location: [...]}

has_dynamic_parts?(query)

Checks if a GraphQL query string contains dynamic interpolation parts.

Returns true if the query contains #{ patterns, indicating dynamic content that cannot be validated at compile time.

Examples

iex> GraphqlQuery.Parser.has_dynamic_parts?("query { user { name } }")
false

iex> GraphqlQuery.Parser.has_dynamic_parts?("query { user { #{@fields} } }")
true

resolve_error_source(error_line, segments)

Resolves which source (query or fragment) an error line belongs to.

Returns {:query, relative_line} or {:fragment, name, relative_line}.