Created
May 4, 2018 09:29
-
-
Save orasik/61aae5578418a68cdc753d288c61ce17 to your computer and use it in GitHub Desktop.
Graphene Example
This file contains 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
# Graphine Example | |
import graphene | |
import json | |
import string | |
import random | |
class User(graphene.ObjectType): | |
id = graphene.ID() | |
name = graphene.String() | |
class Query(graphene.ObjectType): | |
user = graphene.Field(User, id=graphene.Int()) | |
username = graphene.Field(User, username=graphene.String()) | |
all = graphene.Field(User) | |
def resolve_user(self, info, id): | |
return User(random.randint(1,45455), ''.join(random.choices(string.ascii_uppercase + string.digits, k=12))) | |
def resolve_username(self, info, username): | |
return User(random.randint(1,45455),username) | |
def resolve_all(self, info): | |
return User(random.randint(1,45455), "Someone") | |
schema = graphene.Schema(query=Query) | |
query = ''' | |
query q1{ | |
username(username: "Hello") { | |
id | |
name | |
} | |
} | |
''' | |
print(json.dumps(schema.execute(query).data)) | |
query = ''' | |
query q1{ | |
user(id: 4) { | |
id | |
name | |
} | |
} | |
''' | |
print(json.dumps(schema.execute(query).data)) | |
query = ''' | |
query whatever{ | |
all { | |
name | |
} | |
} | |
''' | |
print(json.dumps(schema.execute(query).data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment