Created
March 13, 2018 17:01
-
-
Save BjRo/46be3edb2fe960d3fffb0dd2413adcc9 to your computer and use it in GitHub Desktop.
Preview fields using FieldTags in Sangria
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
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