Skip to content

Commit

Permalink
Derives fails for @lazy directive when Env != Any (#2125)
Browse files Browse the repository at this point in the history
  • Loading branch information
develeon authored Feb 21, 2024
1 parent c2b2a8e commit 6e3c060
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
directive @lazy on FIELD_DEFINITION
directive @newtype(name : String) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION

scalar FID
Expand Down Expand Up @@ -25,8 +26,14 @@ type Foo {
allIDsOpt: [ID]! @newtype(name: "InnerOptId")
mapbeAllIDs: [ID!] @newtype(name: "MaybeInnerId")
mapbeAllIDsOpt: [ID] @newtype(name: "MaybeInnerIdOpt")
getLazyFoo : FooLazy @lazy
}

type FooLazy {
id : ID!
}


input FooInput {
id : ID! @newtype(name: "CustomId")
allIDs : [ID!]! @newtype(name: "IInnerId")
Expand Down
1 change: 1 addition & 0 deletions codegen-sbt/src/sbt-test/codegen/test-compile-newtype/test
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ $ exists target/scala-2.12/src_managed/main/caliban-codegen-sbt/graphql/Generate
$ exec sh verify.sh Foo target/scala-2.12/src_managed/main/caliban-codegen-sbt/graphql/GeneratedAPI.scala
$ exec sh verify.sh FooInput target/scala-2.12/src_managed/main/caliban-codegen-sbt/graphql/GeneratedAPI.scala
$ exec sh verify.sh CustomId target/scala-2.12/src_managed/main/caliban-codegen-sbt/graphql/GeneratedAPI.scala
$ exec sh verify.sh FooLazy target/scala-2.12/src_managed/main/caliban-codegen-sbt/graphql/GeneratedAPI.scala
14 changes: 7 additions & 7 deletions tools/src/main/scala/caliban/tools/SchemaWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ object SchemaWriter {
envForDerives: Option[String] = None
): String = {

val (derivesSchema, derivesSchemaAndArgBuilder, derivesEnvSchema, derivesOnQuery) =
val (derivesSchema, derivesSchemaAndArgBuilder, envSchemaDerivation, derivesEnvSchema) =
(addDerives, envForDerives) match {
case (false, _) => ("", "", "", "")
case (true, Some(env)) if !env.equalsIgnoreCase("Any") =>
(
" derives caliban.schema.Schema.SemiAuto",
" derives caliban.schema.Schema.SemiAuto, caliban.schema.ArgBuilder",
s"object EnvSchema extends caliban.schema.SchemaDerivation[${safeName(env)}]\n\n",
" derives EnvSchema.SemiAuto"
" derives Operations.EnvSchema.SemiAuto"
)
case (true, _) =>
(
Expand Down Expand Up @@ -112,7 +112,7 @@ object SchemaWriter {
s"""
|${writeTypeAnnotations(op)}final case class ${op.name}${generic(op, isRootDefinition = true)}(
|${op.fields.map(c => writeRootField(c, op)).mkString(",\n")}
|)$derivesOnQuery""".stripMargin
|)$derivesEnvSchema""".stripMargin

def writeSubscriptionField(field: FieldDefinition, od: ObjectTypeDefinition): String =
"%s:%s ZStream[Any, Nothing, %s]".format(
Expand All @@ -125,7 +125,7 @@ object SchemaWriter {
s"""
|${writeTypeAnnotations(op)}final case class ${op.name}(
|${op.fields.map(c => writeSubscriptionField(c, op)).mkString(",\n")}
|)$derivesSchema""".stripMargin
|)$derivesEnvSchema""".stripMargin

def writeObject(typedef: ObjectTypeDefinition, extend: List[String]): String = {
val extendRendered = extend match {
Expand All @@ -134,7 +134,7 @@ object SchemaWriter {
}
s"""${writeTypeAnnotations(typedef)}final case class ${typedef.name}${generic(typedef)}(${typedef.fields
.map(field => writeField(field, inheritedFromInterface(typedef, field).getOrElse(typedef), isMethod = false))
.mkString(", ")})$extendRendered$derivesSchema"""
.mkString(", ")})$extendRendered$derivesEnvSchema"""
}

def writeInputObject(typedef: InputObjectTypeDefinition): String = {
Expand Down Expand Up @@ -167,7 +167,7 @@ object SchemaWriter {

def writeInterface(interface: InterfaceTypeDefinition): String =
s"""@GQLInterface
${writeTypeAnnotations(interface)}sealed trait ${interface.name} extends scala.Product with scala.Serializable $derivesSchema {
${writeTypeAnnotations(interface)}sealed trait ${interface.name} extends scala.Product with scala.Serializable $derivesEnvSchema {
${interface.fields.map(field => writeField(field, interface, isMethod = true)).mkString("\n")}
}
"""
Expand Down Expand Up @@ -454,7 +454,7 @@ object SchemaWriter {

${if (hasOperations)
"object Operations {\n" +
derivesEnvSchema +
envSchemaDerivation +
queries + "\n\n" +
mutations + "\n\n" +
subscriptions + "\n" +
Expand Down

0 comments on commit 6e3c060

Please sign in to comment.