Skip to content

Instantly share code, notes, and snippets.

@OlegIlyenko
Last active December 19, 2017 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OlegIlyenko/b98c66ef64518c05862a36a4891c0814 to your computer and use it in GitHub Desktop.
Save OlegIlyenko/b98c66ef64518c05862a36a4891c0814 to your computer and use it in GitHub Desktop.
Function to render the schema with legacy comment-based descriptions
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