-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better support for custom directives
Prototype for #913 In this prototype, we check how we can apply custom directives much easier. Furthermore, we check if we can prove type safety, to that a custom directive that can be applied on some elements cannot be applied on others. Limitations: - a directive can be applied on a field definition for example. With that current approach, we cannot formulate that with the type system as a field definition lives in sangria.ast, and we only handle sangria.schema types. - we are introducing new types to mark on which elements a directive can be applied. Those types are kind of duplication of the current [sangria.schema.DirectiveLocation values](https://github.com/sangria-graphql/sangria/blob/f339b5df97bd89c2a24fcfc977a1f20191ffd7fc/modules/core/src/main/scala/sangria/schema/Schema.scala#L1136-L1158).
- Loading branch information
Showing
5 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
modules/core/src/test/scala/sangria/schema/CustomDirectiveSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package sangria.schema | ||
|
||
import org.scalatest.matchers.should.Matchers | ||
import org.scalatest.wordspec.AnyWordSpec | ||
import sangria.ast | ||
import sangria.renderer.QueryRenderer | ||
import sangria.schema.DirectiveLocationValue.On | ||
import sangria.util.tag.@@ | ||
|
||
class CustomDirectiveSpec extends AnyWordSpec with Matchers { | ||
|
||
case class Domain(value: Int) | ||
|
||
private val AllDirective = new ast.Directive("field-directive") | ||
with On[DirectiveLocationValue.Field.type with DirectiveLocationValue.ArgumentDefinition.type] | ||
|
||
// with OnField | ||
// with OnArgument | ||
// with OnObjectType | ||
// with OnInterfaceType | ||
|
||
private val FieldDirective: ast.Directive with On[DirectiveLocationValue.Field.type] = | ||
new ast.Directive("field-directive") with On[DirectiveLocationValue.Field.type] | ||
|
||
// private val ArgumentDirective = new ast.Directive("arg-directive") with OnArgument | ||
// | ||
// private val ObjectDirective = new ast.Directive("object-directive") with OnObjectType | ||
// | ||
// private val InterfaceDirective = new ast.Directive("interface-directive") with OnInterfaceType | ||
// | ||
// private val CustomDirective = ast.Directive("custom-directive") | ||
|
||
private val resolve: Context[Unit, Domain] => Action[Unit, Int] = _.value.value | ||
|
||
"custom directive" when { | ||
"in context of a Field" should { | ||
"be applied if marked with OnField" in { | ||
fields[Unit, Domain]( | ||
Field("field", IntType, resolve = resolve, astDirectives = Vector(FieldDirective))) | ||
|
||
fields[Unit, Domain]( | ||
Field("field", IntType, resolve = resolve, astDirectives = Vector(AllDirective))) | ||
|
||
fields[Unit, Domain]( | ||
Field( | ||
"field", | ||
IntType, | ||
resolve = resolve, | ||
astDirectives = Vector(FieldDirective, AllDirective))) | ||
|
||
Field("field", IntType, resolve = resolve, astDirectives = Vector(FieldDirective)): Field[ | ||
Unit, | ||
Domain] | ||
|
||
// val field = (Field("field", IntType, resolve = resolve): Field[Unit, Domain]) | ||
// .withDirective(FieldDirective) | ||
// .withDirectives(FieldDirective, AllDirective) | ||
// field.astDirectives should be(Vector(FieldDirective, FieldDirective, AllDirective)) | ||
} | ||
|
||
"not be applied if not marked with OnField" in { | ||
assertTypeError(""" | ||
|fields[Unit, Domain]( | ||
| Field("field", IntType, resolve = resolve, astDirectives = Vector(CustomDirective))) | ||
|""".stripMargin) | ||
|
||
assertTypeError(""" | ||
|val field: Field[Unit, Domain] = | ||
| Field("field", IntType, resolve = resolve, astDirectives = Vector(CustomDirective)) | ||
|""".stripMargin) | ||
} | ||
|
||
// "be combined with the @deprecated directive" in { | ||
// val field = (Field( | ||
// "field", | ||
// IntType, | ||
// resolve = resolve, | ||
// deprecationReason = Some("use field2")): Field[Unit, Domain]) | ||
// .withDirective(FieldDirective) | ||
// | ||
// field.astDirectives should be(Vector(FieldDirective)) | ||
// QueryRenderer.renderPretty(field.toAst) should equal( | ||
// """field: Int! @field-directive @deprecated(reason: "use field2")""") | ||
// } | ||
} | ||
} | ||
|
||
// "in context of an Argument" should { | ||
// "be applied if marked with OnArgument" in { | ||
// Argument("name", IntType, 42, astDirectives = Vector(ArgumentDirective)) | ||
// Argument("name", IntType, 42, astDirectives = Vector(AllDirective)) | ||
// Argument("name", IntType, 42).withDirective(ArgumentDirective) | ||
// val arg = Argument("name", IntType, 42) | ||
// .withDirective(AllDirective) | ||
// .withDirectives(ArgumentDirective, ArgumentDirective) | ||
// arg.astDirectives should be(Vector(AllDirective, ArgumentDirective, ArgumentDirective)) | ||
// } | ||
// | ||
// "not be applied if not marked with OnArgument" in { | ||
// assertTypeError(""" | ||
// |Argument("name", IntType, 42, astDirectives = Vector(FieldDirective)) | ||
// |""".stripMargin) | ||
// assertTypeError(""" | ||
// |Argument("name", IntType, 42).withDirective(FieldDirective) | ||
// |""".stripMargin) | ||
// } | ||
// } | ||
// | ||
// "in context of an ObjectType" should { | ||
// "be applied if marked with OnObjectType" in { | ||
// val obj = ObjectType[Unit, Domain]("name", fields[Unit, Domain]()) | ||
// .withDirective(ObjectDirective) | ||
// .withDirectives(AllDirective, ObjectDirective) | ||
// obj.astDirectives should be(Vector(ObjectDirective, AllDirective, ObjectDirective)) | ||
// } | ||
// | ||
// "not be applied if not marked with OnObjectType" in { | ||
// assertTypeError(""" | ||
// |ObjectType[Unit, Domain]("name", fields[Unit, Domain]()).withDirective(CustomDirective) | ||
// |""".stripMargin) | ||
// assertTypeError(""" | ||
// |ObjectType[Unit, Domain]("name", fields[Unit, Domain]()).withDirective(FieldDirective) | ||
// |""".stripMargin) | ||
// } | ||
// } | ||
// | ||
// "in context of an InterfaceType" should { | ||
// "be applied if marked with OnInterfaceType" in { | ||
// val interface = InterfaceType[Unit, Domain]("name", fields[Unit, Domain]()) | ||
// .withDirective(InterfaceDirective) | ||
// .withDirectives(AllDirective, InterfaceDirective) | ||
// interface.astDirectives should be( | ||
// Vector(InterfaceDirective, AllDirective, InterfaceDirective)) | ||
// } | ||
// | ||
// "not be applied if not marked with OnObjectType" in { | ||
// assertTypeError(""" | ||
// |InterfaceType[Unit, Domain]("name", fields[Unit, Domain]()).withDirective(CustomDirective) | ||
// |""".stripMargin) | ||
// assertTypeError(""" | ||
// |InterfaceType[Unit, Domain]("name", fields[Unit, Domain]()).withDirective(FieldDirective) | ||
// |""".stripMargin) | ||
// } | ||
// } | ||
} |