Closed Dotrox closed 3 years ago
@Dotrox I think we can implement that š I might have some time over the weekend to do so.
I need to think a bit about the implication of doing this for the Dict
type, as it might be confusing to some people.
Does Graphene convert to GenericScalar directly?
Does Graphene convert to GenericScalar directly?
I can`t use pydantic models with Graphene, so define all fields directly by myself:
class Source(graphene.ObjectType):
id = graphene.String()
data = GenericScalar()
With Strawberry I already use additional decorator for types because I use not pure pydantic BaseModel, but wrapped model class from ODMantic ODM. This how it really looks now:
def init_model(model):
def wrap(cls):
cls_annotations = model.__pydantic_model__.__dict__.get(
"__annotations__", {})
for field, field_type in cls_annotations.items():
if field_type == odmantic.bson._datetime:
cls_annotations[field] = datetime
elif field_type == odmantic.bson.ObjectId:
cls_annotations[field] = str
cls.__annotations__ = cls_annotations
return cls
return wrap
@strawberry.type
@init_model(SourceModel)
class Source:
pass
Only this way Strawberry can perfectly works with ODMantic models: I need support for MongoDB ObjectId and also ODMantic implicitly change datetime with own version. And as sugar my decorator load all fields from my model, but main reason for it is type conversion.
So Strawberry may support Dict type in similar way (I tried implement it by myself, but my knowledges not enough).
ok! thanks for the additional information, will take a look at it soon :)
Any news?
@Dotrox haven't had time to look into this yet, is it blocking you?
I can't use Strawberry without this. Maybe you can explain how I can implement this by myself (how add custom type to Strawberry)?
What should be the result of data
? A JSON string?
What should be the result of
data
? A JSON string?
No, it must be an object. As Strawberry Object type, but without predefined fields.
Just added some documentations about an example JSONScalar here
@Dotrox let me know if the docs written by @Speedy1991 are helpful; so we can close this issue š
@Dotrox let me know if the docs written by @Speedy1991 are helpful; so we can close this issue blush
Json doesn't fit my needs, but this example show me how create a custom scalar. So I slightly modified it and get what I need:
GenericScalar = scalar(
typing.NewType("GenericScalar", typing.Any),
description="The GenericScalar scalar type represents a generic GraphQL scalar value that could be: List or Object."
)
Also I made a custom scalar for MongoDB ObjectId:
ObjectIdScalar = scalar(
bson.ObjectId,
serialize=lambda v: str(v),
parse_value=lambda v: bson.ObjectId(v),
description="MongoDB ObjectId"
)
It may be helpful for somebody if it will be in docs.
Here's what I needed to do to get JSONScalar
working with an inline input value
JSONScalar = strawberry.scalar(
NewType("JSONScalar", Any),
serialize=lambda v: v,
parse_value=lambda v: json.loads(v),
parse_literal=graphql.utilities.value_from_ast_untyped,
)
I have the following pydantic model:
And use it to define Strawberry type:
But I get the following error:
Field
data
in my model is embedded document from MongoDB and is structure may varies (I get it from external source), so I can use only Dict for it.In Graphene I use GenericScalar type for it and it perfect serve my needs. But I want switch to Strawberry and can't without this.