Skip to content

Commit

Permalink
W-10674610: added GraphQL custom scalar parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nschejtman committed Apr 20, 2022
1 parent f802b8f commit 25d22fc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import amf.core.internal.remote.Spec
import amf.graphql.internal.spec.context.GraphQLWebApiContext
import amf.graphql.internal.spec.context.GraphQLWebApiContext.RootTypes
import amf.graphql.internal.spec.domain.{
GraphQLCustomScalarParser,
GraphQLInputTypeParser,
GraphQLNestedEnumParser,
GraphQLNestedTypeParser,
Expand Down Expand Up @@ -82,6 +83,11 @@ case class GraphQLDocumentParser(root: Root)(implicit val ctx: GraphQLWebApiCont
ctx.declarations += enum
}

private def parseCustomScalarTypeDef(customScalarTypeDef: Node): Unit = {
val scalar: ScalarShape = new GraphQLCustomScalarParser(customScalarTypeDef).parse(doc.id)
ctx.declarations += scalar
}

private def processTypes(node: Node): Unit = {
this.collect(node, Seq(DOCUMENT, DEFINITION, TYPE_SYSTEM_DEFINITION, SCHEMA_DEFINITION)).toList match {
case head :: Nil => parseSchemaNode(head)
Expand Down Expand Up @@ -127,6 +133,13 @@ case class GraphQLDocumentParser(root: Root)(implicit val ctx: GraphQLWebApiCont
case enumTypeDef: Node =>
parseEnumType(enumTypeDef)
}

// let's parse custom scalars
this
.collect(node, Seq(DOCUMENT, DEFINITION, TYPE_SYSTEM_DEFINITION, TYPE_DEFINITION, SCALAR_TYPE_DEFINITION)) foreach {
case customScalarTypeDef: Node =>
parseCustomScalarTypeDef(customScalarTypeDef)
}
}

private def parseSchemaNode(schemaNode: ASTElement): Unit = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package amf.graphql.internal.spec.domain

import amf.core.client.scala.model.DataType
import amf.graphql.internal.spec.context.GraphQLWebApiContext
import amf.graphql.internal.spec.parser.syntax.GraphQLASTParserHelper
import amf.shapes.client.scala.model.domain.ScalarShape
import org.mulesoft.antlrast.ast.Node

class GraphQLCustomScalarParser(customScalarTypeDef: Node)(implicit val ctx: GraphQLWebApiContext) extends GraphQLASTParserHelper {
val scalar: ScalarShape = ScalarShape(toAnnotations(customScalarTypeDef))

def parse(parentId: String): ScalarShape = {
scalar.withDataType(DataType.String)
parseNameAndFormat()
scalar.adopted(parentId)
scalar
}

private def parseNameAndFormat(): Unit = {
val name= findName(customScalarTypeDef, "AnonymousScalar", "Missing scalar type name", scalar.id)
scalar.withName(name)
scalar.withFormat(name)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ object TokenTypes {
val ENUM_VALUE = "enumValue"
val TYPE_ = "type_"
val LIST_TYPE = "listType"
val SCALAR_TYPE_DEFINITION = "scalarTypeDefinition"
}

0 comments on commit 25d22fc

Please sign in to comment.