8000 Multi-type query generation by rpiaggio · Pull Request #891 · gemini-hlsw/clue · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ abstract class FriendFields extends GraphQLSubquery.Typed[StarWars, Json] {
A subquery's `subquery` is a `GraphQLDocument` too, so it is built with `gql` for the same reason a
`document` is (see [Subquery variables](#subquery-variables)).

Hand-written subqueries may declare **multiple** types (the selection is validated against each:
`@GraphQLType("Human", "Droid")`). A subquery processed by the generator (`@GraphQL`) must declare
**exactly one** type. `@GraphQLType` is only valid on a subquery, not on a `GraphQLOperation`.
A subquery may declare **multiple** types (the selection is validated against each:
`@GraphQLType("Human", "Droid")`). For a subquery processed by the generator (`@GraphQL`), the
selected fields must also have the same types in every declared type, since a single `Data` is
generated from them. `@GraphQLType` is only valid on a subquery, not on a `GraphQLOperation`.

#### Interfaces and unions

Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
lazy val V = _root_.scalafix.sbt.BuildInfo

ThisBuild / tlBaseVersion := "0.61"
ThisBuild / tlBaseVersion := "0.62"
Global / onChangedBuildSource := ReloadOnSourceChanges

// The CI matrix covers Scala 3 only, which builds `sbt-clue` for sbt 2.x. Run the scripted tests
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/scala/clue/annotation/GraphQLType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import scala.annotation.StaticAnnotation

/**
* Declares the GraphQL root type(s) a `GraphQLSubquery`'s selection set applies to, so it can be
* validated against the schema. Multiple types are allowed for hand-written subqueries (the
* selection is validated against each); a subquery processed by the generator (`@GraphQL`) must
* declare exactly one. Not valid on a `GraphQLOperation`.
* validated against the schema. When multiple types are declared, the selection is validated
* against each. For a subquery processed by the generator (`@GraphQL`), the selected fields must
* also have the same types in every declared type, since a single `Data` is generated. Not valid on
* a `GraphQLOperation`.
*/
class GraphQLType(types: String*) extends StaticAnnotation
3 changes: 2 additions & 1 deletion core/src/test/scala/clue/GraphQLInterpolatorSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package clue

import clue.annotation.GraphQLStub
import io.circe.Json

// A subquery declaring a required variable, used to exercise the `gql` caller-check.
Expand Down Expand Up @@ -38,7 +39,7 @@ object InterpolatorTestSubList extends GraphQLSubquery.Typed[Unit, Json] {
// A pre-generation `@GraphQLStub` placeholder (no `GraphQLSubquery` supertype), as the generator
// leaves it before the scalafix rule replaces it with a real subquery. Exercises that the macro
// tolerates splicing it.
@clue.annotation.GraphQLStub
@GraphQLStub
object InterpolatorTestStub

class GraphQLInterpolatorSuite extends munit.FunSuite {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@ type Droid implements Character {
appearsIn: [Episode!]
functions: [String!]
primaryFunction: String @deprecated(reason: "Use 'functions' instead")
}
}

type Starship {
id: String!
name: String!
hero(episode: Episode): Character
}
3 changes: 2 additions & 1 deletion gen/input/src/main/scala/test/StarWarsHumanSubquery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLStub
import clue.annotation.GraphQLType

@GraphQL
Expand All @@ -23,6 +24,6 @@ abstract class StarWarsHumanSubquery extends GraphQLSubquery[StarWars] {
"""
}

@clue.annotation.GraphQLStub
@GraphQLStub
object StarWarsHumanSubquery
// format: on
3 changes: 2 additions & 1 deletion gen/input/src/main/scala/test/StarWarsNestedSubquery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLStub
import clue.annotation.GraphQLType

@GraphQL // assert: GraphQLGen
Expand All @@ -32,6 +33,6 @@ abstract class StarWarsNestedSubquery extends GraphQLSubquery[StarWars] {
"""
}

@clue.annotation.GraphQLStub
@GraphQLStub
object StarWarsNestedSubquery
// format: on
3 changes: 2 additions & 1 deletion gen/input/src/main/scala/test/StarWarsSubquery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLStub
import clue.annotation.GraphQLType

@GraphQL
Expand All @@ -23,6 +24,6 @@ abstract class StarWarsSubquery extends GraphQLSubquery[StarWars] {
"""
}

@clue.annotation.GraphQLStub
@GraphQLStub
object StarWarsSubquery
// format: on
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLStub
import clue.annotation.GraphQLType

// A generated subquery that references `$ep` without declaring it: the generator infers the variable
Expand All @@ -20,6 +21,6 @@ abstract class StarWarsSubqueryInferVariable extends GraphQLSubquery[StarWars] {
override val subquery = gql"{ hero(episode: $$ep) { name } }"
}

@clue.annotation.GraphQLStub
@GraphQLStub
object StarWarsSubqueryInferVariable
// format: on
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLStub
import clue.annotation.GraphQLType

// A subquery processed by the generator (`@GraphQL`) must declare exactly one `@GraphQLType`, since
// generation targets a single type. Multiple types here is an error (and nothing is generated).
@GraphQL // assert: GraphQLGen
// A subquery processed by the generator (`@GraphQL`) declaring multiple root types. `id` and `name`
// have the same types on `Human` and `Droid`, so a single `Data` is generated for both.
@GraphQL
@GraphQLType("Human", "Droid")
abstract class StarWarsSubqueryMultiTypeGenerated extends GraphQLSubquery[StarWars] {
override val subquery = gql"{ id name }"
}

@GraphQLStub
object StarWarsSubqueryMultiTypeGenerated
// format: on
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2016-2026 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

// format: off
/*
rules = [GraphQLGen]
Clue.schemaDirs = ["gen/input/src/main/resources/graphql/schemas"]
*/
package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLType

// `homePlanet` exists on `Human` but not on `Droid`: a generated subquery is validated against
// every declared type too, so it must be reported for `Droid` (and nothing is generated).
@GraphQL
@GraphQLType("Human", "Droid")
abstract class StarWarsSubqueryMultiTypeInvalid extends GraphQLSubquery[StarWars] {
override val subquery = gql"{ id homePlanet }" // assert: GraphQLGen
}
// format: on
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2016-2026 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

// format: off
/*
rules = [GraphQLGen]
Clue.schemaDirs = ["gen/input/src/main/resources/graphql/schemas"]
*/
package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLType

// `name` is nullable on `Human` but not on `Starship`, so the two root types would generate a
// different `Data`. Since a single one is emitted, this is an error (and nothing is generated).
@GraphQL // assert: GraphQLGen
@GraphQLType("Human", "Starship")
abstract class StarWarsSubqueryMultiTypeMismatch extends GraphQLSubquery[StarWars] {
override val subquery = gql"{ id name }"
}
// format: on
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2016-2026 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

// format: off
/*
rules = [GraphQLGen]
Clue.schemaDirs = ["gen/input/src/main/resources/graphql/schemas"]
*/
package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLType

// `$ep` is inferred as `Episode!` from `Query.hero` but as `Episode` from `Starship.hero`. A single
// `type VariableDefs` is emitted, so the variables must be declared explicitly instead.
@GraphQL
@GraphQLType("Query", "Starship")
abstract class StarWarsSubqueryMultiTypeVariableMismatch extends GraphQLSubquery[StarWars] {
override val subquery = gql"{ hero(episode: $$ep) { name } }" // assert: GraphQLGen
}
// format: on
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2016-2026 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

// format: off
/*
rules = [GraphQLGen]
Clue.schemaDirs = ["gen/input/src/main/resources/graphql/schemas"]
*/
package test

import clue.GraphQLSubquery
import clue.annotation.GraphQL
import clue.annotation.GraphQLType
import io.circe.Json

// An annotated subquery that supplies its own `Data` and declares multiple root types: there is
// nothing to generate, so it is validated against each type and copied without `@GraphQL`.
@GraphQL
@GraphQLType("Human", "Droid")
object StarWarsTypedSubqueryMultiTypeAnnotated extends GraphQLSubquery.Typed[StarWars, Json] {
override val subquery = gql"{ id name }"
}
// format: on
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2016-2026 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

// format: off

package test

import clue.GraphQLSubquery
import clue.annotation.GraphQLType

// A subquery processed by the generator (`@GraphQL`) declaring multiple root types. `id` and `name`
// have the same types on `Human` and `Droid`, so a single `Data` is generated for both.

@GraphQLType("Human", "Droid") object StarWarsSubqueryMultiTypeGenerated extends GraphQLSubquery[StarWars] {
import StarWars.Scalars._
ignoreUnusedImportScalars()
import StarWars.Enums._
ignoreUnusedImportEnums()
import StarWars.Types._
ignoreUnusedImportTypes()
override val subquery = gql"{ id name }"
case class Data(val id: String, val name: Option[String] = None)
object Data {
val id: monocle.Lens[Data, String] = monocle.macros.GenLens[Data](_.id)
val name: monocle.Lens[Data, Option[String]] = monocle.macros.GenLens[Data](_.name)
implicit val eqData: cats.Eq[Data] = cats.Eq.fromUniversalEquals
implicit val showData: cats.Show[Data] = cats.Show.fromToString
implicit val jsonDecoderData: io.circe.Decoder[Data] = io.circe.generic.semiauto.deriveDecoder[Data]
}
val dataDecoder: io.circe.Decoder[Data] = Data.jsonDecoderData
}


// format: on
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2016-2026 Association of Universities for Research in Astronomy, Inc. (AURA)
// For license information see LICENSE or https://opensource.org/licenses/BSD-3-Clause

// format: off

package test

import clue.GraphQLSubquery
import clue.annotation.GraphQLType
import io.circe.Json

// An annotated subquery that supplies its own `Data` and declares multiple root types: there is
// nothing to generate, so it is validated against each type and copied without `@GraphQL`.

@GraphQLType("Human", "Droid") object StarWarsTypedSubqueryMultiTypeAnnotated extends GraphQLSubquery.Typed[StarWars, Json] { override val subquery = gql"{ id name }" }
// format: on
Loading
Loading
0