Skip to content

Commit

Permalink
add @composeDirective to sangria implementation (#555)
Browse files Browse the repository at this point in the history
Co-authored-by: ogotte <[email protected]>
  • Loading branch information
oskargotte and oskargotte authored Nov 14, 2023
1 parent f646cc6 commit dbdf379
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions implementations/sangria/products.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ extend schema
"@tag"
]
)
@link(url: "https://myspecs.dev/myDirective/v1.0", import: ["@custom"])
@composeDirective(name: "@custom")

directive @custom on OBJECT

type Product
@custom
@key(fields: "id")
@key(fields: "sku package")
@key(fields: "sku variation { id }") {
Expand Down
7 changes: 5 additions & 2 deletions implementations/sangria/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import cats.effect.{ExitCode, IO, IOApp}
import com.comcast.ip4s.IpLiteralSyntax
import graphql.{AppContext, GraphQLSchema, InventoryGraphQLSchema, ProductGraphQLSchema, UserGraphQLSchema}
import graphql.{AppContext, CustomDirectiveSpec, GraphQLSchema, InventoryGraphQLSchema, ProductGraphQLSchema, UserGraphQLSchema}
import http.{GraphQLExecutor, GraphQLServer}
import io.circe.Json
import sangria.federation.v2.Federation
import sangria.federation.v2.{CustomDirectivesDefinition, Federation, Spec}
import sangria.marshalling.InputUnmarshaller
import sangria.renderer.QueryRenderer
import sangria.schema.Schema
Expand Down Expand Up @@ -43,6 +43,9 @@ object Main extends IOApp {
private def schemaAndUm: (Schema[AppContext, Unit], InputUnmarshaller[Json]) =
Federation.federate(
GraphQLSchema.schema,
CustomDirectivesDefinition(
Spec("https://myspecs.dev/myCustomDirective/v1.0") -> List(CustomDirectiveSpec.CustomDirectiveDefinition)
),
sangria.marshalling.circe.CirceInputUnmarshaller,
ProductGraphQLSchema.productResolver,
ProductGraphQLSchema.deprecatedProductResolver,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package graphql

import sangria.ast
import sangria.schema

object CustomDirectiveSpec {
val CustomDirective: ast.Directive = ast.Directive("custom")
val CustomDirectiveDefinition: schema.Directive =
schema.Directive("custom", locations = Set(schema.DirectiveLocation.Object))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package graphql

import graphql.CustomDirectiveSpec.CustomDirective
import graphql.UserGraphQLSchema.UserType
import io.circe.Json
import io.circe.generic.semiauto._
Expand Down Expand Up @@ -59,6 +60,7 @@ object ProductGraphQLSchema {
Field("research", ListType(ProductResearchType), resolve = _.value.research)
)
).withDirectives(
CustomDirective,
Key("id"),
Key("sku package"),
Key("sku variation { id }")
Expand Down

0 comments on commit dbdf379

Please sign in to comment.