Skip to content

Commit

Permalink
can use projector
Browse files Browse the repository at this point in the history
  • Loading branch information
yanns committed Jul 10, 2024
1 parent 3361bf0 commit 4ddcff4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ trait EntityResolver[Ctx, Node] {
val decoder: Decoder[Node, Arg]

def typename: String
def resolve(arg: Arg, ctx: Context[Ctx, _]): LeafAction[Ctx, Option[_]]
def resolve(arg: Arg)(ctx: Context[Ctx, _]): LeafAction[Ctx, Option[_]]
}

object EntityResolver {

def apply[Ctx, Node, Val, A](
__typeName: String,
resolver: (A, Context[Ctx, Val]) => LeafAction[Ctx, Option[Val]]
resolver: A => Context[Ctx, Val] => LeafAction[Ctx, Option[Val]]
)(implicit ev: Decoder[Node, A]): EntityResolver[Ctx, Node] {
type Arg = A
} = new EntityResolver[Ctx, Node] {
Expand All @@ -26,7 +26,7 @@ object EntityResolver {
val decoder: Decoder[Node, A] = ev

def typename: String = __typeName
def resolve(arg: Arg, ctx: Context[Ctx, _]): LeafAction[Ctx, Option[Val]] =
resolver(arg, ctx.asInstanceOf[Context[Ctx, Val]])
def resolve(arg: Arg)(ctx: Context[Ctx, _]): LeafAction[Ctx, Option[Val]] =
resolver(arg)(ctx.asInstanceOf[Context[Ctx, Val]])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ object InventoryGraphQLSchema {
def inventoryResolver: EntityResolver[AppContext, Json] { type Arg = InventoryArgs } =
EntityResolver[AppContext, Json, Inventory, InventoryArgs](
__typeName = InventoryType.name,
(arg, ctx) => ctx.ctx.productService.inventory(arg.id)
arg => _.ctx.productService.inventory(arg.id)
)(decoder)
}
19 changes: 9 additions & 10 deletions example/product/src/main/scala/graphql/ProductGraphQLSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -154,26 +154,25 @@ object ProductGraphQLSchema {
def productResearchResolver: EntityResolver[AppContext, Json] { type Arg = ProductResearchArgs } =
EntityResolver[AppContext, Json, ProductResearch, ProductResearchArgs](
ProductResearchType.name,
(arg, ctx) => ctx.ctx.productResearchService.productResearch(arg.study.caseNumber)
arg => _.ctx.productResearchService.productResearch(arg.study.caseNumber)
)

def productResolver: EntityResolver[AppContext, Json] { type Arg = ProductArgs } =
EntityResolver[AppContext, Json, Product, ProductArgs](
ProductType.name,
(arg, ctx) =>
arg match {
case ProductArgs.IdOnly(id) => ctx.ctx.productService.product(id)
case ProductArgs.SkuAndPackage(sku, pack) =>
ctx.ctx.productService.bySkuAndPackage(sku, pack)
case ProductArgs.SkuAndVariationId(sku, variation) =>
ctx.ctx.productService.bySkuAndProductVariantionId(sku, variation)
}
{
case ProductArgs.IdOnly(id) => _.ctx.productService.product(id)
case ProductArgs.SkuAndPackage(sku, pack) =>
_.ctx.productService.bySkuAndPackage(sku, pack)
case ProductArgs.SkuAndVariationId(sku, variation) =>
_.ctx.productService.bySkuAndProductVariantionId(sku, variation)
}
)

def deprecatedProductResolver
: EntityResolver[AppContext, Json] { type Arg = DeprecatedProductArgs } =
EntityResolver[AppContext, Json, DeprecatedProduct, DeprecatedProductArgs](
DeprecatedProductType.name,
(arg, ctx) => ctx.ctx.productService.deprecatedProduct(arg.sku, arg.`package`)
arg => _.ctx.productService.deprecatedProduct(arg.sku, arg.`package`)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ object UserGraphQLSchema {
def userResolver: EntityResolver[AppContext, Json] { type Arg = UserArgs } =
EntityResolver[AppContext, Json, User, UserArgs](
__typeName = UserType.name,
(arg, ctx) => ctx.ctx.userService.user(arg.email)
arg => _.ctx.userService.user(arg.email)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ object StateGraphQLSchema {
implicit val decoder: Decoder[Json, StateArg] = deriveDecoder[StateArg].decodeJson(_)
val stateResolver = EntityResolver[StateContext, Json, State, StateArg](
__typeName = StateType.name,
(arg, _) => states.deferOpt(arg.id))
arg => _ => states.deferOpt(arg.id))
}

0 comments on commit 4ddcff4

Please sign in to comment.