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

Functions to simplify the creation of GraphQL queries.

The easiest way to use these functions is to import this module directly, this way you'll have all you need to build a query.

Helper functions

Writing queries and mutations

As an example, consider the following GraphQL request:

query UserQuery($id: Integer = 1) {
  user (id: $id) {
    id
    email
    ...personFields
  }
}

fragment personField on Person {
  firstName
  lastName
}

Using the functions in this module, you can create a representation of this query in this way:

q = query("UserQuery", %{id: {"Integer", 1}}, [
  field(:user, %{}, [
    field(:id)
    field(:email),
    fragment("personFields")
  ])
], [
  fragment("personFields", "Person", [
    field("firstName"),
    field("lastName")
  ])
])

Link to this section Summary

Functions

Creates a reference to a fragment. Use it inside a field.

Creates a fragment. Use it on the query level.

Creates an inline fragment. Use it inside a field.

Creates a new GraphQL.Query struct, for a :mutation operation

Creates a new GraphQL.Query struct, for a :query operation.

Link to this section Functions

Specs

enum(String.t()) :: {:enum, String.t()}
Link to this function

field(name, args \\ nil, fields \\ nil, directives \\ nil)

View Source

Creates a field.

When rendered, it will have the following body:

  1. A simple field, no arguments or sub fields

    fieldName
  2. A field with an alias

    fieldAlias: fieldName
  3. A field with arguments

    fieldName(arg: value)
  4. A field with sub fields

    fieldName {
      subField
    }
  5. A field an alias, arguments and sub fields

    fieldAlias: fieldName (arg: value) {
      subField
    }

Examples

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

iex> field({:some_field, "fieldAlias"})
%GraphQL.Node{node_type: :field, name: :some_field, alias: "fieldAlias"}

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

Specs

fragment(String.t()) :: GraphQL.Node.t()

Creates a reference to a fragment. Use it inside a field.

When rendered, it will generate the following body:

...fragmentName

Examples

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

fragment(name, type, fields)

View Source

Specs

fragment(String.t(), String.t(), list()) :: GraphQL.Node.t()

Creates a fragment. Use it on the query level.

When rendered, it will generate the following body:

... fragmentName on Type {
  field1
  field2
}

Examples

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

inline_fragment(type, fields)

View Source

Specs

inline_fragment(String.t(), list()) :: GraphQL.Node.t()

Creates an inline fragment. Use it inside a field.

When rendered, it will generate the following body:

... on Type {
  field1
  field2
}

Examples

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

mutation(name, variables, fields, fragments \\ [])

View Source

Specs

mutation(String.t(), map(), list(), list()) :: GraphQL.Query.t()

Creates a new GraphQL.Query struct, for a :mutation operation

Link to this function

query(name, variables, fields, fragments \\ [])

View Source

Specs

query(String.t(), map(), list(), list()) :: GraphQL.Query.t()

Creates a new GraphQL.Query struct, for a :query operation.

Link to this function

var(name, type, value \\ nil)

View Source

Specs

var(any(), any(), any()) :: GraphQL.Variable.t()

Creates a GraphQL.Variable struct.