GraphqlQuery.Schema.Remote.HttpClient (graphql_query v0.6.2)

View Source

HTTP client for fetching remote GraphQL schemas using Req.

Builds a %Req.Request{}, optionally passes it through a schema module's build_request/1 callback for customization (auth, headers, etc.), then executes it.

Request Customization

When a schema module is provided, its build_request/1 function is called with the %Req.Request{} before execution. This allows adding authentication, custom headers, or any other request modifications.

See GraphqlQuery.Schema for documentation on how to override build_request/1.

Summary

Functions

Fetches content from a URL using HTTP GET.

Fetches a schema via GraphQL introspection.

Functions

fetch(url, schema_module \\ nil)

@spec fetch(String.t(), module() | nil) :: {:ok, String.t()} | {:error, String.t()}

Fetches content from a URL using HTTP GET.

Optionally accepts a schema module whose build_request/1 callback will be called to customize the request before sending.

Returns {:ok, body} on success or {:error, reason} on failure.

Examples

iex> GraphqlQuery.Schema.Remote.HttpClient.fetch("https://example.com/schema.graphql")
{:ok, "type Query { hello: String }"}

iex> GraphqlQuery.Schema.Remote.HttpClient.fetch("https://example.com/schema.graphql", MyApp.Schema)
{:ok, "type Query { hello: String }"}

introspect(url, schema_module \\ nil)

@spec introspect(String.t(), module() | nil) ::
  {:ok, String.t()} | {:error, String.t()}

Fetches a schema via GraphQL introspection.

Sends the standard introspection query as a POST request to the given URL, then converts the JSON response to SDL using GraphqlQuery.Schema.Remote.Introspection.

Optionally accepts a schema module whose build_request/1 callback will be called to customize the request before sending (e.g., for authentication).

Returns {:ok, sdl} on success or {:error, reason} on failure.

Examples

iex> GraphqlQuery.Schema.Remote.HttpClient.introspect("https://api.example.com/graphql")
{:ok, "type Query { ... }"}

iex> GraphqlQuery.Schema.Remote.HttpClient.introspect("https://api.example.com/graphql", MyApp.Schema)
{:ok, "type Query { ... }"}