graphql-python / graphql-python/graphene-django
Extending connections using relay on same level as edges.
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 760
- PR merge metrics
- No merged PRs in 30d
Description
I have been pulling my hair out tying to solve this one. I would appreciate some help with this. Graphene Relay specs limits me to having my api calls received in the following format:
```
QUERY:
{
allSpecies {
edges {
node {
id
name
}
}
}
}
RECEIVED DATA
"data": {
"allSpecies": {
"edges": [
{
"node": {
"id": "U3BlY2llOjE=",
"name": "Human"
}
},
{
"node": {
"id": "U3BlY2llOjI=",
"name": "Alien"
}
}
]
}
}
}
```
I want to be able to create a same level property to access the api without going through edges and node first but still keep relay integrated for the purposes of pagination in case i need the added functionality.
```
NEW QUERY:
{
allSpecies {
newProperty{
id
name
}
}
}
```
In my current setup I am trying to point my newly created property "new property" to the edges node. How can I easily point to the same edges connection from the connection class and receive the same list of data? Is there a better way to do this?
```
class Custom(graphene.Connection):
class Meta:
abstract = True
new_property = graphene.List(graphene.String)
def resolve_new_property(self, info):
return self.edges
class Specie(DjangoObjectType):
eye_colors = graphene.List(graphene.String)
hair_colors = graphene.List(graphene.String)
skin_colors = graphene.List(graphene.String)
def resolve_eye_colors(self, info):
return [c.strip() for c in self.eye_colors.split(',')]
def resolve_hair_colors(self, info):
return [c.strip() for c in self.hair_colors.split(',')]
def resolve_skin_colors(self, info):
return [c.strip() for c in self.skin_colors.split(',')]
class Meta:
model = models.Species
interfaces = (Node, )
exclude_fields = ('created', 'edited', 'eye_colors', 'hair_colors',
'skin_colors')
filter_fields = {'name': {'startswith', 'contains'}}
connection_class = Custom
class Query(graphene.ObjectType):
all_species = DjangoFilterConnectionField(Specie)
schema = graphene.Schema(
query=Query,
mutation=Mutation
)
```
After running the query, I get the following error
```
{
"data": {
"allSpecies": {
"newProperty": "[, ]"
}
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.