python ariadne usage
Last active
April 6, 2021 03:44
-
-
Save allenyang79/abd8f5543a2a48ab322d4091896b15f2 to your computer and use it in GitHub Desktop.
python ariadne usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query = QueryType() | |
type_defs = gql(""" | |
type Query { | |
hello: String! | |
} | |
""") | |
# ...and assign our resolver function to its "hello" field. | |
@query.field("hello") | |
def resolve_hello(_, info): | |
# print(info) | |
# print(info.context) | |
# return "oops" | |
request = info.context["request"] | |
user_agent = request.headers.get("user-agent", "guest") | |
return "Hello, %s!" % user_agent | |
schema = make_executable_schema(type_defs, query) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type_defs = gql(""" | |
schema { | |
query: Query | |
} | |
scalar Datetime | |
type User { | |
id: String! | |
email: String! | |
age: Int! | |
size: Size! | |
} | |
type Size { | |
width: Int! | |
height: Int! | |
} | |
type Query { | |
hello: String! | |
sum(x: Int, y: Int): Int! | |
now: Datetime | |
user(email: String): User | |
} | |
""") | |
query = QueryType() # ObjectType("Query") | |
user = ObjectType("User") | |
@query.field("hello") | |
def resolve_hello(*_): | |
return "Hello!" | |
@query.field("sum") | |
def resolve_sum(obj: Any, info:GraphQLResolveInfo, x: int, y: int): | |
# obj: Any, info: GraphQLResolveInfo | |
return x + y | |
@query.field("user") | |
def resolve_user(obj: Any, info:GraphQLResolveInfo, email: str): | |
return { | |
'email': "email", | |
'age': 0, | |
'size': { | |
'width': 10, | |
'height': 20, | |
} | |
} | |
@user.field("id") | |
def resolve_user_id(obj: Any, info: GraphQLResolveInfo): | |
pprint.pprint(obj) | |
email = obj['email'] | |
_id = email.split("@")[0] | |
return _id | |
datetime_scalar = ScalarType("Datetime") | |
@datetime_scalar.serializer | |
def datetime_serializer(value): | |
print("QQ", value) | |
return value.isoformat() | |
@query.field("now") | |
def resolve_now(obj: Any, info: GraphQLResolveInfo): | |
return arrow.get() | |
schema = make_executable_schema(type_defs, [query, user, datetime_scalar]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GraphQLResolveInfo(field_name='sum', | |
field_nodes=[FieldNode at 12:27], | |
return_type=<GraphQLNonNull <GraphQLScalarType 'Int'>>, | |
parent_type=<GraphQLObjectType 'Query'>, | |
path=ResponsePath(prev=None, key='sum'), | |
schema=<graphql.type.schema.GraphQLSchema object at 0x1092b5ac8>, | |
fragments={}, | |
root_value=None, | |
operation=OperationDefinitionNode at 0:29, | |
variable_values={}, | |
context={'request': <Request 'http://127.0.0.1:5000/graphql' [POST]>}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment