diff --git a/CHANGELOG.md b/CHANGELOG.md index a0ca05cf..b141e45a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ ## Upcoming +* Added support for rendering SDL with legacy comment-based descriptions (#334). You can use it with `SchemaFilter.default.withLegacyCommentDescriptions`. * Improved `ResolverBasedAstSchemaBuilder` and introduced `InputTypeResolver`/`OutputTypeResolver`. +* Removed previously deprecated `SchemaRenderer.renderIntrospectionSchema`. ## v1.3.3 (2017-12-03) diff --git a/src/main/scala/sangria/renderer/SchemaRenderer.scala b/src/main/scala/sangria/renderer/SchemaRenderer.scala index 32028203..0212debb 100644 --- a/src/main/scala/sangria/renderer/SchemaRenderer.scala +++ b/src/main/scala/sangria/renderer/SchemaRenderer.scala @@ -5,8 +5,10 @@ import sangria.introspection._ import sangria.marshalling.{InputUnmarshaller, ToInput} import sangria.schema._ import sangria.ast +import sangria.ast.{AstNode, AstVisitor} import sangria.introspection.__DirectiveLocation import sangria.parser.QueryParser +import sangria.visitor.VisitorCommand object SchemaRenderer { def renderTypeName(tpe: Type, topLevel: Boolean = false) = { @@ -214,7 +216,7 @@ object SchemaRenderer { private def renderDirective(dir: IntrospectionDirective) = ast.DirectiveDefinition(dir.name, renderArgsI(dir.args), dir.locations.toVector.map(renderDirectiveLocation).sortBy(_.name), renderDescription(dir.description)) - def schemaAstFromIntrospection(introspectionSchema: IntrospectionSchema, filter: SchemaFilter = SchemaFilter.withoutSangriaBuiltIn): ast.Document = { + def schemaAstFromIntrospection(introspectionSchema: IntrospectionSchema, filter: SchemaFilter = SchemaFilter.default): ast.Document = { val schemaDef = if (filter.renderSchema) renderSchemaDefinition(introspectionSchema) else None val types = introspectionSchema.types filter (t ⇒ filter.filterTypes(t.name)) sortBy (_.name) map renderType val directives = introspectionSchema.directives filter (d ⇒ filter.filterDirectives(d.name)) sortBy (_.name) map renderDirective @@ -223,12 +225,12 @@ object SchemaRenderer { } def renderSchema(introspectionSchema: IntrospectionSchema): String = - schemaAstFromIntrospection(introspectionSchema, SchemaFilter.withoutSangriaBuiltIn).renderPretty + schemaAstFromIntrospection(introspectionSchema, SchemaFilter.default).renderPretty def renderSchema[T: InputUnmarshaller](introspectionResult: T): String = { import sangria.parser.DeliveryScheme.Throw - schemaAstFromIntrospection(IntrospectionParser parse introspectionResult, SchemaFilter.withoutSangriaBuiltIn).renderPretty + schemaAstFromIntrospection(IntrospectionParser parse introspectionResult, SchemaFilter.default).renderPretty } def renderSchema(introspectionSchema: IntrospectionSchema, filter: SchemaFilter): String = @@ -240,62 +242,79 @@ object SchemaRenderer { schemaAstFromIntrospection(IntrospectionParser parse introspectionResult, filter).renderPretty } - def schemaAst(schema: Schema[_, _], filter: SchemaFilter = SchemaFilter.withoutSangriaBuiltIn): ast.Document = { + def schemaAst(schema: Schema[_, _], filter: SchemaFilter = SchemaFilter.default): ast.Document = { val schemaDef = if (filter.renderSchema) renderSchemaDefinition(schema) else None val types = schema.typeList filter (t ⇒ filter.filterTypes(t.name)) sortBy (_.name) map renderType val directives = schema.directives filter (d ⇒ filter.filterDirectives(d.name)) sortBy (_.name) map renderDirective - ast.Document(schemaDef.toVector ++ types ++ directives) + val document = ast.Document(schemaDef.toVector ++ types ++ directives) + + if (filter.legacyCommentDescriptions) transformLegacyCommentDescriptions(document) + else document } + def transformLegacyCommentDescriptions[T <: AstNode](node: T): T = + AstVisitor.visit(node, 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))) + }) + + private def commentDescription(node: ast.WithDescription) = + node.description.toVector.flatMap(sv ⇒ sv.value.split("\\r?\\n").toVector.map(ast.Comment(_))) + def renderSchema(schema: Schema[_, _]): String = - schemaAst(schema, SchemaFilter.withoutSangriaBuiltIn).renderPretty + schemaAst(schema, SchemaFilter.default).renderPretty def renderSchema(schema: Schema[_, _], filter: SchemaFilter): String = schemaAst(schema, filter).renderPretty - - private def introspectionSchemaAst(introspectionSchema: IntrospectionSchema): ast.Document = { - val types = introspectionSchema.types filter (tpe ⇒ Schema.isIntrospectionType(tpe.name)) sortBy (_.name) map (renderType(_)) - val directives = introspectionSchema.directives filter (d ⇒ Schema.isBuiltInDirective(d.name)) sortBy (_.name) map (renderDirective(_)) - - ast.Document(types.toVector ++ directives) - } - - @deprecated("use `renderSchema` with `SchemaFilter.introspection`", "1.2.1") - def renderIntrospectionSchema(introspectionSchema: IntrospectionSchema): String = - introspectionSchemaAst(introspectionSchema).renderPretty - - @deprecated("use `renderSchema` with `SchemaFilter.introspection`", "1.2.1") - def renderIntrospectionSchema[T: InputUnmarshaller](introspectionResult: T): String = { - import sangria.parser.DeliveryScheme.Throw - - renderIntrospectionSchema(IntrospectionParser parse introspectionResult) - } } -case class SchemaFilter(filterTypes: String ⇒ Boolean, filterDirectives: String ⇒ Boolean, renderSchema: Boolean = true) +case class SchemaFilter(filterTypes: String ⇒ Boolean, filterDirectives: String ⇒ Boolean, renderSchema: Boolean = true, legacyCommentDescriptions: Boolean = false) { + @deprecated("Please migrate to new string-based description format", "1.3.4") + def withLegacyCommentDescriptions = copy(legacyCommentDescriptions = true) +} object SchemaFilter { - val withoutSangriaBuiltIn = SchemaFilter( + val withoutSangriaBuiltIn: SchemaFilter = SchemaFilter( typeName ⇒ !Schema.isBuiltInType(typeName), dirName ⇒ !Schema.isBuiltInDirective(dirName)) + val default: SchemaFilter = withoutSangriaBuiltIn + val withoutGraphQLBuiltIn = SchemaFilter( typeName ⇒ !Schema.isBuiltInGraphQLType(typeName), dirName ⇒ !Schema.isBuiltInDirective(dirName)) - val withoutIntrospection = SchemaFilter( + val withoutIntrospection: SchemaFilter = SchemaFilter( typeName ⇒ !Schema.isIntrospectionType(typeName), Function.const(true)) - val builtIn = SchemaFilter( + val builtIn: SchemaFilter = SchemaFilter( typeName ⇒ Schema.isBuiltInType(typeName), dirName ⇒ Schema.isBuiltInDirective(dirName)) - val introspection = SchemaFilter( + val introspection: SchemaFilter = SchemaFilter( typeName ⇒ Schema.isIntrospectionType(typeName), Function.const(false), renderSchema = false) - val all = SchemaFilter(Function.const(true), Function.const(true)) + val all: SchemaFilter = SchemaFilter(Function.const(true), Function.const(true)) } \ No newline at end of file diff --git a/src/test/scala/sangria/renderer/SchemaRenderSpec.scala b/src/test/scala/sangria/renderer/SchemaRenderSpec.scala index d6711c35..db00dccc 100644 --- a/src/test/scala/sangria/renderer/SchemaRenderSpec.scala +++ b/src/test/scala/sangria/renderer/SchemaRenderSpec.scala @@ -603,7 +603,7 @@ class SchemaRenderSpec extends WordSpec with Matchers with FutureResultSupport w "Print Introspection Schema" in { val schema = Schema(ObjectType("Root", fields[Unit, Unit](Field("foo", IntType, resolve = _ ⇒ 1)))) val rendered = SchemaRenderer.renderSchema(Executor.execute(schema, introspectionQuery).await, SchemaFilter.introspection) - + ("\n" + rendered + "\n") should equal (s""" |$quotes |A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document. @@ -769,4 +769,169 @@ class SchemaRenderSpec extends WordSpec with Matchers with FutureResultSupport w |""".stripMargin) (after being strippedOfCarriageReturns) } } + + "Print schema with legacy comment descriptions" in { + val schema = Schema(ObjectType("Root", fields[Unit, Unit](Field("foo", IntType, resolve = _ ⇒ 1)))) + val rendered = schema.renderPretty(SchemaFilter.introspection.withLegacyCommentDescriptions) + + ("\n" + rendered + "\n") should equal (""" + |# A Directive provides a way to describe alternate runtime execution and type validation behavior in a GraphQL document. + |# + |# In some cases, you need to provide options to alter GraphQL’s execution behavior in ways field arguments will not suffice, such as conditionally including or skipping a field. Directives provide this by describing additional information to the executor. + |type __Directive { + | name: String! + | description: String + | locations: [__DirectiveLocation!]! + | args: [__InputValue!]! + | onOperation: Boolean! @deprecated(reason: "Use `locations`.") + | onFragment: Boolean! @deprecated(reason: "Use `locations`.") + | onField: Boolean! @deprecated(reason: "Use `locations`.") + |} + | + |# A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies. + |enum __DirectiveLocation { + | # Location adjacent to a query operation. + | QUERY + | + | # Location adjacent to a mutation operation. + | MUTATION + | + | # Location adjacent to a subscription operation. + | SUBSCRIPTION + | + | # Location adjacent to a field. + | FIELD + | + | # Location adjacent to a fragment definition. + | FRAGMENT_DEFINITION + | + | # Location adjacent to a fragment spread. + | FRAGMENT_SPREAD + | + | # Location adjacent to an inline fragment. + | INLINE_FRAGMENT + | + | # Location adjacent to a schema definition. + | SCHEMA + | + | # Location adjacent to a scalar definition. + | SCALAR + | + | # Location adjacent to an object type definition. + | OBJECT + | + | # Location adjacent to a field definition. + | FIELD_DEFINITION + | + | # Location adjacent to an argument definition. + | ARGUMENT_DEFINITION + | + | # Location adjacent to an interface definition. + | INTERFACE + | + | # Location adjacent to a union definition. + | UNION + | + | # Location adjacent to an enum definition. + | ENUM + | + | # Location adjacent to an enum value definition. + | ENUM_VALUE + | + | # INPUT_OBJECT + | INPUT_OBJECT + | + | # Location adjacent to an input object field definition. + | INPUT_FIELD_DEFINITION + |} + | + |# One possible value for a given Enum. Enum values are unique values, not a placeholder for a string or numeric value. However an Enum value is returned in a JSON response as a string. + |type __EnumValue { + | name: String! + | description: String + | isDeprecated: Boolean! + | deprecationReason: String + |} + | + |# Object and Interface types are described by a list of Fields, each of which has a name, potentially a list of arguments, and a return type. + |type __Field { + | name: String! + | description: String + | args: [__InputValue!]! + | type: __Type! + | isDeprecated: Boolean! + | deprecationReason: String + |} + | + |# Arguments provided to Fields or Directives and the input fields of an InputObject are represented as Input Values which describe their type and optionally a default value. + |type __InputValue { + | name: String! + | description: String + | type: __Type! + | + | # A GraphQL-formatted string representing the default value for this input value. + | defaultValue: String + |} + | + |# A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations. + |type __Schema { + | # A list of all types supported by this server. + | types: [__Type!]! + | + | # The type that query operations will be rooted at. + | queryType: __Type! + | + | # If this server supports mutation, the type that mutation operations will be rooted at. + | mutationType: __Type + | + | # If this server support subscription, the type that subscription operations will be rooted at. + | subscriptionType: __Type + | + | # A list of all directives supported by this server. + | directives: [__Directive!]! + |} + | + |# The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum. + |# + |# Depending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types. + |type __Type { + | kind: __TypeKind! + | name: String + | description: String + | fields(includeDeprecated: Boolean = false): [__Field!] + | interfaces: [__Type!] + | possibleTypes: [__Type!] + | enumValues(includeDeprecated: Boolean = false): [__EnumValue!] + | inputFields: [__InputValue!] + | ofType: __Type + |} + | + |# An enum describing what kind of type a given `__Type` is. + |enum __TypeKind { + | # Indicates this type is a scalar. + | SCALAR + | + | # Indicates this type is an object. `fields` and `interfaces` are valid fields. + | OBJECT + | + | # Indicates this type is an interface. `fields` and `possibleTypes` are valid fields. + | INTERFACE + | + | # Indicates this type is a union. `possibleTypes` is a valid field. + | UNION + | + | # Indicates this type is an enum. `enumValues` is a valid field. + | ENUM + | + | # Indicates this type is an input object. `inputFields` is a valid field. + | INPUT_OBJECT + | + | # Indicates this type is a list. `ofType` is a valid field. + | LIST + | + | # Indicates this type is a non-null. `ofType` is a valid field. + | NON_NULL + |} + |""".stripMargin) (after being strippedOfCarriageReturns) + } }