Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directives emission #1553

Merged
merged 3 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
type Query {

person: Person
}
type Person {
name : String
firstName : String
name: String
firstName: String @deprecated(reason: Use `name` instead)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type Query {
rootField: String @example(text: Jorge)
}
enum MyEnum @example(text: Jorge) {
MyEnumValue @example(text: Jorge)
}
type MyObject2 {
myField2: String
}
interface MyInterface @example(text: Jorge) {
myField: String!
}
type MyInputObject @example(text: Jorge) {
myField: String! @example(text: Jorge)
}
scalar MyScalar @example(text: Jorge)
type MyObject @example(text: Jorge) {
myField(myArgument: String @example(text: Jorge)): String! @example(text: Jorge)
}
union MyUnion @example(text: Jorge) = MyObject | MyObject2
directive @example(text: String @example2) on ENUM | INPUT_FIELD_DEFINITION | ENUM_VALUE | SCALAR | ARGUMENT_DEFINITION | SCHEMA | INPUT_OBJECT | FIELD_DEFINITION | INTERFACE | OBJECT | UNION
directive @example2 on ARGUMENT_DEFINITION
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
schema @example(text: "Jorge") {
query: Query
}

type Query {
rootField: String @example(text: "Jorge")
}

scalar MyScalar @example(text: "Jorge")

type MyObject @example(text: "Jorge") {
myField(myArgument: String @example(text: "Jorge")): String! @example(text: "Jorge")
}

interface MyInterface @example(text: "Jorge") {
myField: String!
}

type MyObject2 {
myField2: String
}

union MyUnion @example(text: "Jorge") = MyObject | MyObject2

enum MyEnum @example(text: "Jorge") {
MyEnumValue @example(text: "Jorge")
}

input MyInputObject @example(text: "Jorge") {
myField: String! @example(text: "Jorge")
}

directive @example(text: String @example2) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
directive @example2 on ARGUMENT_DEFINITION

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Query {

name: String
}
directive @MyDirective(text: String) on SCHEMA
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Query {

name: String
}
directive @MyDirective on SCHEMA | OBJECT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type Query {

name: String
}
directive @MyDirective on SCHEMA
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type Query {
name: String
}
extend scalar Date
extend scalar Date @example
directive @example on SCALAR
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
type Query {

a: String
}
type InputType {
name : String
name: String
}
enum Enum {
A
B
}
scalar Pepe
directive @testInputType(input_: InputType) on SCHEMA
directive @testEnum(input_: Enum) on SCHEMA
directive @testScalar(input_: String) on SCHEMA
directive @testCustomScalar(input_: Pepe) on SCHEMA
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package amf.graphql.internal.spec.annotations
import amf.core.client.scala.model.domain.Annotation

case class GraphQLLocation(location: Set[String]) extends Annotation
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package amf.graphql.internal.spec.domain
import amf.apicontract.internal.validation.definitions.ParserSideValidations
import amf.core.client.scala.model.domain.extensions.{CustomDomainProperty, PropertyShape}
import amf.core.internal.parser.domain.Annotations.virtual
import amf.graphql.internal.spec.annotations.GraphQLLocation
import amf.graphql.internal.spec.context.GraphQLBaseWebApiContext
import amf.graphql.internal.spec.parser.syntax.TokenTypes._
import amf.graphql.internal.spec.parser.syntax.{GraphQLASTParserHelper, Locations}
Expand Down Expand Up @@ -52,11 +53,14 @@ case class GraphQLDirectiveDeclarationParser(node: Node)(implicit val ctx: Graph
}

private def parseLocations(): Unit = {
var domains = Set[String]()
var domains = Set[String]()
var locations = Set[String]()
collect(node, Seq(DIRECTIVE_LOCATIONS, DIRECTIVE_LOCATION)).foreach { n =>
path(n, Seq(TYPE_SYSTEM_DIRECTIVE_LOCATION)) match {
case Some(graphqlLocation: Node) =>
val domainsFromLocation = getDomains(graphqlLocation).toSet
val locationName = graphqlLocation.children.head.asInstanceOf[Terminal].value
locations = locations + locationName
val domainsFromLocation = getDomains(locationName).toSet
domains = domainsFromLocation ++: domains
case _ =>
n match {
Expand All @@ -65,13 +69,11 @@ case class GraphQLDirectiveDeclarationParser(node: Node)(implicit val ctx: Graph
}
}
}
directive.add(GraphQLLocation(locations))
directive.withDomain(domains.toSeq)
}

private def getDomains(location: Node): Seq[String] = {
val locationName: String = location.children.head.asInstanceOf[Terminal].value
Locations.locationToDomain.getOrElse(locationName, Seq())
}
private def getDomains(locationName: String): Seq[String] = Locations.locationToDomain.getOrElse(locationName, Seq())

private def checkErrorNode(children: Seq[ASTElement]): Unit = {
children.foreach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import amf.core.client.scala.model.document.{BaseUnit, Document}
import amf.core.client.scala.model.domain.extensions.CustomDomainProperty
import amf.core.internal.plugins.syntax.StringDocBuilder
import amf.graphql.internal.spec.emitter.context.GraphQLEmitterContext
import amf.graphql.internal.spec.emitter.domain.{GraphQLDescriptionEmitter, GraphQLEmitter, GraphQLRootTypeEmitter, GraphQLTypeEmitter}
import amf.graphql.internal.spec.emitter.domain.{
GraphQLDescriptionEmitter,
GraphQLDirectiveApplicationsRenderer,
GraphQLDirectiveDeclarationEmitter,
GraphQLEmitter,
GraphQLRootTypeEmitter,
GraphQLTypeEmitter
}
import amf.graphql.internal.spec.emitter.helpers.LineEmitter
import amf.shapes.client.scala.model.domain.AnyShape

class GraphQLDocumentEmitter(document: BaseUnit, builder: StringDocBuilder) extends GraphQLEmitter {
Expand All @@ -23,38 +31,47 @@ class GraphQLDocumentEmitter(document: BaseUnit, builder: StringDocBuilder) exte

// TODO: schema is not being rendered
private def emitSchema(doc: StringDocBuilder) = {
val directives = GraphQLDirectiveApplicationsRenderer(ctx.webApi)

doc.fixed { b =>
GraphQLDescriptionEmitter(ctx.webApi.description.option(), ctx, b).emit()
b.+=("schema {")
b.obj { obj =>
ctx.queryType.foreach { queryType =>
obj.+=(s"query: ${queryType.name}")
}
ctx.mutationType.foreach { mutationType =>
obj.+=(s"mutation: ${mutationType.name}")
}
ctx.subscriptionType.foreach { subscriptionType =>
obj.+=(s"subscription: ${subscriptionType.name}")
}
LineEmitter(b, "schema", directives, "{").emit()
emitRootOperationTypeDefinitions(b)
LineEmitter(b, "}").emit()
}
}

private def emitRootOperationTypeDefinitions(b: StringDocBuilder) = {
b.obj { obj =>
ctx.queryType.foreach { queryType =>
LineEmitter(obj, "query:", queryType.name).emit()
}
ctx.mutationType.foreach { mutationType =>
LineEmitter(obj, "mutation:", mutationType.name).emit()
}
ctx.subscriptionType.foreach { subscriptionType =>
LineEmitter(obj, "subscription:", subscriptionType.name).emit()
}
b.+=("}")
}
}

def emitTopLevelTypes(b: StringDocBuilder): Unit = {
private def emitTopLevelTypes(b: StringDocBuilder): Unit = {
val rootLevelTypes = ctx.queryType ++ ctx.mutationType ++ ctx.subscriptionType
rootLevelTypes.foreach { queryType =>
GraphQLRootTypeEmitter(queryType, ctx, b).emit()
}
}

def emitDeclarations(doc: StringDocBuilder): Unit = {
private def emitDeclarations(doc: StringDocBuilder): Unit = {
document.asInstanceOf[Document].declares.foreach {
case shape: AnyShape =>
GraphQLTypeEmitter(shape, ctx, doc).emit()
case directive: CustomDomainProperty =>
// TODO: emit directive declarations, something like: GraphQLDirectiveDeclarationEmitter(directive, ctx, doc).emit()
case directive: CustomDomainProperty if !isStandardDirective(directive) =>
GraphQLDirectiveDeclarationEmitter(directive, ctx, doc).emit()
case _ => // ignore
}
}

// TODO: Change this filter to an annotation to make it scalable
private def isStandardDirective(directive: CustomDomainProperty): Boolean = directive.name.value() == "deprecated"
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
package amf.graphql.internal.spec.emitter.domain

import amf.apicontract.client.scala.model.domain.Parameter
import amf.core.client.scala.model.domain.ScalarNode
import amf.core.client.scala.model.domain.{DataNode, NamedDomainElement, ScalarNode}
import amf.core.client.scala.model.domain.extensions.PropertyShape
import amf.graphql.internal.spec.emitter.context.GraphQLEmitterContext
import amf.graphql.internal.spec.emitter.helpers.StringBuilder

case class GeneratedGraphQLArgument(documentation: Option[String], value: String)

case class GraphQLArgumentGenerator(param: Parameter, ctx: GraphQLEmitterContext) extends GraphQLEmitter {
case class GraphQLDirectiveArgumentGenerator(arg: PropertyShape, ctx: GraphQLEmitterContext)
extends GraphQLArgumentGenerator(arg, ctx) {
override def argumentType: String = typeTarget(arg.range)
override def description: Option[String] = arg.description.option()
override def value: String = getDefaultValue(arg.default)
}

def generate(): GeneratedGraphQLArgument = {
val name = param.name.value()
val argumentType = extractGraphQLType
val description = param.description.option()
case class GraphQLOperationArgumentGenerator(arg: Parameter, ctx: GraphQLEmitterContext)
extends GraphQLArgumentGenerator(arg, ctx) {
override def argumentType: String = {
val graphQLType = typeTarget(arg.schema)
if (arg.required.option().getOrElse(false)) graphQLType else cleanNonNullable(graphQLType)
}
override def description: Option[String] = arg.description.option()
override def value: String = getDefaultValue(arg.defaultValue)
}

val argumentDefinition = defaultValue match {
case Some(defaultValue) => s"$name: $argumentType = $defaultValue"
case _ => s"$name: $argumentType"
}
abstract class GraphQLArgumentGenerator(arg: NamedDomainElement, ctx: GraphQLEmitterContext) extends GraphQLEmitter {

def generate(): GeneratedGraphQLArgument = {
val argumentDefinition = StringBuilder(s"$name:", argumentType, defaultValue, directives)
GeneratedGraphQLArgument(description, argumentDefinition)
}

private def extractGraphQLType: String = {
val argumentType = typeTarget(param.schema)
if (param.required.option().getOrElse(false)) argumentType else cleanNonNullable(argumentType)
}
protected def name: String = arg.name.value()
protected def defaultValue: String = if (value.nonEmpty) s"= $value" else ""

private def defaultValue: Option[String] = {
param.defaultValue match {
case defaultValueNode: ScalarNode => Some(defaultValueNode.value.value())
case _ => None
protected def getDefaultValue(node: DataNode): String = {
node match {
case defaultValueNode: ScalarNode => defaultValueNode.value.value()
case _ => ""
}
}
def argumentType: String
def description: Option[String]
def value: String
def directives: String = GraphQLDirectiveApplicationsRenderer(arg)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package amf.graphql.internal.spec.emitter.domain
import amf.core.internal.plugins.syntax.StringDocBuilder
import amf.graphql.internal.spec.emitter.context.GraphQLEmitterContext
import amf.graphql.internal.spec.emitter.helpers.LineEmitter
import org.mulesoft.common.client.lexical.Position

case class GraphQLDescriptionEmitter(
Expand All @@ -12,13 +13,9 @@ case class GraphQLDescriptionEmitter(
def emit(): Unit = {
description match {
case Some(desc) =>
if (pos.isDefined) {
b.+=("\"\"\"", pos.get)
} else {
b.+=("\"\"\"")
}
b.+=(desc)
b.+=("\"\"\"")
if (pos.isDefined) LineEmitter(b, "\"\"\"").emit() else LineEmitter(b, "\"\"\"").emit()
LineEmitter(b, desc).emit()
LineEmitter(b, "\"\"\"").emit()
case _ => // ignore
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package amf.graphql.internal.spec.emitter.domain
import amf.core.client.scala.model.domain.extensions.DomainExtension
import amf.core.client.scala.model.domain.{DataNode, NamedDomainElement, ObjectNode, ScalarNode}

object GraphQLDirectiveApplicationsRenderer {
def apply(elem: NamedDomainElement): String = {
val applications = elem.customDomainProperties
applications.map(renderDirectiveApplication(_)).mkString(" ")
}

private def renderDirectiveApplication(directive: DomainExtension) = {
val name = directive.name.value()
renderDirectiveArguments(directive) match {
case Some(arguments) => s"@$name($arguments)"
case _ => s"@$name"
}
}

private def renderDirectiveArguments(directive: DomainExtension): Option[String] = {
val arguments = directive.extension.asInstanceOf[ObjectNode].allProperties()
val hasArguments = arguments.nonEmpty

if (hasArguments) {
val args = arguments.foldLeft("") { (acc, argument) =>
val argString = buildArgumentString(argument)
if (renderingFirstArgument(acc)) argString else s"$acc, $argString"
}
Some(args)
} else {
None
}
}

private def buildArgumentString(arg: DataNode) = {
val argName = arg.asInstanceOf[ScalarNode].name.value()
val value = arg.asInstanceOf[ScalarNode].value.value()
s"$argName: $value"
}

private def renderingFirstArgument(acc: String) = acc.isEmpty

}
Loading