Exposing fields that are in the db/model works fine, but what about calculated fields? For example, if I want to expose a field called Author.book_count that will obviously not be queriable or editable, how can I do that?
@skorokithakis checkout issue this issue:
https://github.com/graphql-python/graphene/issues/101
Import graphene and do:
vote_count = graphene.Int(source='vote_count')
'vote_count' is my property.
Full code:
class QuestionNode(DjangoObjectType):
vote_count = graphene.Int(source='vote_count')
class Meta:
model = Question
# filter_fields = ['name', 'ingredients']
interfaces = (relay.Node, )
Ah, thanks! It would be great if this was in the docs.
How to add a new field that its type is a list?
How do I resolve a django object along with an extra, calculated field like below during a query?
class ProjectType(DjangoObjectType):
class Meta:
model = Project
extra_field = graphene.String()
class Query(ObjectType):
project = graphene.Field(ProjectType,
name=graphene.String())
def resolve_project(self, info, name):
if id is not None:
return Project.objects.get(pk=id) # how do I populate `extra_field` here?
return None
@paymog
def resolve_project(self, info, name):
if id is not None:
project = Project.objects.get(pk=id) # how do I populate `extra_field` here?
return ProjectType(**project, extra_field="something")
return None
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Most helpful comment
@skorokithakis checkout issue this issue:
https://github.com/graphql-python/graphene/issues/101
Import graphene and do:
vote_count = graphene.Int(source='vote_count')'vote_count' is my property.
Full code: