Skip to content

Commit

Permalink
Update directive usage (ghostdogpr#1187)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulpdaniels authored Dec 2, 2021
1 parent 0e8233a commit 14a989c
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
12 changes: 12 additions & 0 deletions core/src/main/scala/caliban/wrappers/ApolloCaching.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import caliban.ResponseValue.{ ListValue, ObjectValue }
import caliban.Value.{ EnumValue, IntValue, StringValue }
import caliban.execution.FieldInfo
import caliban.parsing.adt.Directive
import caliban.schema.Annotations.GQLDirective
import caliban.wrappers.Wrapper.{ EffectfulWrapper, FieldWrapper, OverallWrapper }
import caliban.{ CalibanError, GraphQLRequest, GraphQLResponse, ResponseValue }
import zio.duration.Duration
Expand All @@ -20,8 +21,19 @@ object ApolloCaching {

private val directiveName = "cacheControl"

case class GQLCacheControl(maxAge: Option[Duration] = None, scope: Option[CacheScope] = None)
extends GQLDirective(CacheControl(scope, maxAge))

object CacheControl {

def apply(scope: Option[CacheScope], maxAge: Option[Duration]): Directive =
(scope, maxAge) match {
case (Some(scope), Some(age)) => apply(age, scope)
case (None, Some(age)) => apply(age)
case (Some(scope), None) => apply(scope)
case _ => Directive(directiveName, Map.empty)
}

def apply(scope: ApolloCaching.CacheScope): Directive =
Directive(directiveName, Map("scope" -> EnumValue(scope.toString)))

Expand Down
6 changes: 3 additions & 3 deletions core/src/test/scala/caliban/wrappers/WrappersSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import caliban.execution.{ ExecutionRequest, FieldInfo }
import caliban.introspection.adt.{ __Directive, __DirectiveLocation }
import caliban.schema.Annotations.GQLDirective
import caliban.schema.{ GenericSchema, Schema }
import caliban.wrappers.ApolloCaching.CacheControl
import caliban.wrappers.ApolloCaching.{ CacheControl, GQLCacheControl }
import caliban.wrappers.ApolloPersistedQueries.apolloPersistedQueries
import caliban.wrappers.Wrapper.{ ExecutionWrapper, FieldWrapper }
import caliban.wrappers.Wrappers._
Expand Down Expand Up @@ -191,9 +191,9 @@ object WrappersSpec extends DefaultRunnableSpec {
)
},
testM("Apollo Caching") {
case class Query(@GQLDirective(CacheControl(10.seconds)) hero: Hero)
case class Query(@GQLCacheControl(maxAge = Some(10.seconds)) hero: Hero)

@GQLDirective(CacheControl(2.seconds))
@GQLCacheControl(maxAge = Some(2.seconds))
case class Hero(name: URIO[Clock, String], friends: List[Hero] = Nil)

object schema extends GenericSchema[Clock] {
Expand Down
19 changes: 10 additions & 9 deletions examples/src/main/scala/example/federation/FederationData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ import caliban.federation
import caliban.schema.Annotations.GQLDirective

import zio.query.ZQuery
import _root_.caliban.federation._

object FederationData {

object episodes {
@GQLDirective(federation.Key("season episode"))
@GQLKey("season episode")
case class Episode(
name: String,
season: Int,
episode: Int
)

@GQLDirective(federation.Key("name"))
@GQLDirective(federation.Extend)
case class Character(@GQLDirective(federation.External) name: String)
@GQLKey("name")
@GQLExtend
case class Character(@GQLExternal name: String)

case class EpisodeArgs(season: Int, episode: Int)
case class EpisodesArgs(season: Option[Int])
Expand Down Expand Up @@ -52,7 +53,7 @@ object FederationData {
import Role._
import Origin._

@GQLDirective(federation.Key("name"))
@GQLKey("name")
case class Character(
name: String,
nicknames: List[String],
Expand All @@ -61,11 +62,11 @@ object FederationData {
starredIn: List[Episode] = Nil
)

@GQLDirective(federation.Key("season episode"))
@GQLDirective(federation.Extend)
@GQLKey("season episode")
@GQLExtend
case class Episode(
@GQLDirective(federation.External) season: Int,
@GQLDirective(federation.External) episode: Int,
@GQLExternal season: Int,
@GQLExternal episode: Int,
characters: ZQuery[CharacterService, Nothing, List[Character]] = ZQuery.succeed(List.empty)
)

Expand Down
11 changes: 11 additions & 0 deletions federation/src/main/scala/caliban/federation/Federation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import caliban.CalibanError.ExecutionError
import caliban.Value.{ NullValue, StringValue }
import caliban.introspection.adt._
import caliban.parsing.adt.Directive
import caliban.schema.Annotations.GQLDirective
import caliban.schema.Step.QueryStep
import caliban.schema._
import caliban.{ CalibanError, GraphQL, GraphQLAspect, InputValue, RootResolver }
Expand All @@ -12,23 +13,33 @@ import zio.query.ZQuery
trait Federation {
import Federation._

case class GQLKey(fields: String) extends GQLDirective(Key(fields))

object Key {
def apply(fields: String): Directive =
Directive("key", Map("fields" -> StringValue(fields)))
}

case class GQLProvides(fields: String) extends GQLDirective(Provides(fields))

object Provides {
def apply(fields: String): Directive =
Directive("provides", Map("fields" -> StringValue(fields)))
}

case class GQLRequires(fields: String) extends GQLDirective(Requires(fields))

object Requires {
def apply(fields: String): Directive =
Directive("requires", Map("fields" -> StringValue(fields)))
}

case class GQLExtend() extends GQLDirective(Extend)

val Extend = Directive("extends")

case class GQLExternal() extends GQLDirective(External)

val External = Directive("external")

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ object FederationSpec extends DefaultRunnableSpec {
implicit val schema: Schema[Any, Orphan] = Schema.gen
}

@GQLDirective(Key("name"))
@GQLDirective(Extend)
case class Orphan(@GQLDirective(External) name: String, nicknames: List[String], child: OrphanChild)
@GQLKey("name")
@GQLExtend
case class Orphan(@GQLExternal name: String, nicknames: List[String], child: OrphanChild)

object Orphan {
implicit val schema: Schema[Any, Orphan] = Schema.gen
Expand Down
8 changes: 4 additions & 4 deletions vuepress/docs/docs/federation.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ First, any types that will be "resolvable" need to be annotated with a `@key` di
in the `federation` package to help with that.

```scala
@GQLDirective(Key("name"))
@GQLKey("name")
case class Character(name: String)
```

Expand All @@ -51,9 +51,9 @@ If you need to extend a type from another service, you will need to define a stu
and annotate it with the `@extends` annotation

```scala
@GQLDirective(Key("season episode"))
@GQLDirective(Extend)
case class Episode(@GQLDirective(External) season: Int, @GQLDirective(External) episode: Int, cast: List[Character])
@GQLKey("season episode")
@GQLExtend
case class Episode(@GQLExternal season: Int, @GQLExternal episode: Int, cast: List[Character])
```

Note the additional annotations we needed in this case. `Extend` is needed to tell the gateway that this type is defined within
Expand Down

0 comments on commit 14a989c

Please sign in to comment.