Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Miklos Szots
@smiklos
very important to mention, (perhaps a bit late) that I'm strictly talking about deriveInputObject and not manually creating the fields
Nick Hudkins
@nickhudkins
If you can give me a full example, I think I may be able to help
This IS important :)
what's your deriveInputObject call look like?
Miklos Szots
@smiklos
import io.circe.{Decoder, Encoder}
import sangria.macros.derive.deriveInputObjectType
import sangria.schema.{ScalarAlias, StringType}

case class ResultData(resultId: ResultId)

object ResultData {

  implicit val derived = deriveInputObjectType[ResultData]()
}

object ResultId {

  //Already derived it as a string
  //This is a factory for creating ResultId from String
  implicit val encoder: Encoder[ResultId] = Encoder.encodeString.contramap(_.id)
  implicit val decoder: Decoder[ResultId] = Decoder.decodeString.map(ResultId(_))

  //Now I repeat myself
  //This is also a factory for creating ResultId from String
  implicit val graphQlType: ScalarAlias[ResultId, String] = ScalarAlias[ResultId, String](
    StringType,
    _.id,
    id => Right(ResultId(id))
  )
}

case class ResultId(id: String) extends AnyVal
Miklos Szots
@smiklos
I've literally added a println to both decoder and alias and they both get invoked :D , the graphql is invoked even twice
to get one value mapping, 3 invocation :/
removed circe decoder
 "Argument 'test' has invalid value: Attempt to decode value on failed cursor: DownField(id),DownField(resultId) (line 4, column 18):\n   testing(test: {\n                 ^",
so , I need a scalar alias so sangira generates a valid input/output object (for wrapper classes) but I also need to actually map it and tell circe how to do it.
Funny enough, the graphql type gets invoked 2 times here as well, and only circe fails
Miklos Szots
@smiklos
looking at the marshaller, it's clear that creating a case class uses the json node parsed straight out of the request and that whatever sangria does with that value is out of the hands of circe. It all makes sense, just using the lib is confusing
Oliver Wetterau
@owetterau

Does anybody have an idea, why enums and Circe are failing with an "argument xyzzy has invalid value: CNil" error?

As far as I have followed it up, it seems that a case class containing an enum (whereas the enum has no quotes in GraphQL-JSON) is being read properly (including the enum), then being converted to a new JSON where the enum is treated as a string (with quotes) and it seems that it then shall be converted to a case class where it fails because the enum value is now being read with quotes and cannot be converted to an Enum. Unfortunately I don't know how to change that behavior.

I am using EnumType(...)and deriveInputObjectType[...] to create the schema.

Does it have to do with the issues you have been describing with Circe in the last posts?

Dan Di Spaltro
@dispalt
@nickhudkins well the actual issue is just how it all ties together. Like circe's decoders and encoders are not introspectable. so if you make a custom decoder/encoder in circe because you've renamed a field or something, the derivation for the input object wouldn't reflect that. It's not a bug per se, and nothing really sangria can do about it, it just limits the usefulness
Nick Hudkins
@nickhudkins
Yeah :( I was looking at it now, creating a bit of a test case
@dispalt the question I have is... can deriveInputObjectType, ensure that the proper (encoding / decoding) implicits are in scope along with the graphql type
Nick Hudkins
@nickhudkins
As time has gone on, I know many folks who maybe used to use Sangria, no longer do. I was curious if we could get a quick list of "Companies using Sangria at scale" If anyone here is using it in production at work (and you can share) I'd love to collect a list!
Yann Simon
@yanns
Hi, I gave a talk about GraphQL that you might find interesting: https://www.youtube.com/watch?v=Y4aP-ryulgQ&t=489s
Srepfler Srdan
@schrepfler
as any project, it would be good if there's a leadership with a clear roadmap ahead. What differentiates Sangria from the other implementations, what will keep it relevant in time etc.
Yann Simon
@yanns
@schrepfler I agree with you. As I wrote in https://github.com/sangria-graphql/sangria/issues/446#issuecomment-623075358, I'm not the one who can do it. I don't have enough time for this.
@nickhudkins invested a lot of time recently. Thanks to him! If you want to join, you are welcome.
Tim Rourke
@timrourke
is there a good way to deal with errors in sangria that doesn't involve throwing and catching exceptions? my server uses Either[SomeErrorType, SomeResourceType] to constrain the error channel and keep it typed, and there are some errors i would want to handily lazily instead of eagerly
(also totally unrelated to that, is there a sensible way to provide a query i am executing a database transaction? this would be especially important to me for mutation queries, because i need to treat writes as atomic and roll back a transaction if any one DB operation fails during a write. is this kind of use case even common in graphql-land?)
er, is the thing to do to provide a custom context of some kind?
Tim Rourke
@timrourke
ok, figured both of the above q's out
Tim Rourke
@timrourke
for anyone playing along at home, the solution is to define a custom context object containing whatever DAOs/repositories/thingies that get stuff from the DB. i feel a little icky sticking a live database transaction object into a case class as a field, but it works
and for serializing exceptions, you can implement a custom exception handler to extract relevant data for an exception thrown for a given field. would be nice to avoid throw but not a big deal at this external layer of the application
Nick Hudkins
@nickhudkins
Hey @timrourke you should checkout Alternative Execution Scheme https://sangria-graphql.github.io/learn/#alternative-execution-scheme
DeliveryScheme.Either will allow you to handle QUERY parsing as an Either as well. Hoping this helps point you into the "sangria" way to handle this
Nick Hudkins
@nickhudkins
as for your db question, the pattern I have used is to create an execution and mutation context that i put in... context. the context is created per HTTP request, providing a SHARED read connection for any DB executions and a unique transaction for any mutation. the connection itself is created per HTTP request as well, pulled from a pool. This lets me handle transaction rollback within a wcWrite function provided by a WithWriteConnection trait that wraps all of my DB operations and provides the transaction to be used, with any failures resulting in automatic rollback, or committing on success
Yann Simon
@yanns
Hi all. We will remove support for scala 2.11 and jvm < 1.8: https://github.com/sangria-graphql/sangria/issues/523#issuecomment-739152466
It's still time to react if you are impacted by this plan.
Binh Nguyen
@ngbinh
ok with it. There is not much excuse to not moving away from 2.11
Yann Simon
@yanns
Hi, if you want to help, you can open PR to use github actions instead of travis CI. We need that on all repositories.
I've migrated Sangria (sangria-graphql/sangria#547) and macro-visit (sangria-graphql/macro-visit#28 and https://github.com/sangria-graphql/macro-visit/pull/29)
Sagar Jadhav
@SagarJa07481451_twitter
Is there a way to not use case classes to model object types in sangria. Using Graphql-Java , we can externalize the schema and pass it at runtime. How can we achieve this in graphQL-scala?
Yann Simon
@yanns
It's possible to derive the GraphQL types from case classes, but it is also possible to write the GraphQL types per hand: https://sangria-graphql.github.io/learn/#schema-definition
Sagar Jadhav
@SagarJa07481451_twitter
@yanns I get "Schema was materialized and cannot be used for any queries except introspection queries." error when I parse it and try to execute.
Yann Simon
@yanns
Strange we are using this approach on production without any issues.
Sagar Jadhav
@SagarJa07481451_twitter
@yanns Can you please list down high level steps that you follow. Maybe I am missing some step.
Yann Simon
@yanns
@SagarJa07481451_twitter we are using the Executor.execute, like in https://github.com/sangria-graphql/sangria-akka-http-example/blob/master/src/main/scala/Server.scala#L33
Sagar Jadhav
@SagarJa07481451_twitter
@yanns This does not work for me. Are you also reading the schema from an external schema.graphql file ? In the above link, schema is not read from an external file. It is created through the code.
Dave Handy
@dave-handy
hi @yanns ! I see you are also working on migrating to github actions... have you tried publishing a new signed version yet? I'm having an issue with the action not seeing PGP_SECRET
Yann Simon
@yanns
@SagarJa07481451_twitter no I don't use an external schema file. Sorry I might have misunderstood you. I thought you want to avoid using the macro derivation to generate the GraphQL schema.
@dave-handy the publishing with github actions is disabled for now: sangria-graphql/macro-visit#29
This would requite much more work.
5 replies
Yann Simon
@yanns
https://github.com/sangria-graphql/sangria-slowlog is now using github actions & auto-formatting
Yann Simon
@yanns
same now for https://github.com/sangria-graphql/sangria-msgpack. If you want to help, you can have a look at the changed that were necessary, and apply them to other repositories.
Yann Simon
@yanns
Binh Nguyen
@ngbinh
:clap:
Philipp Zerelles
@pz_mxu_gitlab
Hello, Can someone maybe help me? I create a schema from AST and in the AST I use a custom scalar "DateTime". Now I also created a ScalarType for that type and use ScalarResolver to assign it to the scalar defined in the AST. But for outputs with the custom type, I get an exception that String cannot be cast to LocalDateTime and my custom "coerceOutput" is not called at all. What am I missing?
Yann Simon
@yanns
An example of a ScalarType we are using:
  implicit val DateTimeType = ScalarType[DateTime](
    "DateTime",
    description = Some(
      "DateTime is a scalar value that represents an [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) formatted date and time."),
    coerceOutput = (date, _) => ISODateTimeFormat.dateTime().print(date),
    coerceUserInput = {
      case s: String =>
        parseDateTime(s) match {
          case scala.util.Success(date) => Right(date)
          case scala.util.Failure(_) => Left(DateCoercionViolation)
        }
      case _ => Left(DateCoercionViolation)
    },
    coerceInput = {
      case ast.StringValue(s, _, _, _, _) =>
        parseDateTime(s) match {
          case scala.util.Success(date) => Right(date)
          case scala.util.Failure(_) => Left(DateCoercionViolation)
        }
      case _ => Left(DateCoercionViolation)
    }
  )
abhishekvaidprophecy
@abhishekvaidprophecy

I'm new to Sangria. I need to create an Argument which is of custom object type.

val authorizationContextArg: Argument[DefaultInput] = Argument(
    "authorizationContext",
    InputObjectType(
      "AuthorizationContext",
      "Define the Auhorization Context of the Search",
      fields = List(
        InputField("authEntityKind", StringType, description = "Auth Entity Kind"),
        InputField("authEntityId",   StringType, description = "Auth Entity Id")
      )
    )
  )

Now, in my resolver, I'm accessing this argument like this ?

val arg             = ctx.arg(authorizationContextArg)

My query is as to how do I now access both the authEntityKind and authEntityId from arg ?

macchiatow
@macchiatow
does anyone know why resolve function of DynamicDirectiveResolver never called where directive define on Interface or Type (this is not a case with field), so far I use middleware as workaround.
TrasheRacer
@TrasheRacer

Hi everyone, first time poster but long time Sangria user,

Our team has identified what appears to be a bug in sangria.Schema.

Looks like there is some side-effect causing Schema() to throw an NPE when it calls collectTypes(). We haven't yet figured out where it's coming from - possibly something to do with lazy val usage. Any advice on fixing this would be appreciated!

I can provide a unit test which reproduces the bug on request.
Cheers! Jake