-
-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support of JSON scalar type. #167
Comments
Using For which type would you add a schema that returns a Json scalar? Something like Circe Json? There’s an |
Hello, trait GenericScalar[T] extends Value {
def toJson: Json
} Also an implementation of input value encoder: Then in mine project I am able to define : final case class JsonScalar(json:Json) extends Value.GenericScalar[Json] {
override def toString(): String = json.noSpaces
override def toJson: Json = json
}
implicit val jsonScalarSchema = schema.scalarSchema[Json]("JSON", Some("Json extension of scalar value."), JsonScalar(_))
implicit val jsonArgBuilder: ArgBuilder[Json] = {
case v:InputValue.ObjectValue =>
Right(InputValue.circeEncoder(v))
case other =>
Left(ExecutionError(s"Can't build a Json from input: $other"))
} I am not very happy with solution, but is working. |
Why do we need implicit val jsonScalarSchema = schema.scalarSchema[Json]("JSON", Some("Json extension of scalar value."), ???) With By the way FYI, to include JSON encoders and decoders for |
Ok. I see point. Question is, do you want extend project with extensions ? I can create caliban-circe- json-extension, containing all necessary stuff: object json {
private def jsonToValue(json: Json): ResponseValue = ...
private val inputValueEncoder: Encoder[InputValue] = ...
implicit val jsonScalarSchema = new Schema[Any, Json] {
override def toType(isInput: Boolean): __Type = Types.makeScalar("JSON", Some("Scalar of json type."))
override def resolve(value: Json): Step[Any] = PureStep(jsonToValue(value))
}
implicit val jsonArgBuilder: ArgBuilder[Json] = {
case v:InputValue.ObjectValue => Right(inputValueEncoder(v))
case other => Left(ExecutionError(s"Can't build a Json from input: $other"))
}
} I believe that that lot of people can use cicrce json directly ... |
I think we can use the same technique I mentioned earlier (as shown in https://github.com/ghostdogpr/caliban/blob/master/core/src/main/scala/caliban/interop/circe/circe.scala), so we don’t need a separate module. What do you think? |
I recommended separate module (artifact) because scalar Json support is an extension (not part of specification of graphql). |
Actually I already have some custom scalars such as The optional dependency is not a problem because if someone needs those |
I made a PR #176 for this. It only requires |
Thanks a lot. Looks great. |
In Several implementations of graphql there exists possibility to extend scalar types.
Blog: blog also mentions JSON scalar extension for an implementation of graphql.
I can’t see any easy way how to do it with current implementation of caliban.
If you folks agree that it should be implemented, I can try to make some PR in near future.
The text was updated successfully, but these errors were encountered: