View Source GraphQLDocument.Fragment (GraphQLDocument v0.2.2)

Fragments are the primary unit of composition in GraphQL.

Fragments allow for the reuse of common repeated selections of fields, reducing duplicated text in the document. Inline Fragments can be used directly within a selection to condition upon a type condition when querying against an interface or union.

See render_definitions/1 for details about rendering FragmentDefinitions.

See render/2 for details about rendering a FragmentSpread or InlineFragment.

Link to this section Summary

Types

These are given in the fragments key of the Operation options. (See GraphQLDocument.Operation.option/0.)

An inline fragment is a fragment that doesn't have a definition; the definition appears right in the middle of a selection set in the query/mutation/subscription.

The name of a fragment. An atom or string.

A fragment spread is injecting ...fragmentName into a request to instruct the server to return the fields in the fragment.

t()

The type to which the fragment applies.

Functions

Returns a FragmentSpread or InlineFragment as an iolist to be inserted in a Document.

Returns the given fragment definitions as iodata to be rendered in a GraphQL document.

Link to this section Types

These are given in the fragments key of the Operation options. (See GraphQLDocument.Operation.option/0.)

This is not the usage of the Fragment (in a Selection set) but rather defining to be used in the rest of the Document.

examples

Examples

GraphQLDocument.query(
  [...],
  fragments: [
    friendFields: {
      on(User),
      [
        :id,
        :name,
        profilePic: field(args: [size: 50])
      ]
    }
  ]
)

An inline fragment is a fragment that doesn't have a definition; the definition appears right in the middle of a selection set in the query/mutation/subscription.

It exists to support requesting certain fields only if the object is a certain type (for objects of a union type), or to apply directives to only a subset of fields.

@type name() :: GraphQLDocument.Name.t()

The name of a fragment. An atom or string.

@type spread() :: {:..., name()} | {:..., {name(), [GraphQLDocument.Directive.t()]}}

A fragment spread is injecting ...fragmentName into a request to instruct the server to return the fields in the fragment.

Fragment spreads are expressed with the :... atom to match GraphQL syntax. They are often inserted among other fields as in ...: :friendFields below.

GraphQLDocument.query(
  [
    self: [
      :name,
      :email,
      ...: :friendFields
    ]
  ],
  fragments: [
    friendFields: {...}
  ]
)
@type t() :: {:..., spread() | inline()}
@type type_condition() :: {:on, GraphQLDocument.Name.t()}

The type to which the fragment applies.

{:on, Person} is rendered as "on Person".

Instead of using {:on, type} tuples directly, you can use GraphQLDocument.on/1:

iex> import GraphQLDocument
iex> on(Person)
{:on, Person}

Link to this section Functions

Link to this function

render(fragment, indent_level)

View Source
@spec render(t(), non_neg_integer()) :: iolist()

Returns a FragmentSpread or InlineFragment as an iolist to be inserted in a Document.

fragment-spreads

Fragment Spreads

To express a Fragment Spread, provide the name of the fragment as an atom or string. If there are directives, provide a {name, directives} tuple.

examples

Examples

iex> render(:friendFields, 1)
...> |> IO.iodata_to_binary()
"...friendFields"

iex> render({:friendFields, [skip: [if: {:var, :antisocial}]]}, 1)
...> |> IO.iodata_to_binary()
"...friendFields @skip(if: $antisocial)"

inline-fragments

Inline Fragments

To express an Inline Fragment, provide an {{:on, Type}, selections} tuple. If there are directives, provide {{:on, Type}, directives, selections}.

The {:on, Type} syntax can be substituted with GraphQLDocument.on/1:

on(Type)

examples-1

Examples

iex> render(
...>   {
...>     :friendFields,
...>     [include: [if: {:var, :expanded}]]
...>   },
...>   1
...> )
...> |> IO.iodata_to_binary()
"...friendFields @include(if: $expanded)"

iex> render([:foo, :bar], 1)
...> |> IO.iodata_to_binary()
"""
... {
  foo
  bar
}\
"""

iex> render({[log: [level: "warn"]], [:foo, :bar]}, 1)
...> |> IO.iodata_to_binary()
"""
... @log(level: "warn") {
  foo
  bar
}\
"""

iex> render(
...>   {
...>     {:on, Person},
...>     [:name, friends: [:name, :city]]
...>   },
...>   1
...> )
...> |> IO.iodata_to_binary()
"""
... on Person {
  name
  friends {
    name
    city
  }
}\
"""

iex> render(
...>   {
...>     {:on, Person},
...>     [:log],
...>     [:name, friends: [:name, :city]]
...>   },
...>   1
...> )
...> |> IO.iodata_to_binary()
"""
... on Person @log {
  name
  friends {
    name
    city
  }
}\
"""
Link to this function

render_definitions(fragments)

View Source
@spec render_definitions([definition()]) :: iolist()

Returns the given fragment definitions as iodata to be rendered in a GraphQL document.

examples

Examples

iex> render_definitions(friendFields: {
...>   {:on, User},
...>   [:id, :name, profilePic: {[size: 50], []}]
...> })
...> |> IO.iodata_to_binary()
"""
\n\nfragment friendFields on User {
  id
  name
  profilePic(size: 50)
}\
"""