GraphQL.Node (GraphQL Client v0.2.1) View Source

Functions to create all different types of nodes of a GraphQL operation.

Usually, this module should not be used directly, since it is easier to use the function from GraphQL.QueryBuilder.

Link to this section Summary

Types

A GraphQL identifier that is not a GraphQL keyword (like mutation, query and fragment)

A two-element tuple where the first position is the name of the field and the second element is the alias of the field.

The GraphQL query element that this node represents.

t()

A struct representing a GraphQL operation node.

Functions

Creates a simple field, with no arguments or sub nodes.

Creates a field with arguments and sub nodes.

Creates a reference to a fragment.

Creates a fragment.

Creates an inline fragment.

Link to this section Types

Specs

name() :: String.t() | atom()

A GraphQL identifier that is not a GraphQL keyword (like mutation, query and fragment)

Used to identify fields, aliases and fragments.

Specs

name_and_alias() :: {name(), name()}

A two-element tuple where the first position is the name of the field and the second element is the alias of the field.

Specs

node_type() :: :field | :fragment_ref | :fragment | :inline_fragment

The GraphQL query element that this node represents.

The four node types are:

  • field: a single field of a GraphQL schema, may have arguments and other nodes
  • fragment_ref: a reference to a fragment, used inside fields to import fragment fields
  • fragment: a fragment definition, with name, type and fields
  • inline_fragment: much like a fragment, but being inline, it does not need a name

Specs

t() :: %GraphQL.Node{
  alias: name(),
  arguments: map() | Keyword.t(),
  directives: map() | Keyword.t(),
  name: name(),
  node_type: node_type(),
  nodes: [t()],
  type: String.t()
}

A struct representing a GraphQL operation node.

A %Node{} struct can be represent a field, a fragment, an inline fragment or a fragment reference, identified by the :node_type field.

The name represents how this node is identified within the GraphQL operation.

The alias is only used when the :node_type is :field, and as the name suggests, represents the alias of the field's name.

The arguments is a map with all the arguments used by a node, and it's only valid when thew :node_type is :field.

The type is only used when :node_type is :fragment or :inline_fragment, and represents the GraphQL type of the fragment.

The nodes is a list of child nodes, that can used to query for complex objects.

The directives field is an enum with all the graphQL directives to be applied on a node node.

Link to this section Functions

Specs

field(name() | name_and_alias()) :: t()

Creates a simple field, with no arguments or sub nodes.

The name parameter can be an atom or string, or a two-element tuple with atoms or strings, where the first element is the actual name of the field and the second element is the alias of the field.

GraphQL example

A query with a simple field inside another field:

query {
  user {
    id      <---- Simple field
  }
}

A query with a simple field with an alias:

query {
  user {
    theId: id      <---- Simple field with alias
  }
}

Examples

iex> field(:my_field)
%GraphQL.Node{node_type: :field, name: :my_field}

iex> field({:my_field, "field_alias"})
%GraphQL.Node{node_type: :field, name: :my_field, alias: "field_alias"}
Link to this function

field(name_spec, arguments, nodes, directives \\ nil)

View Source

Specs

field(name() | name_and_alias(), map(), [t()], [any()]) :: t()

Creates a field with arguments and sub nodes.

The name parameter can be an atom or string, or a two-element tuple with atoms or strings, where the first element is the actual name of the field and the second element is the alias of the field.

The arguments parameter is a map.

The nodes argument is a list of %GraphQL.Node{} structs.

GraphQL Example

A query with a field that has arguments, an alias and subfields

query {
  someObject: object(slug: "the-object") {   <----- Field with an alias and arguments
    field                                    <----- Sub field
    anotherField                             <----- Sub field
  }
}

Examples

iex> field(:my_field, %{id: "id"}, [ field(:subfield) ] )
%GraphQL.Node{node_type: :field, name: :my_field, arguments: %{id: "id"}, nodes: [%GraphQL.Node{node_type: :field, name: :subfield}]}

iex> field({:my_field, "field_alias"}, %{id: "id"}, [ field(:subfield) ] )
%GraphQL.Node{node_type: :field, name: :my_field, alias: "field_alias", arguments: %{id: "id"}, nodes: [%GraphQL.Node{node_type: :field, name: :subfield}]}

Specs

fragment(name()) :: t()

Creates a reference to a fragment.

A fragment reference is used inside a field to import the fields of a fragment.

GraphQL Example

query {
  object {
    ...fieldsFromFragment        <----- Fragment Reference
  }
}

Examples

iex> fragment("myFields")
%GraphQL.Node{node_type: :fragment_ref, name: "myFields"}
Link to this function

fragment(name, type, fields)

View Source

Specs

fragment(name(), name(), [t()]) :: t()

Creates a fragment.

A fragment is used to share fields between other fields

GraphQL Example

query {
  object {
    ...fieldsFromFragment
  }
}

fragment fieldsFromFragment on Type {    <------ Fragment
  field1
  field2
}

Examples

iex> fragment("myFields", "SomeType", [field(:field)])
%GraphQL.Node{node_type: :fragment, name: "myFields", type: "SomeType", nodes: [%GraphQL.Node{node_type: :field, name: :field}]}
Link to this function

inline_fragment(type, fields)

View Source

Specs

inline_fragment(name(), [t()]) :: t()

Creates an inline fragment.

An inline fragment is used to conditionally add fields on another field depending on its type

GraphQL Example

query {
  object {
    ... on Type {          <------  Inline Fragment
      field1
      field2
    }
  }
}

Examples

iex> inline_fragment("SomeType", [field(:field)])
%GraphQL.Node{node_type: :inline_fragment, type: "SomeType", nodes: [%GraphQL.Node{node_type: :field, name: :field}]}