SDLPrinter is an optimized visitor used to output GraphQL Schema Definition Language (SDL) from an AST into a TextWriter. It is designed to be asynchronous and non-blocking, often avoiding managed heap allocations.
Available methods include:
Print(document): Returns the SDL as a string.Print(document, stringBuilder): Writes the SDL to a StringBuilder.PrintAsync(document, writer, cancellationToken): Asynchronously writes the SDL to a TextWriter.
var document = Parser.Parse("query { hero { name age } }");
// Print to a string with default options
var sdl = new SDLPrinter().Print(document);
// Print to a string builder
var sb = new StringBuilder();
new SDLPrinter().Print(document, sb);
// Print to a string with custom options
var sdlPrinter = new SDLPrinter(
new SDLPrinterOptions
{
PrintComments = true,
EachDirectiveLocationOnNewLine = true,
EachUnionMemberOnNewLine = true,
});
var sdlCustom = sdlPrinter.Print(document);
// Print to a stream asynchronously
using var writer = new StreamWriter(stream);
await sdlPrinter.PrintAsync(document, writer, default);
await writer.FlushAsync();