Skip to content

Commit

Permalink
Merge pull request #168 from evo-company/fix-docs
Browse files Browse the repository at this point in the history
fix docs all over the place
  • Loading branch information
kindermax authored Sep 16, 2024
2 parents b2719ae + 441252a commit fd2e523
Show file tree
Hide file tree
Showing 17 changed files with 586 additions and 166 deletions.
8 changes: 4 additions & 4 deletions docs/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This is the simplest :py:class:`~hiku.graph.Graph` with one

Then this field could be queried using this query:

.. code-block::
.. code-block:: graphql
{ now }
Expand Down Expand Up @@ -98,7 +98,7 @@ node. This function should return character ids.

So now you are able to try this query in the console:

.. code-block::
.. code-block:: graphql
{ characters { name species } }
Expand Down Expand Up @@ -163,8 +163,8 @@ finite structure and result follows it's structure.
:lines: 106-127
:dedent: 4
:linenos:
:emphasize-lines: 11,13,15
:emphasize-lines: 9,11,13

As you can see, there are duplicate entries in the result :sup:`[11,13,15]` --
As you can see, there are duplicate entries in the result :sup:`[9,11,13]` --
this is how our cycle can be seen, the same character `Spock` seen multiple
times.
2 changes: 1 addition & 1 deletion docs/basics/test_stage1.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def execute(graph, query_string):
@patch('{}.datetime'.format(__name__))
def test(dt):
dt.now = Mock(return_value=_NOW)
result = execute(GRAPH, '[:now]')
result = execute(GRAPH, '{ now }')
assert result == {'now': '2015-10-21T07:28:00'}

# console
Expand Down
10 changes: 5 additions & 5 deletions docs/caching.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Consider this graph:
So in next 3 queries `company` node will be cached separately:

.. code-block::
.. code-block:: graphql
query {
product(id: 1) {
Expand All @@ -63,7 +63,7 @@ So in next 3 queries `company` node will be cached separately:
``company`` here has ``name`` field, which is the difference between this query and
previous one.

.. code-block::
.. code-block:: graphql
query {
product(id: 1) {
Expand All @@ -79,7 +79,7 @@ previous one.
previous one.


.. code-block::
.. code-block:: graphql
query {
product(id: 1) {
Expand All @@ -103,7 +103,7 @@ When we fetching data from cache, we basically restoring parts of index from cac

For example, if we have this query:

.. code-block::
.. code-block:: graphql
query {
products {
Expand Down Expand Up @@ -301,7 +301,7 @@ How to specify cache on client

Use `@cached` directive on any non root node.

.. code-block::
.. code-block:: graphql
query Products {
products {
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
.. toctree::
:maxdepth: 2

changes_08
changes_07
changes_06
changes_05
Expand Down
26 changes: 26 additions & 0 deletions docs/endpoints/async_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from hiku.types import String
from hiku.graph import Graph, Root, Field
from hike.schema import Schema
from hiku.endpoint.graphql import AsyncGraphQLEndpoint
from hiku.executors.asyncio import AsyncIOExecutor

async def say_hello(fields):
return ['Hello World!' for _ in fields]

QUERY_GRAPH = Graph([
Root([Field('hello', String, say_hello)]),
])

schema = Schema(AsyncIOExecutor(), QUERY_GRAPH)

endpoint = AsyncGraphQLEndpoint(schema)

assert await endpoint.dispatch({
'query': "{ hello }",
'variables': None,
'operationName': "GetHello",
}) == {
'data': {
'hello': 'Hello World!',
},
}
26 changes: 26 additions & 0 deletions docs/endpoints/sync_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from hiku.graph import Graph, Root, Field
from hiku.types import String
from hiku.schema import Schema
from hiku.executors.sync import SyncExecutor
from hiku.endpoint.graphql import GraphQLEndpoint

def say_hello(fields):
return ['Hello World!' for _ in fields]

QUERY_GRAPH = Graph([
Root([Field('hello', String, say_hello)]),
])

schema = Schema(SyncExecutor(), QUERY_GRAPH)

endpoint = GraphQLEndpoint(schema)

assert endpoint.dispatch({
'query': "{ hello }",
'variables': None,
'operationName': "GetHello",
}) == {
'data': {
'hello': 'Hello World!',
},
}
104 changes: 54 additions & 50 deletions docs/enums.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ Enums

Enums are a special types that can be used to define a set of possible values for a field.

In graphql you can use enum type like this:
In graphql you can define enum type and use it like this:

.. code-block::
.. code-block:: graphql
enum Status {
ACTIVE
DELETED
}
type Usr {
type User {
id: ID!
status: Status!
}
Expand All @@ -24,73 +24,77 @@ In graphql you can use enum type like this:
}
Enum from string
----------------
Define enum
-----------

The simplest way to create enum in `hiku` from a list of strings:
.. tab:: Enum from string

.. code-block:: python
The simplest way to create enum in `hiku` from a list of strings:

from hiku.enum import Enum
.. code-block:: python
Enum('Status', ['ACTIVE', 'DELETED'])
from hiku.enum import Enum
Enum from python Enum
---------------------
Enum('Status', ['ACTIVE', 'DELETED'])
You can also create enum from builtin python ``Enum`` type:
.. tab:: Enum from python Enum

.. code-block:: python
You can also create enum from builtin python ``Enum`` type:

from enum import Enum as PyEnum
from hiku.enum import Enum
.. code-block:: python
class Status(PyEnum):
ACTIVE = 'active'
DELETED = 'deleted'
from enum import Enum as PyEnum
from hiku.enum import Enum
Enum.from_builtin(Status)
class Status(PyEnum):
ACTIVE = 'active'
DELETED = 'deleted'
In this example:
Enum.from_builtin(Status)
- ``EnumFromBuiltin`` will use ``Enum.__name__`` as a enum name.
- ``EnumFromBuiltin`` will use ``Enum.__members__`` to get a list of possible values.
- ``EnumFromBuiltin`` will use ``member.name`` to get a value name:
In this example:

So this python enum:
- ``EnumFromBuiltin`` will use ``Enum.__name__`` as a enum name.
- ``EnumFromBuiltin`` will use ``Enum.__members__`` to get a list of possible values.
- ``EnumFromBuiltin`` will use ``member.name`` to get a value name:

.. code-block:: python
So this python enum:

class Status(PyEnum):
ACTIVE = 1
DELETED = 2
.. code-block:: python
is equivalent to this enum in graphql:
class Status(PyEnum):
ACTIVE = 1
DELETED = 2
.. code-block:: python
is equivalent to this enum in graphql:

enum Status { ACTIVE, DELETED }
.. code-block:: python
If you want to specify different name you can pass ``name`` argument to ``Enum.from_builtin`` method:
enum Status { ACTIVE, DELETED }
.. code-block:: python
If you want to specify different name you can pass ``name`` argument to ``Enum.from_builtin`` method:

Enum.from_builtin(Status, name='User_Status')
.. code-block:: python
.. note::
Enum.from_builtin(Status, name='User_Status')
If you use builtin python ``Enum``, then you MUST return enum value from the resolver function, otherwise ``hiku`` will raise an error.
.. note::

.. code-block:: python
If you use builtin python ``Enum``, then you MUST return enum value from the resolver function, otherwise ``hiku`` will raise an error.

def user_fields_resolver(fields, ids):
def get_field(field, user):
if field.name == 'id':
return user.id
elif field.name == 'status':
return Status(user.status)
.. code-block:: python
return [[get_field(field, users[id]) for field in fields] for id in ids]
def user_fields_resolver(fields, ids):
def get_field(field, user):
if field.name == 'id':
return user.id
elif field.name == 'status':
return Status(user.status)
return [[get_field(field, users[id]) for field in fields] for id in ids]
Use enum
--------

Lets look at the full example on how to use enum type in `hiku`:

Expand Down Expand Up @@ -138,7 +142,7 @@ Lets decode the example above:

If we run this query:

.. code-block:: python
.. code-block:: graphql
query {
user {
Expand All @@ -149,11 +153,11 @@ If we run this query:
We will get the following result:

.. code-block::
.. code-block:: json
{
'id': "1",
'status': 'ACTIVE',
"id": "1",
"status": "ACTIVE",
}
Expand Down Expand Up @@ -247,7 +251,7 @@ You can use enum as an field input argument:
Now you can use enum as a field argument:

.. code-block::
.. code-block:: graphql
query {
users(status: DELETED) {
Expand All @@ -258,7 +262,7 @@ Now you can use enum as a field argument:
The result will be:

.. code-block::
.. code-block:: json
[{
"id": "2",
Expand Down
Loading

0 comments on commit fd2e523

Please sign in to comment.