File: sdl.rst

package info (click to toggle)
graphql-core 3.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,384 kB
  • sloc: python: 45,812; makefile: 26; sh: 13
file content (87 lines) | stat: -rw-r--r-- 2,632 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Using the Schema Definition Language
------------------------------------

.. currentmodule:: graphql.type

Above we defined the GraphQL schema as Python code, using the :class:`GraphQLSchema`
class and other classes representing the various GraphQL types.

GraphQL-core 3 also provides a language-agnostic way of defining a GraphQL schema
using the GraphQL schema definition language (SDL) which is also part of the GraphQL
specification. To do this, we simply feed the SDL as a string to the
:func:`~graphql.utilities.build_schema` function in :mod:`graphql.utilities`::

    from graphql import build_schema

    schema = build_schema("""

        enum Episode { NEWHOPE, EMPIRE, JEDI }

        interface Character {
          id: String!
          name: String
          friends: [Character]
          appearsIn: [Episode]
        }

        type Human implements Character {
          id: String!
          name: String
          friends: [Character]
          appearsIn: [Episode]
          homePlanet: String
        }

        type Droid implements Character {
          id: String!
          name: String
          friends: [Character]
          appearsIn: [Episode]
          primaryFunction: String
        }

        type Query {
          hero(episode: Episode): Character
          human(id: String!): Human
          droid(id: String!): Droid
        }
        """)

The result is a :class:`GraphQLSchema` object just like the one we defined above, except
for the resolver functions which cannot be defined in the SDL.

We would need to manually attach these functions to the schema, like so::

    schema.query_type.fields['hero'].resolve = get_hero
    schema.get_type('Character').resolve_type = get_character_type

Another problem is that the SDL does not define the server side values of the
``Episode`` enum type which are returned by the resolver functions and which are
different from the names used for the episode.

So we would also need to manually define these values, like so::

    for name, value in schema.get_type('Episode').values.items():
        value.value = EpisodeEnum[name].value

This would allow us to query the schema built from SDL just like the manually assembled
schema::

    from graphql import graphql_sync

    result = graphql_sync(schema, """
        {
          hero(episode: EMPIRE) {
            name
            appearsIn
          }
        }
        """)
    print(result)

And we would get the expected result::

    ExecutionResult(
        data={'hero': {'name': 'Luke Skywalker',
                       'appearsIn': ['NEWHOPE', 'EMPIRE', 'JEDI']}},
        errors=None)