sangria-graphql / sangria

Scala GraphQL implementation
https://sangria-graphql.github.io
Apache License 2.0
1.97k stars 221 forks source link

Projector <=> deriveContextObjectType #176

Open tekacs opened 7 years ago

tekacs commented 7 years ago

I've tried taking a quick look about to see how possible this is, but I was wondering - is it possible to refactor Projector in such a way that it can be passed to methods generated by deriveContextObjectType in the same way that Context is?

As it stands, where Projector overrides the resolve function, I can't immediately see a way to make this work short of refactoring its mechanism.

OlegIlyenko commented 7 years ago

Yeah, unfortunately there is no out-of-the-box support. One thing that I can recommend you to check is a ReplaceField macro setting. Here is an example:

val tpe = deriveObjectType[Ctx, TestSubject](
  ReplaceField("foo", Field("foo", ListType(StringType), resolve = Projector((ctx, proj) ⇒ {
    // resolve logic goes here
  }))))
tekacs commented 7 years ago

Thanks for this! Would you be opposed to a pull request refactoring the way Projector` works, if it were possible to make both the current API and this approach work?

(more generally it would be nice to make more Sangria features work with macro derivation, since derived interfaces leave the underlying functions far easier to reuse)

OlegIlyenko commented 7 years ago

It would be cool if you could check it out! At the moment I'm not quite sure how to signal that particular field needs a projection (it's probably only relevant for methods, since the projection will be passed as an argument). Also projection should accept an argument (the depth).

If you have ideas, I would be happy to discuss them. Especially how the end-user API would look like.