Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Yann Simon
@yanns
But how do you create this object first? Load from database?
I'd suggest to put any validation logic where the instance is created first.
Mark Tomko
@mtomko:matrix.org
[m]
No this gets created in a mutation, so it's coming in from the outside world.
Greg Fisher
@gnfisher
@yanns Thanks, I was starting down that path but did not include the GraphQLTypeName[A], which I think is the thing!
Yann Simon
@yanns
@mtomko:matrix.org then your mutation should return a validation of the creation of the instance. You can use exceptions, or Either for that.
Greg Fisher
@gnfisher
@yanns in your example, I will need to make a typeName for String and every other type, correct? I haveValidatedString, ValidatedCurrency etc.
(this is brushing up at the limits of my type knowledge in scala)
Greg Fisher
@gnfisher
The answer was yes, as far as Ive been able to get it to work!
Yann Simon
@yanns
You can also use Either[Error, String], Either[Error, Currency].
PsyfireX
@PsyfireX
Does Sangria have Scala 3 support, or is it likely to get Scala 3 support in the near future?
Shohei Shimomura
@sh0hei
@PsyfireX Of course we plan to support Scala3. I am currently working on it.
PsyfireX
@PsyfireX
@sh0hei That's great to hear & thank you for working on it. Do you have an idea on the approximate timeline (apologies for asking for an estimate, hah), and are there any features on Sangria I should avoid if I intend to minimize migration-pain (like macros?)
Yann Rolland
@TidyMaze

Hello :) I'm trying to use the @GraphQLDescription annotation for arguments. The doc says "@GraphQLDescription - provide a description for a type, field, enum value or an argument", it works perfectly for type, fields, enum but I have no idea if it can work on args. I tried

@GraphQLDescription("My arg description")
    val myArg: Argument[Option[MyClass]] =
      Argument("arg", OptionInputType(MyClassInputType))

    val myArg: Argument[Option[MyClass]] = {
       @GraphQLDescription("My arg description")
      Argument("arg", OptionInputType(MyClassInputType))
}

without Success, and I can't find an example in unit tests. Any help? Thanks for the nice library btw

Yann Simon
@yanns
No need for the annotation on Argument. You can directly set the description field:
Argument("arg", OptionInputType(MyClassInputType), description = "My arg description")
Annotations are only useful when you generate the GraphQL type.
Yann Rolland
@TidyMaze
Hi, thanks for your response, I know it wasn't necessary to use annotation, I wanted to make sure it was possible or not (and if not, what was the meaning of the quoted doc)
When generating GraphQL type you can also add DocumentField(), so annotation are always a bonus, not necessary
PsyfireX
@PsyfireX
I'm trying to implement a GraphQL Interface. I see there's an example of how to do it manually https://sangria-graphql.github.io/learn/#schema-definition but I'm currently using macro-based derivation for each instance of the derivation. (1) Is there a way for me to define the TagInputGql with macros? (2) If [1] is not possible, could I define the interface manually, but somehow keep the macro-based derivation for each instance?
object Example {
  sealed trait TagInputGql

  object TagInputGql{
    implicit val _ot: InterfaceType[Unit, TagInputGql] =
      ???  // <-- what to do here?
  }

  case class CreateTagInputGql(
    //fields
  ) extends TagInputGql

  object CreateTagInputGql{
    implicit val _ot: InputObjectType[CreateTagInputGql] =
      deriveInputObjectType[CreateTagInputGql](
        InputObjectTypeName("CreateTagInput")
      )
  }

  case class MarkTagInputGql(
    // fields
  ) extends TagInputGql

  object MarkTagInputGql{
    implicit val _ot: InputObjectType[MarkTagInputGql] =
      deriveInputObjectType[MarkTagInputGql](
        InputObjectTypeName("MarkTagInput")
      )
  }
}
As your sealed trait does not define any method, the GraphQL interface should not define any fields.
I have not checked if it's compiling, but that would be the direction:
sealed trait TagInputGql

  object TagInputGql{
    implicit val _ot: InterfaceType[Unit, TagInputGql] =
      InterfaceType(
        "TagInputGql",
         "Your description",
        List.empty[Field]
  }
PsyfireX
@PsyfireX
Thank you for the help, I'll give it a try :)
PsyfireX
@PsyfireX
Hmmm, it looks like I'd need to essentially create an DeriveInputObjectSetting equivalent for the Interfaces in DeriveObjectSettings
Which would require an update to DeriveInputObjectTypeMacro (I'm a bit new to macros and Sangria)
PsyfireX
@PsyfireX
It looks like manually defining the input types might be the least complicated route for the moment, based on my current familiarity levels.
PsyfireX
@PsyfireX
It appears InterfaceType is an OutputType but not an InputType. Does Sangria currently support Interface types, that are input-types? (perhaps as a side-question, are input interfaces valid graphql?)
Nick Hudkins
@nickhudkins
I do not believe input types can be interfaces at least, last I knew of the GraphQL spec
Greg Fisher
@gnfisher
I am attempting to convert our existing Sangria graphQL into a federated subgraph. The only reason is that we are migrating the Graphql server into another app, and want to be able to do it incrementally. I don't need to extend types or reference types from one to another subgraph. Do I still need to define EntityResolvers (via Sangria Federated package https://github.com/sangria-graphql/sangria-federated)? Would it still "just work" if I use the federated schema an unmarshaller?
Greg Fisher
@gnfisher
The schema worked fine before using the federated schema. Now one of my ScalarAlias for a case class is throwing a java.lang.ClassCastException: scala.math.BigDecimal cannot be cast to ads.quoting.data.Currency, but compiles fine. I've poked at this a few hours, clearly out of my depth, not sure where to go with it. I hard coded the to and from functions it uses to convert between the Currency and BigDecimal types, even. Just no clue.
This is the bit of code throwing that error at runtime, if anyone is interested
  def decimalValue[A](
      fromDecimal: BigDecimal => A
  )(toDecimal: A => BigDecimal): ScalarAlias[A, BigDecimal] =
    ScalarAlias[A, BigDecimal](
      BigDecimalType,
      (value => toDecimal(value).setScale(2, BigDecimal.RoundingMode.HALF_UP)),
      decimal => Right(fromDecimal(decimal))
    )

  implicit val CurrencyValType = decimalValue[Currency](Currency.apply)(_.value)
A number of other very similar ScalarAlias definitions are not throwing an error.
Greg Fisher
@gnfisher
Update here: it works! But my test fails with that error when it didn't before, so trying to figure it out.
Yann Simon
@yanns
The federation support is very new. If you find some issues, it'd be great to add tickets on https://github.com/sangria-graphql/sangria-federated/issues
Greg Fisher
@gnfisher
Definitely will, thanks @yanns. Right now just a spike to see that I could get it working at all. When I get back to it I will open an issue if I can't figure out why the test suite raises that error but not the live server. Probably something about how I've set that test up.
Yann Simon
@yanns
@xsoufiane is the author of the federation library for sangria. He might be able to help also.
Yann Simon
@yanns
If you want to follow the progress of the scala 3 cross-compilation, and if you want to help, we've started a project: https://github.com/sangria-graphql/sangria/projects/2
(It's not exhaustive)
Nick Hudkins
@nickhudkins
Hi! Is it possible to call fetchers from within futures in resolvers?
Nick Hudkins
@nickhudkins
yes it is lol
Nick Hudkins
@nickhudkins
        def getFriendship(userA: String, userB: String): Future[Boolean] = Future.successful(true)

        resolve = _ => {
          getFriendship(userA, userB).map{ usersAreFriends => {
              if (usersAreFriends) {
                  fetchProfile.deferOpt(userB) 
              } else {
                  // TODO: How do I allow this to be None?
                  None
              }
          }}
        }
It feels to me that maybe this whole Future[Boolean] is a terrible idea and really it should be Future[Unit] and use .map(successResponse).recover(failureResponse)
Nick Hudkins
@nickhudkins
Actually no this ends up with the same problem which is that the response is lifted to a Future[Object] since map is evaluating to a Deferred and .recover is a NoneType. Perhaps I should simply sleep
Nick Hudkins
@nickhudkins
I suppose the semantics of GraphQL would also simply permit me to throw rather than attempt an explicit None, and maybe that's the right way to go about this
abhishekvaidprophecy
@abhishekvaidprophecy
Can someone urgently help me with this: sangria-graphql/sangria#676
LĂ©onard Marques
@keuhdall

Hello, I'm pretty new to GraphQL and I'm encountering an issue with updating my entities in my mutation schema, the entity looks like this:

  Place {
    id: ID!
    name: String!
    city: String!
    pictures: [String!]!
    address: String
    description: String
  }

and the update mutation looks like this:

updatePlace ( id ID!, city String, pictures [String!], address String, description String ) Void!

Now my issue is, since address and description are optional fields, is there a way to differentiate in the mutation whether I'm not providing arguments for those fields (and therefore don't want to change their values), or if I do but I want to set them to null ?
Because as far as I know, in both cases I'll get None when calling c.arg() on my argument.
Thanks in advance !

3 replies
Srepfler Srdan
@schrepfler
@sh0hei will Scala 3 support also include support of native Union Types?
1 reply
spyros
@spyrosh_twitter
Hey all. Quick question: can I have a Query with a fieldType being an InterfaceType, instead of a concrete type that interfaces some InterfaceType? It compiles just fine, but sangria then complains with the "Can't find appropriate subtype of an interface type" error. Any help would be most appreciated!
1 reply
Mads Saustrup Fox
@MadsSaustrupFox
Hey, i have an issue. Would really like to have all the functionality of the Executor, but before it start to handle a query i would like to resolve/complete a future. I've hear it's not good practice to block the thread with Await.result. Is it posssible to have a step before execution where it fetches data from an external service. I have tried with middleware, but then it looks like i would have to await the result in beforeQuery, before i pass it on. Any suggestions or corrections to my design logic will be greatly appreciated
Yaroslav Derman
@yarosman
Hello. Can somebody show examples of using sangria for sealed hierarchies of case classes not objects ?
PsyfireX
@PsyfireX
Is it possible to combine deriveObjectType with Deferred values? For example if I have a case-class that is derived, and I want to have a single deferred field.
1 reply
PsyfireX
@PsyfireX

New Question:
https://sangria-graphql.github.io/learn/#actions

Is it possible for an Action to be a Future UpdateCtx? The basic idea is I want to return a Future, but also update the Ctx.

1 reply
Methrat0n
@Methrat0n
Hello all, how do I get the arguments of a sub-query ? Its one level deep.
I need it to paginate a database query and can't find any documentation about projections or even if its the right way to go.
1 reply
Tam-bhall
@Tam-bhall
Hi, I am new to sangria and resolvers in graphQL. Have a question, sorry if it sounds stupid:
What's the difference between sdl based schema resolvers and resolvers we create with static schema in scala object. And which one is better?
Ex: sdl based resolver is defined something like : AstSchemaBuilder.resolverBased("here we define DirectiveResolver")
And with static schema,
we define the type : implicit val ProductType: ObjectType[Unit, Product] = deriveObjectType[Unit, Product]
then : we have define fetcher(productFetcher) and finally we add it to DeferredResolver.fetchers(productFetcher)
3 replies
Tam-bhall
@Tam-bhall

Hi, I am back with another question:
I need to use schema first approach and have a schema like:

type Query {
    item(id: [String]!) : [Item]
}
type Item {
    itemId: String
    product: Product
}
type Product {
     productId: String
}

How do I write resolvers for list of inputIds above with nested entities using AstSchemaBuilder?

Tam-bhall
@Tam-bhall
Hi all, Follow up on above question, I tried creating resolvers one for Item and one for product for give list of ids. But it gives error "can't extract value of Product.productId". Is it with this approach graphQL can't know how to nest correct ids ?
I am thinking defining relation might help ? But I am stuck on how to define relations in SDL based approach ?
Sorry if duplicate.