GraphQL.Query (GraphQL Client v0.2.0) View Source

Functions to create and modify query representations.

Link to this section Summary

Types

t()

A struct that represents a GraphQL query or mutation.

Functions

Adds a field to a query.

Adds a fragment to a query.

Add a new variable to an existing query

Combine two queries into one query, merging fields, variables and fragments.

Combines a list of queries into one query, merging fields, variables and fragments.

Creates a new query struct for a 'mutation' operation from a keyword list.

Creates a new query struct for a 'query' operation from a keyword list.

Link to this section Types

Specs

t() :: %GraphQL.Query{
  fields: [GraphQL.Node.t()],
  fragments: [GraphQL.Node.t()] | nil,
  name: String.t(),
  operation: :query | :mutation,
  variables: [GraphQL.Variable.t()] | nil
}

A struct that represents a GraphQL query or mutation.

The :operation field can be :query, for a query operation, or :mutation, for a mutation operation.

The :name field is the name of the query or mutation. GraphQL does not require a name for operations, but this struct will enforce its presence in order to enrich trace and logging information.

The :fields field is a list of GraphQL.Node structs. This the list of roof fields of a query or mutation.

The :fragments field is also a list of GraphQL.Node structs, but intended to only keep fragment nodes, as they are usually placed after the root fields in a typical GraphQL query/mutation.

The :variables fields is a list of GraphQL.Variable structs, that represents the expected variables during the request. Note that this list is the definition of variables, not the values of them.

Link to this section Functions

Specs

add_field(t(), GraphQL.Node.t()) :: t()

Adds a field to a query.

The field argument must be a GraphQL.Node struct and its :node_type must be :field.

Examples

iex> f1 = GraphQL.Node.field(:field)
%GraphQL.Node{node_type: :field, name: :field}
iex> f2 = GraphQL.Node.field(:other_field)
%GraphQL.Node{node_type: :field, name: :other_field}
iex> q = %GraphQL.Query{operation: :query, name: "MyQuery", fields: [f1]}
%GraphQL.Query{operation: :query, name: "MyQuery", fields: [f1]}
iex> add_field(q, f2)
%GraphQL.Query{name: "MyQuery", operation: :query, fields: [f2, f1]}
Link to this function

add_fragment(query, fragment)

View Source

Specs

add_fragment(t(), GraphQL.Node.t()) :: t()

Adds a fragment to a query.

The field argument must be a GraphQL.Node struct and its :node_type must be :field.

Examples

iex> f1 = GraphQL.Node.fragment("personFields", "Person", [GraphQL.Node.field(:field)])
%GraphQL.Node{node_type: :fragment, name: "personFields", type: "Person", nodes: [%GraphQL.Node{node_type: :field, name: :field}]}
iex> f2 = GraphQL.Node.fragment("userFields", "User", [GraphQL.Node.field(:another_field)])
%GraphQL.Node{node_type: :fragment, name: "userFields", type: "User", nodes: [%GraphQL.Node{node_type: :field, name: :another_field}]}
iex> q = %GraphQL.Query{operation: :query, name: "MyQuery", fields: [], fragments: [f1]}
%GraphQL.Query{operation: :query, name: "MyQuery", fields: [], fragments: [f1]}
iex> add_fragment(q, f2)
%GraphQL.Query{name: "MyQuery", operation: :query, fields: [], fragments: [f2, f1]}
Link to this function

add_variable(query, variable)

View Source

Specs

add_variable(t(), GraphQL.Variable.t()) :: t()

Add a new variable to an existing query

Examples

iex> v1 = %GraphQL.Variable{name: "id", type: "Integer"}
%GraphQL.Variable{name: "id", type: "Integer"}
iex> v2 = %GraphQL.Variable{name: "slug", type: "String"}
%GraphQL.Variable{name: "slug", type: "String"}
iex> q = %GraphQL.Query{operation: :query, name: "MyQuery", fields: [], variables: [v1]}
%GraphQL.Query{operation: :query, name: "MyQuery", fields: [], variables: [v1]}
iex> add_variable(q, v2)
%GraphQL.Query{operation: :query, name: "MyQuery", fields: [], variables: [v2, v1]}
Link to this function

merge(query_a, query_b, name)

View Source

Specs

merge(t(), t(), String.t()) :: {:ok, t()} | {:error, any()}

Combine two queries into one query, merging fields, variables and fragments.

The two queries must have the same operation.

Link to this function

merge_many(queries, name \\ nil)

View Source

Specs

merge_many([t()], String.t()) :: {:ok, t()} | {:error, any()}

Combines a list of queries into one query, merging fields, variables and fragments.

All queries must have the same operation.

Specs

mutation(Keyword.t()) :: t()

Creates a new query struct for a 'mutation' operation from a keyword list.

Specs

query(Keyword.t()) :: t()

Creates a new query struct for a 'query' operation from a keyword list.