Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small cleanups #178

Merged
merged 1 commit into from
Sep 19, 2022
Merged
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
6 changes: 3 additions & 3 deletions core/src/main/scala/sangria/federation/v1/Federation.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sangria.federation.v1

import sangria.ast.Document
import sangria.ast
import sangria.marshalling.InputUnmarshaller
import sangria.renderer.SchemaFilter
import sangria.schema._
Expand Down Expand Up @@ -29,15 +29,15 @@ object Federation {
(entities match {
case Nil =>
schema.extend(
Document(definitions = Vector(queryType(_service))),
ast.Document(definitions = Vector(queryType(_service))),
AstSchemaBuilder.resolverBased[Ctx](
FieldResolver.map("Query" -> Map("_service" -> (_ => _Service(sdl)))),
AdditionalTypes(_Any.__type[Node], _Service.Type, _FieldSet.Type)
)
)
case entities =>
schema.extend(
Document(definitions = Vector(queryType(_service, _entities))),
ast.Document(definitions = Vector(queryType(_service, _entities))),
AstSchemaBuilder.resolverBased[Ctx](
FieldResolver.map(
"Query" -> Map(
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/sangria/federation/v1/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import sangria.ast._

object Query {

val _service = FieldDefinition(
val _service: FieldDefinition = FieldDefinition(
name = "_service",
fieldType = NotNullType(NamedType("_Service")),
arguments = Vector.empty)

val _entities =
val _entities: FieldDefinition =
FieldDefinition(
name = "_entities",
fieldType = NotNullType(ListType(NamedType("_Entity"))),
Expand All @@ -20,7 +20,7 @@ object Query {
defaultValue = None))
)

def queryType(fields: FieldDefinition*) =
def queryType(fields: FieldDefinition*): ObjectTypeExtensionDefinition =
ObjectTypeExtensionDefinition(
name = "Query",
interfaces = Vector.empty,
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/sangria/federation/v1/_Any.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ private[federation] object _Any {

def __type[Node] = ScalarType[_Any[Node]](
name = "_Any",
coerceOutput = { case _ =>
coerceOutput = { (_, _) =>
"output"
},
coerceUserInput = {
case n: NodeObject[Node] =>
case n: NodeObject[Node] @unchecked =>
n.__typename match {
case Some(__typename) => Right(_Any(__typename, n))
case None => Left(TypeNameNotFound)
Expand Down
37 changes: 15 additions & 22 deletions core/src/main/scala/sangria/federation/v2/Federation.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package sangria.federation.v2

import sangria.ast.{
Argument => AstArgument,
Directive => AstDirective,
Document,
ListValue,
SchemaExtensionDefinition,
StringValue
}
import sangria.ast
import sangria.marshalling.InputUnmarshaller
import sangria.renderer.SchemaFilter
import sangria.schema._
Expand All @@ -33,23 +26,23 @@ object Federation {

val sdl = Some(schema.renderPretty(SchemaFilter.withoutGraphQLBuiltIn))

val schemaExtensionDefinition = SchemaExtensionDefinition(
val schemaExtensionDefinition = ast.SchemaExtensionDefinition(
operationTypes = Vector.empty,
directives = Vector(
AstDirective(
ast.Directive(
name = "link",
arguments = Vector(
AstArgument("url", StringValue("https://specs.apollo.dev/federation/v2.0")),
AstArgument(
ast.Argument("url", ast.StringValue("https://specs.apollo.dev/federation/v2.0")),
ast.Argument(
"import",
ListValue(Vector(
StringValue("@key"),
StringValue("@shareable"),
StringValue("@inaccessible"),
StringValue("@override"),
StringValue("@external"),
StringValue("@provides"),
StringValue("@requires")
ast.ListValue(Vector(
ast.StringValue("@key"),
ast.StringValue("@shareable"),
ast.StringValue("@inaccessible"),
ast.StringValue("@override"),
ast.StringValue("@external"),
ast.StringValue("@provides"),
ast.StringValue("@requires")
))
)
)
Expand All @@ -59,7 +52,7 @@ object Federation {
(entities match {
case Nil =>
schema.extend(
Document(Vector(queryType(_service), schemaExtensionDefinition)),
ast.Document(Vector(queryType(_service), schemaExtensionDefinition)),
AstSchemaBuilder.resolverBased[Ctx](
FieldResolver.map("Query" -> Map("_service" -> (_ => _Service(sdl)))),
AdditionalTypes(
Expand All @@ -72,7 +65,7 @@ object Federation {
)
case entities =>
schema.extend(
Document(Vector(queryType(_service, _entities), schemaExtensionDefinition)),
ast.Document(Vector(queryType(_service, _entities), schemaExtensionDefinition)),
AstSchemaBuilder.resolverBased[Ctx](
FieldResolver.map(
"Query" -> Map(
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/sangria/federation/v2/Query.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import sangria.ast._

object Query {

val _service = FieldDefinition(
val _service: FieldDefinition = FieldDefinition(
name = "_service",
fieldType = NotNullType(NamedType("_Service")),
arguments = Vector.empty)

val _entities =
val _entities: FieldDefinition =
FieldDefinition(
name = "_entities",
fieldType = NotNullType(ListType(NamedType("_Entity"))),
Expand All @@ -20,7 +20,7 @@ object Query {
defaultValue = None))
)

def queryType(fields: FieldDefinition*) =
def queryType(fields: FieldDefinition*): ObjectTypeExtensionDefinition =
ObjectTypeExtensionDefinition(
name = "Query",
interfaces = Vector.empty,
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/sangria/federation/v2/_Any.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ private[federation] object _Any {

def __type[Node] = ScalarType[_Any[Node]](
name = "_Any",
coerceOutput = { case _ =>
coerceOutput = { (_, _) =>
"output"
},
coerceUserInput = {
case n: NodeObject[Node] =>
case n: NodeObject[Node] @unchecked =>
n.__typename match {
case Some(__typename) => Right(_Any(__typename, n))
case None => Left(TypeNameNotFound)
Expand Down
6 changes: 4 additions & 2 deletions core/src/test/scala/sangria/federation/v1/_AnySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import io.circe.parser._
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.EitherValues
import sangria.marshalling.InputUnmarshaller
import sangria.validation.Violation

class _AnySpec extends AnyWordSpec with Matchers with EitherValues {

implicit val um = Federation.upgrade(sangria.marshalling.circe.CirceInputUnmarshaller)
implicit private val um: InputUnmarshaller[Json] =
Federation.upgrade(sangria.marshalling.circe.CirceInputUnmarshaller)

"_Any scalar coercion accepts an object with __typename field" in {
// https://www.apollographql.com/docs/federation/v1/federation-spec#scalar-_any
parseUserInput(
parse("""{ "__typename": "foo", "foo": "bar" }""")
.getOrElse(Json.Null)).value shouldBe a[_Any[Json]]
.getOrElse(Json.Null)).value shouldBe a[_Any[_]]
}

def parseUserInput(value: Json): Either[Violation, _Any[Json]] =
Expand Down
6 changes: 4 additions & 2 deletions core/src/test/scala/sangria/federation/v2/_AnySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import io.circe.parser._
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.EitherValues
import sangria.marshalling.InputUnmarshaller
import sangria.validation.Violation

class _AnySpec extends AnyWordSpec with Matchers with EitherValues {

implicit val um = Federation.upgrade(sangria.marshalling.circe.CirceInputUnmarshaller)
implicit private val um: InputUnmarshaller[Json] =
Federation.upgrade(sangria.marshalling.circe.CirceInputUnmarshaller)

"_Any scalar coercion accepts an object with __typename field" in {
// https://www.apollographql.com/docs/federation/federation-spec/#scalar-_any
parseUserInput(
parse("""{ "__typename": "foo", "foo": "bar" }""")
.getOrElse(Json.Null)).value shouldBe a[_Any[Json]]
.getOrElse(Json.Null)).value shouldBe a[_Any[_]]
}

def parseUserInput(value: Json): Either[Violation, _Any[Json]] =
Expand Down
4 changes: 2 additions & 2 deletions example/common/src/main/scala/common/GraphQL.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cats.effect._
import cats.implicits._
import io.circe._
import io.circe.optics.JsonPath._
import sangria.ast.Document
import sangria.ast
import sangria.execution._
import sangria.marshalling.InputUnmarshaller
import sangria.marshalling.circe.CirceResultMarshaller
Expand Down Expand Up @@ -75,7 +75,7 @@ object GraphQL {
def exec(
schema: Schema[A, Any],
userContext: F[A],
query: Document,
query: ast.Document,
operationName: Option[String],
variables: Json): F[Either[Json, Json]] =
for {
Expand Down
2 changes: 1 addition & 1 deletion example/common/src/main/scala/common/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object Server {
PermanentRedirect(Location(uri"/playground"))
}

BlazeServerBuilder[F](global)
BlazeServerBuilder[F]
.bindHttp(port, "localhost")
.withHttpApp(routes.orNotFound)
.resource
Expand Down