Skip to content

Commit

Permalink
Merge pull request #90 from graphql-python/refactor-advanced-type-res…
Browse files Browse the repository at this point in the history
…olver

Refactor advanced type resolver
  • Loading branch information
abawchen authored May 13, 2019
2 parents 20a50d2 + 6ba2db3 commit c94e024
Showing 1 changed file with 35 additions and 43 deletions.
78 changes: 35 additions & 43 deletions graphene_mongo/advanced_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,67 @@
import graphene


def _resolve_type_coordinates(self, info):
return self['coordinates']


def _resolve_fs_field(field, name, default_value=None):
v = getattr(field.instance, field.key)
return getattr(v, name, default_value)


def _resolve_content_type(self, info):
return _resolve_fs_field(self, 'content_type')


def _resolve_md5(self, info):
return _resolve_fs_field(self, 'md5')


def _resolve_chunk_size(self, info):
return _resolve_fs_field(self, 'chunk_size', 0)
class FileFieldType(graphene.ObjectType):

content_type = graphene.String()
md5 = graphene.String()
chunk_size = graphene.Int()
length = graphene.Int()
data = graphene.String()

def _resolve_length(self, info):
return _resolve_fs_field(self, 'length', 0)
@classmethod
def _resolve_fs_field(cls, field, name, default_value=None):
v = getattr(field.instance, field.key)
return getattr(v, name, default_value)

def resolve_content_type(self, info):
return FileFieldType._resolve_fs_field(self, 'content_type')

def _resolve_data(self, info):
v = getattr(self.instance, self.key)
data = v.read()
if data is not None:
return base64.b64encode(data)
return None
def resolve_md5(self, info):
return FileFieldType._resolve_fs_field(self, 'md5')

def resolve_chunk_size(self, info):
return FileFieldType._resolve_fs_field(self, 'chunk_size', 0)

class FileFieldType(graphene.ObjectType):
def resolve_length(self, info):
return FileFieldType._resolve_fs_field(self, 'length', 0)

content_type = graphene.String(resolver=_resolve_content_type)
md5 = graphene.String(resolver=_resolve_md5)
chunk_size = graphene.Int(resolver=_resolve_chunk_size)
length = graphene.Int(resolver=_resolve_length)
data = graphene.String(resolver=_resolve_data)
def resolve_data(self, info):
v = getattr(self.instance, self.key)
data = v.read()
if data is not None:
return base64.b64encode(data)
return None


class _TypeField(graphene.ObjectType):
class _CoordinatesTypeField(graphene.ObjectType):

type = graphene.String()

def resolve_type(self, info):
return self['type']

def resolve_coordinates(self, info):
return self['coordinates']

class PointFieldType(_TypeField):

coordinates = graphene.List(
graphene.Float, resolver=_resolve_type_coordinates)
class PointFieldType(_CoordinatesTypeField):

coordinates = graphene.List(graphene.Float)


class PolygonFieldType(_TypeField):
class PolygonFieldType(_CoordinatesTypeField):

coordinates = graphene.List(
graphene.List(
graphene.List(graphene.Float)),
resolver=_resolve_type_coordinates
graphene.List(graphene.Float))
)


class MultiPolygonFieldType(_TypeField):
class MultiPolygonFieldType(_CoordinatesTypeField):

coordinates = graphene.List(
graphene.List(
graphene.List(
graphene.List(graphene.Float))),
resolver=_resolve_type_coordinates)
graphene.List(graphene.Float)))
)

0 comments on commit c94e024

Please sign in to comment.