Last active
December 19, 2017 17:01
-
-
Save OlegIlyenko/b98c66ef64518c05862a36a4891c0814 to your computer and use it in GitHub Desktop.
Function to render the schema with legacy comment-based descriptions
This file contains hidden or 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
import sangria.ast | |
import sangria.ast.AstVisitor | |
import sangria.schema._ | |
import sangria.renderer.{QueryRenderer, QueryRendererConfig, SchemaFilter} | |
import sangria.visitor.VisitorCommand | |
def renderSchemaWithLegacyDescriptions(schema: Schema[_, _], filter: SchemaFilter = SchemaFilter.withoutSangriaBuiltIn, config: QueryRendererConfig = QueryRenderer.Pretty) = { | |
def commentDescription(node: ast.WithDescription) = | |
node.description.toVector.flatMap(sv ⇒ sv.value.split("\\r?\\n").toVector.map(ast.Comment(_))) | |
val schemaAst = | |
AstVisitor.visit(schema.toAst(filter), AstVisitor { | |
case n: ast.DirectiveDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.InterfaceTypeDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.EnumTypeDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.EnumValueDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.FieldDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.InputObjectTypeDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.InputValueDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.ObjectTypeDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.ScalarTypeDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
case n: ast.UnionTypeDefinition if n.description.isDefined ⇒ | |
VisitorCommand.Transform(n.copy(description = None, comments = n.comments ++ commentDescription(n))) | |
}) | |
QueryRenderer.render(schemaAst, config) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment