Skip to content

Instantly share code, notes, and snippets.

@alexanderhupfer
Created April 5, 2021 19:04
Show Gist options
  • Save alexanderhupfer/4602edfdd99f39ecfa21d8142a775b0c to your computer and use it in GitHub Desktop.
Save alexanderhupfer/4602edfdd99f39ecfa21d8142a775b0c to your computer and use it in GitHub Desktop.
Customising a python-graphene SQLAlchemy Query
class Configuration(graphene.ObjectType):
#ID! definition to be ommitted, generated by node interface
enabled = graphene.Boolean()
value = graphene.Int()
class Meta:
interfaces = (relay.Node, )
def query_Configuration(user_id):
configuration = get_user_configuration(user_id)
result = {
'enabled' : configuration.enabled,
'value' : configuration.value,
'id': configuration.id
}
return result
class User(SQLAlchemyObjectType):
class Meta:
model = UserModel
configuration = graphene.Field(lambda: Confguration)
def resolve_configuration(self, info, **kwargs):
return resolve_configuration(user_id=self.id)
class Query(graphene.ObjectType):
node = relay.Node.Field()
user = graphene.Field(User,
id=graphene.String(required=True))
def resolve_user(
args,
info,
id, **kwargs):
user = SqlAlchermyQuery(id) # return UserModel from sqlAlchemy
return user
schema = graphene.Schema(query=Query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment