Comments (3)
@sloria hrm, I'll test your example in the next couple of days and try to fix the relay code that is giving those issues!
from strawberry-django-plus.
Hey @sloria ,
I have to be honest with you that, although I wrote that relay module specifically not attached to django to allow it to be used standalone, I mostly tested it with django =P
One thing you could do to workaround that is to add an _id
to Fruit
and define a resolve_id
for it. Like this:
@strawberry.type
class Fruit(relay.Node):
_id: strawberry.Private[str]
name: str
description: Optional[str]
@classmethod
def resolve_id(cls, root, *, info=None):
return root._id
By typing it as Private
it should not be exposed to graphql, and the resolve_id
method should use it.
I'll try to think how to allow id
to be passed like you did while still being able to have the custom resolver for it.
from strawberry-django-plus.
Ah I see. Unfortunately, I'm still unable to get id
to properly resolve
from typing import Iterable, Optional
import strawberry
from strawberry.types.info import Info
from .utils import relay
fruits = [
{
"_id": 1,
"name": "Banana",
"description": "Lorem ipsum",
},
{
"_id": 2,
"name": "Apple",
"description": None,
},
{
"_id": 3,
"name": "Orange",
"description": "Lorem ipsum",
},
]
@strawberry.type
class Fruit(relay.Node):
_id: strawberry.Private[str]
name: str
description: Optional[str]
@classmethod
def resolve_id(cls, root, *, info=None):
return str(root._id)
@classmethod
def resolve_node(
cls,
node_id: str,
*,
info: Optional[Info] = None,
required: bool = False,
):
for fruit in fruits:
if str(fruit["_id"]) == node_id:
return Fruit(**fruit)
if required:
raise ValueError(f"Fruit by id {node_id} not found.")
return None
@classmethod
def resolve_nodes(
cls,
*,
info: Optional[Info] = None,
node_ids: Optional[Iterable[str]] = None,
):
node_ids = node_ids and set(node_ids)
for fruit in fruits:
if node_ids is not None and str(fruit["id"]) not in node_ids:
continue
yield Fruit(**fruit)
@strawberry.type
class Query:
fruit: Fruit = relay.node()
fruits_conn: relay.Connection[Fruit] = relay.connection()
@relay.connection
def fruits_conn_with_filter(self, name_startswith: str) -> Iterable[Fruit]:
for fruit in fruits:
if fruit["name"].startswith(name_startswith):
yield Fruit(**fruit)
@strawberry.type
class Mutation:
@relay.input_mutation
def create_fruit(self, name: str, description: Optional[str]) -> Fruit:
fruit_data = {
"id": max(f["id"] for f in fruits) + 1,
"name": name,
"description": description,
}
fruits.append(fruit_data)
return Fruit(**fruit_data)
schema = strawberry.Schema(query=Query, mutation=Mutation)
If I query for a fruit's ID...
query {
fruit(id: "RnJ1aXQ6MQ==") {
id
name
}
}
I get
utils.relay.GlobalIDValueError: node_id is expected to be a string, found <bound method Node.id of <class 'utils.relay.Node'>>
from strawberry-django-plus.
Related Issues (20)
- Querying both sides of a OneToOneField at the same time is an error HOT 5
- Can't make a union of `gql.Connection | OperationMessage` HOT 1
- Headers are not passed to `self.client.post` in `TestClient.request` HOT 1
- Can't make a union of `gql.Connection | OperationMessage` (part 2) HOT 9
- clash with the novel strawberry.relay module (GlobalId)
- relay: compatibility with the new strawberry.relay module HOT 2
- more SQLs then expected HOT 6
- need for an async resolvers.update HOT 1
- NameError with lazy types in relay connection annotations
- input_mutation: TypeError: MutateContentInput fields cannot be resolved HOT 9
- filters not showing up HOT 1
- Optimise manually prefetched field HOT 1
- Got an unexpected keyword argument 'filters' HOT 2
- NodeExtension broken with Strawberry HOT 1
- class inheritance not being detected? HOT 8
- hard dependency on contenttypes framework in optimizer
- hard dependency on django.contrib.auth HOT 1
- Using input_mutation with a None return type throws an exception HOT 1
- Query resolution is really slow HOT 15
- This repo has been merged into strawberry-graphql-django and it is now deprecated HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from strawberry-django-plus.