Skip to content

Fields and Directives

The Fields class is a powerful class for making nested or pre-configured field selections. In combination with the Field class you can create every query you need.

Using the Fields Class

from qlient.core import Fields

nested_fields = Fields(
    "first_name",  # will be converted to Field("first_name")
    "last_name",  # will be converted to Field("first_name")
    # This will be converted to
    # Field(
    #   "hobby",
    #   _sub_fields=Fields(
    #       Field("name"),
    #       Field("club", _sub_fields=Fields(Field("name")))
    #   )
    # )
    hobby=Fields("name", club="name"),
)

# last_name first_name hobby { name club { name } }

Supported Operators

Both, the Field and Fields class currently support the following operators:

Addition

from qlient.core import Fields, Field

name_fields = Fields("first_name", "last_name")
age_field = Field("age")

person_fields = name_fields + age_field

# { first_name last_name age }

Combination of Fields and Field

from qlient.core import Fields, Field, Directive

my_home_world_selection = Fields("id", "name", "population")

my_person_selection = Fields(
    "id",  # will be converted to Field("id")
    Field("name"),
    Field("height", _alias="my_height"),
    Field(
        "homeworld",
        _sub_fields=my_home_world_selection,
        _directive=Directive("include"),
    ),
)

# Every variable name used in a Field or Directive
# is automatically generated and therefore unique.
# {
#     id
#     name
#     my_height: height
#     homeworld @include {
#         id
#         name
#         population
#     }
# }