Skip to content

Instantly share code, notes, and snippets.

@BjRo
Created March 13, 2018 17:01
Show Gist options
  • Save BjRo/46be3edb2fe960d3fffb0dd2413adcc9 to your computer and use it in GitHub Desktop.
Save BjRo/46be3edb2fe960d3fffb0dd2413adcc9 to your computer and use it in GitHub Desktop.
Preview fields using FieldTags in Sangria
class PreviewSchemaFilter extends DefaultAstSchemaBuilder[RequestContext] {
override def extendObjectType(origin: MatOrigin,
existing: ObjectType[RequestContext, _],
extensions: List[TypeExtensionDefinition],
fields: () => List[Field[RequestContext, Any]],
interfaces: List[InterfaceType[RequestContext, Any]],
mat: AstSchemaMaterializer[RequestContext]): ObjectType[RequestContext, Any] = {
existing.asInstanceOf[ObjectType[RequestContext, Any]].copy(fieldsFn = () => fields().filterNot(_.tags.contains(Preview)))
}
}
object PreviewSchemaFilter {
//TODO: This can probably be changed in Sangria 1.4
//Sangria 1.4 allows type extensions and type definitions without fields
private val emptyStub: Document =
Document(
Vector(
ObjectTypeDefinition(
name = "GeneratedPreviewSchema",
description = Some(StringValue("used to generate preview fields")),
interfaces = Vector.empty,
fields = Vector(
FieldDefinition(
name = "generated",
fieldType = NamedType("String"),
arguments = Vector.empty,
directives = Vector(
Directive(
name = "deprecated",
arguments = Vector(
Argument("reason", StringValue("used to generate preview fields"))
)
)
)
)
)
)
))
def filter(sangriaSchema: SangriaSchema): SangriaSchema = {
AstSchemaMaterializer.extendSchema(sangriaSchema, emptyStub, new PreviewSchemaFilter())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment