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
query/4
- creates a new "query" operationmutation/4
- creates a new "mutation" operationfield/3
- creates a new field (optionals: variables and subfields)fragment/1
- creates a reference to a fragmentfragment/3
- creates a fragmentinline_fragment/2
- creates an inline fragment
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 field.
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.
Creates a GraphQL.Variable
struct.
Link to this section Functions
Specs
Creates a field.
When rendered, it will have the following body:
A simple field, no arguments or sub fields
fieldName
A field with an alias
fieldAlias: fieldName
A field with arguments
fieldName(arg: value)
A field with sub fields
fieldName { subField }
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}
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}]}
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}]}
Specs
mutation(String.t(), map(), list(), list()) :: GraphQL.Query.t()
Creates a new GraphQL.Query
struct, for a :mutation
operation
Specs
query(String.t(), map(), list(), list()) :: GraphQL.Query.t()
Creates a new GraphQL.Query
struct, for a :query
operation.
Specs
var(any(), any(), any()) :: GraphQL.Variable.t()
Creates a GraphQL.Variable
struct.