-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.py
More file actions
88 lines (83 loc) · 2.46 KB
/
schema.py
File metadata and controls
88 lines (83 loc) · 2.46 KB
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
88
# Pure GraphQL schema using graphql-core
# -----------------------------------------------
from datetime import datetime
from graphql import (
GraphQLSchema,
GraphQLObjectType,
GraphQLField,
GraphQLArgument,
GraphQLString,
GraphQLList,
GraphQLNonNull,
GraphQLID,
)
from data import USERS # In-memory user mock data
# --------------------------
# User GraphQL Type
# Represents a single user object
# --------------------------
UserType = GraphQLObjectType(
name="User",
fields=lambda: {
"id": GraphQLField(GraphQLNonNull(GraphQLID)),
"name": GraphQLField(GraphQLNonNull(GraphQLString)),
"email": GraphQLField(GraphQLNonNull(GraphQLString)),
},
)
# --------------------------
# Message GraphQL Type
# Represents a message object returned by sendMessage mutation
# --------------------------
MessageType = GraphQLObjectType(
name="Message",
fields=lambda: {
"message": GraphQLField(GraphQLNonNull(GraphQLString)),
"timestamp": GraphQLField(GraphQLNonNull(GraphQLString)),
},
)
# --------------------------
# Query Root
# Defines read-only queries for the API
# --------------------------
QueryType = GraphQLObjectType(
name="Query",
fields=lambda: {
"hello": GraphQLField(
GraphQLNonNull(GraphQLString),
resolve=lambda *_: "Hello from Python GraphQL!",
),
"users": GraphQLField(
GraphQLNonNull(GraphQLList(GraphQLNonNull(UserType))),
resolve=lambda *_: USERS,
),
"user": GraphQLField(
UserType,
args={"id": GraphQLArgument(GraphQLNonNull(GraphQLID))},
resolve=lambda _root, _info, id: next(
(u for u in USERS if str(u["id"]) == str(id)), None
),
),
},
)
# --------------------------
# Mutation Root
# Defines write operations (mutations) for the API
# --------------------------
MutationType = GraphQLObjectType(
name="Mutation",
fields=lambda: {
"sendMessage": GraphQLField(
MessageType,
args={"message": GraphQLArgument(GraphQLNonNull(GraphQLString))},
resolve=lambda _root, _info, message: {
"message": message,
"timestamp": datetime.utcnow().isoformat(),
},
)
},
)
# --------------------------
# GraphQL Schema
# Combines Query and Mutation types
# --------------------------
schema = GraphQLSchema(query=QueryType, mutation=MutationType)