Skip to content

Commit

Permalink
Merge pull request #184 from sangria-graphql/fed2-add-link-sdl
Browse files Browse the repository at this point in the history
feat(fed2): extend schema with @link
  • Loading branch information
Soufiane Maguerra authored Oct 14, 2022
2 parents 5b7b748 + ad0c4cd commit bc6d8da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
25 changes: 12 additions & 13 deletions core/src/main/scala/sangria/federation/v2/Federation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ object Federation {
case obj: ObjectType[Ctx, _] @unchecked if obj.astDirectives.exists(_.name == "key") => obj
}.toList

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

val schemaExtensionDefinition = ast.SchemaExtensionDefinition(
operationTypes = Vector.empty,
directives = Vector(
val extendedSchema = schema
.copy(astDirectives = Vector(
ast.Directive(
name = "link",
arguments = Vector(
Expand All @@ -42,17 +39,19 @@ object Federation {
ast.StringValue("@override"),
ast.StringValue("@external"),
ast.StringValue("@provides"),
ast.StringValue("@requires")
ast.StringValue("@requires"),
ast.StringValue("@tag")
))
)
)
))
)
)))

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

(entities match {
case Nil =>
schema.extend(
ast.Document(Vector(queryType(_service), schemaExtensionDefinition)),
extendedSchema.extend(
ast.Document(Vector(queryType(_service))),
AstSchemaBuilder.resolverBased[Ctx](
FieldResolver.map("Query" -> Map("_service" -> (_ => _Service(sdl)))),
AdditionalTypes(
Expand All @@ -64,8 +63,8 @@ object Federation {
)
)
case entities =>
schema.extend(
ast.Document(Vector(queryType(_service, _entities), schemaExtensionDefinition)),
extendedSchema.extend(
ast.Document(Vector(queryType(_service, _entities))),
AstSchemaBuilder.resolverBased[Ctx](
FieldResolver.map(
"Query" -> Map(
Expand All @@ -92,7 +91,7 @@ object Federation {
Link__Purpose.Type)
)
)
}).copy(directives = Directives.definitions ::: schema.directives)
}).copy(directives = Directives.definitions ::: extendedSchema.directives)
}

def upgrade[Node](default: InputUnmarshaller[Node]): InputUnmarshaller[Node] =
Expand Down
14 changes: 7 additions & 7 deletions core/src/test/scala/sangria/federation/v2/FederationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FederationSpec extends AsyncFreeSpec {

val expectedSubGraphSchema = Schema
.buildFromAst(graphql"""
schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@shareable", "@inaccessible", "@override", "@external", "@provides", "@requires"]) {
schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@shareable", "@inaccessible", "@override", "@external", "@provides", "@requires", "@tag"]) {
query: Query
}

Expand Down Expand Up @@ -109,7 +109,7 @@ class FederationSpec extends AsyncFreeSpec {

val expectedSubGraphSchema = Schema
.buildFromAst(graphql"""
schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@shareable", "@inaccessible", "@override", "@external", "@provides", "@requires"]) {
schema @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@shareable", "@inaccessible", "@override", "@external", "@provides", "@requires", "@tag"]) {
query: Query
}

Expand Down Expand Up @@ -185,7 +185,7 @@ class FederationSpec extends AsyncFreeSpec {
"should not include federation types" - {
"in case no entity is defined" in {
val schema = Federation.extend(
Schema.buildFromAst(graphql"""
Schema.buildFromAst(graphql"""
schema {
query: Query
}
Expand All @@ -202,7 +202,7 @@ class FederationSpec extends AsyncFreeSpec {
.map(QueryRenderer.renderPretty(_) should be("""{
| data: {
| _service: {
| sdl: "type Query {\n field: Int\n}"
| sdl: "schema @link(url: \"https://specs.apollo.dev/federation/v2.0\", import: [\"@key\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\", \"@tag\"]) {\n query: Query\n}\n\ntype Query {\n field: Int\n}"
| }
| }
|}""".stripMargin))
Expand All @@ -214,7 +214,7 @@ class FederationSpec extends AsyncFreeSpec {
schema {
query: Query
}

type Query {
states: [State]
}
Expand All @@ -232,7 +232,7 @@ class FederationSpec extends AsyncFreeSpec {
.map(QueryRenderer.renderPretty(_) should be("""{
| data: {
| _service: {
| sdl: "type Query {\n states: [State]\n}\n\ntype State @key(fields: \"id\") {\n id: Int\n value: String\n}"
| sdl: "schema @link(url: \"https://specs.apollo.dev/federation/v2.0\", import: [\"@key\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\", \"@tag\"]) {\n query: Query\n}\n\ntype Query {\n states: [State]\n}\n\ntype State @key(fields: \"id\") {\n id: Int\n value: String\n}"
| }
| }
|}""".stripMargin))
Expand All @@ -259,7 +259,7 @@ class FederationSpec extends AsyncFreeSpec {
.map(QueryRenderer.renderPretty(_) should be("""{
| data: {
| _service: {
| sdl: "\"The `Long` scalar type represents non-fractional signed whole numeric values. Long can represent values between -(2^63) and 2^63 - 1.\"\nscalar Long\n\ntype Query {\n foo: Long\n bar: Int\n}"
| sdl: "schema @link(url: \"https://specs.apollo.dev/federation/v2.0\", import: [\"@key\", \"@shareable\", \"@inaccessible\", \"@override\", \"@external\", \"@provides\", \"@requires\", \"@tag\"]) {\n query: Query\n}\n\n\"The `Long` scalar type represents non-fractional signed whole numeric values. Long can represent values between -(2^63) and 2^63 - 1.\"\nscalar Long\n\ntype Query {\n foo: Long\n bar: Int\n}"
| }
| }
|}""".stripMargin))
Expand Down

0 comments on commit bc6d8da

Please sign in to comment.