<commit_before>Defining the schema
===================
GraphQL requires a graph-like schema to query against.
The nodes and edges of the graph will be
the objects and relationships in your API.

Using the Star Wars example from the GraphQL documentation_,
let's assume we have a Django app with the following model structure:

- Characters appear in Episodes.
- Characters are friends with other Characters.

Adding nodes - Objects
----------------------

Create an Object node for each of the models:
::

    from django_graph_api import (
        Object,
    )

    class Episode(Object):
        ...

    class Character(Object):
        ...

Add scalar fields to each of the nodes:
::

    from django_graph_api import (
        Object,
        CharField,
        IntegerField,
    )

    class Episode(Object):
        name = CharField()
        number = IntegerField()

    class Character(Object):
        name = CharField()

You can define any field on the node (Object)
that is also a **field** or **property** of the model
that it represents.
You can also define custom logic to get a field's value by adding a ``get_<field_name>`` method to the object.
The current model instance will be available as ``self.data``.

Arguments can be defined for fields by passing in `<argname>=<graphql type instance>`.
The value passed in a query will be available as a keyword argument to the field value method if there is one.

::
    from django_graph_api.graphql.types import Boolean

    class Character(Object):
        name = CharField(upper=Boolean())

        def get_name(self, upper=False):
            name = '{} {}'.format(
                self.data.first_name,
                self.data.last_name,
            )
            if upper:
                return name.upper()
            return name

Scalar field types
^^^^^^^^^^^^^^^^^^
For scalar types,
the type of the field determines how it will be returned by the API.

For example, if a model's field is stored as an ``IntegerField`` on the Django model
and defined as a ``CharField`` in the graph API,
the model value will be coerced from an ``int`` to a ``str`` type
when it is resolved.<commit_msg>Fix docs/define_schema code block<commit_after>Defining the schema
===================
GraphQL requires a graph-like schema to query against.
The nodes and edges of the graph will be
the objects and relationships in your API.

Using the Star Wars example from the GraphQL documentation_,
let's assume we have a Django app with the following model structure:

- Characters appear in Episodes.
- Characters are friends with other Characters.

Adding nodes - Objects
----------------------

Create an Object node for each of the models:
::

    from django_graph_api import (
        Object,
    )

    class Episode(Object):
        ...

    class Character(Object):
        ...

Add scalar fields to each of the nodes:
::

    from django_graph_api import (
        Object,
        CharField,
        IntegerField,
    )

    class Episode(Object):
        name = CharField()
        number = IntegerField()

    class Character(Object):
        name = CharField()

You can define any field on the node (Object)
that is also a **field** or **property** of the model
that it represents.
You can also define custom logic to get a field's value by adding a ``get_<field_name>`` method to the object.
The current model instance will be available as ``self.data``.

Arguments can be defined for fields by passing in `<argname>=<graphql type instance>`.
The value passed in a query will be available as a keyword argument to the field value method if there is one.
::
    from django_graph_api.graphql.types import Boolean

    class Character(Object):
        name = CharField(upper=Boolean())

        def get_name(self, upper=False):
            name = '{} {}'.format(
                self.data.first_name,
                self.data.last_name,
            )
            if upper:
                return name.upper()
            return name

Scalar field types
^^^^^^^^^^^^^^^^^^
For scalar types,
the type of the field determines how it will be returned by the API.

For example, if a model's field is stored as an ``IntegerField`` on the Django model
and defined as a ``CharField`` in the graph API,
the model value will be coerced from an ``int`` to a ``str`` type
when it is resolved.
{'fraction_non_alphanumeric': 0.09592822636300898, 'fraction_numerical': 0.0, 'mean_word_length': 2.7162393162393164, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3790056', 'n_tokens_mistral': 1139, 'n_tokens_neox': 1099, 'n_words': 538}