File: context_example.py

package info (click to toggle)
python-graphene 3.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,120 kB
  • sloc: python: 8,935; makefile: 214; sh: 18
file content (35 lines) | stat: -rw-r--r-- 698 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
import graphene


class User(graphene.ObjectType):
    id = graphene.ID()
    name = graphene.String()


class Query(graphene.ObjectType):
    me = graphene.Field(User)

    def resolve_me(root, info):
        return info.context["user"]


schema = graphene.Schema(query=Query)
query = """
    query something{
      me {
        id
        name
      }
    }
"""


def test_query():
    result = schema.execute(query, context={"user": User(id="1", name="Syrus")})
    assert not result.errors
    assert result.data == {"me": {"id": "1", "name": "Syrus"}}


if __name__ == "__main__":
    result = schema.execute(query, context={"user": User(id="X", name="Console")})
    print(result.data["me"])