Skip to content

Commit

Permalink
Merge pull request #1580 from aml-org/W-11574070
Browse files Browse the repository at this point in the history
W-11574070: add matrix initial support & examples to graphql tck
  • Loading branch information
arielmirra authored Sep 6, 2022
2 parents 72269e9 + 523c2e0 commit c7c2afd
Show file tree
Hide file tree
Showing 11 changed files with 789 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package amf.apicontract.internal.validation.shacl.graphql
import amf.apicontract.client.scala.model.domain.EndPoint
import amf.apicontract.client.scala.model.domain.api.WebApi
import amf.core.client.scala.model.domain.{AmfScalar, DataNode, DomainElement}
import amf.core.client.scala.model.domain.extensions.{DomainExtension, PropertyShape}
import amf.apicontract.internal.validation.shacl.graphql.GraphQLUtils.inferGraphQLKind
import amf.core.client.scala.model.domain.extensions.DomainExtension
import amf.core.client.scala.model.domain.{AmfScalar, DomainElement}
import amf.core.internal.metamodel.domain.DomainElementModel
import amf.core.internal.metamodel.domain.common.{NameFieldSchema, NameFieldShacl}
import amf.core.internal.metamodel.domain.extensions.PropertyShapeModel
import amf.shapes.client.scala.model.domain.{NodeShape, ScalarShape, UnionShape}
import amf.shapes.client.scala.model.domain.operations.{ShapeOperation, ShapeParameter}
import amf.shapes.internal.domain.metamodel.operations.ShapeParameterModel
import amf.validation.internal.shacl.custom.CustomShaclValidator.ValidationInfo

Expand Down Expand Up @@ -77,22 +76,4 @@ object GraphQLDirectiveLocationValidator {
}
.map(name => s"'$name'")
}
private def inferGraphQLKind(element: DomainElement, appliedToDirectiveArgument: Boolean): String = {
element match {
case _: PropertyShape if appliedToDirectiveArgument => "argument"
case _: PropertyShape => "field"
case _: ShapeOperation => "field"
case _: ShapeParameter => "argument"
case s: ScalarShape if s.values.nonEmpty => "enum"
case _: ScalarShape => "scalar"
case n: NodeShape if n.isAbstract.value() => "interface"
case n: NodeShape if n.isInputOnly.value() => "input object"
case _: NodeShape => "object"
case _: UnionShape => "union"
case _: DataNode => "value"
case _: EndPoint => "field"
case _: WebApi => "schema"
case _ => "type" // should be unreachable
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package amf.apicontract.internal.validation.shacl.graphql

import amf.apicontract.client.scala.model.domain.api.WebApi
import amf.apicontract.client.scala.model.domain.{EndPoint, Response}
import amf.apicontract.internal.validation.shacl.graphql.GraphQLUtils.isValidOutputType
import amf.core.client.scala.model.DataType
import amf.core.client.scala.model.domain._
import amf.core.client.scala.model.domain.extensions.{CustomDomainProperty, DomainExtension, PropertyShape}
import amf.core.client.scala.model.domain.{DataNode, ObjectNode, ScalarNode, Shape}
import amf.core.internal.parser.domain.Annotations
import amf.shapes.client.scala.model.domain._
import amf.shapes.client.scala.model.domain.operations._
Expand Down Expand Up @@ -147,7 +148,7 @@ case class GraphQLNullable(union: UnionShape) {
union.anyOf match {
case List(_: NilShape, n: NodeShape) => n.name.value()
case List(_: NilShape, u: UnionShape) => GraphQLNullable(u).name
case List(_: NilShape, s: ScalarShape) => s.name.value()
case List(_: NilShape, s: ScalarShape) => s.name.option().getOrElse(GraphQLDataTypes.from(s))
case List(_: NilShape, a: ArrayShape) =>
val items = a.items match {
case u: UnionShape => GraphQLNullable(u).name
Expand Down Expand Up @@ -211,4 +212,23 @@ object GraphQLUtils {
case _ => true
}
}

def inferGraphQLKind(element: DomainElement, appliedToDirectiveArgument: Boolean): String = {
element match {
case _: PropertyShape if appliedToDirectiveArgument => "argument"
case _: PropertyShape => "field"
case _: ShapeOperation => "field"
case _: ShapeParameter => "argument"
case s: ScalarShape if s.values.nonEmpty => "enum"
case _: ScalarShape => "scalar"
case n: NodeShape if n.isAbstract.value() => "interface"
case n: NodeShape if n.isInputOnly.value() => "input object"
case _: NodeShape => "object"
case _: UnionShape => "union"
case _: DataNode => "value"
case _: EndPoint => "field"
case _: WebApi => "schema"
case _ => "type" // should be unreachable
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
ModelId: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql
Profile: GraphQL
Conforms: false
Number of results: 5

Level: Violation

- Constraint: http://a.ml/vocabularies/amf/parser#required-fields
Message: field 'grid' required by interface 'Chess' can't be nullable because it's definition is non-nullable
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard1
Property: http://a.ml/vocabularies/shapes#schema
Range: [(10,4)-(10,19)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#required-fields
Message: field 'grid' required by interface 'Chess' can't be nullable because it's definition is non-nullable
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard2
Property: http://a.ml/vocabularies/shapes#schema
Range: [(14,4)-(14,18)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#required-fields
Message: field 'grid' required by interface 'Chess' can't be nullable because it's definition is non-nullable
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard3
Property: http://a.ml/vocabularies/shapes#schema
Range: [(18,4)-(18,17)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-extension-argument-type
Message: Missing required argument matrix
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard4/property/property/grid/customDomainProperties/matrixDirective
Property: http://a.ml/vocabularies/document#definedBy
Range: [(22,21)-(22,83)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: Field 'matrix' must be an input type, 'a list of a list of String' it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/matrixDirective/shape/default-node
Property: http://www.w3.org/ns/shacl#property
Range: [(25,27)-(25,45)]
Location:
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type Query {
name: String
}

interface Chess {
grid: [[Int!]!]!
}

type ChessBoard1 implements Chess {
grid: [[Int!]!]
}

type ChessBoard2 implements Chess {
grid: [[Int!]]
}

type ChessBoard3 implements Chess {
grid: [[Int]]
}

type ChessBoard4 implements Chess {
grid: [[Int!]!]! @matrixDirective(matrix: [["this"],["is"], ["a"], ["string"]])
}

directive @matrixDirective(matrix: [[String]]) on FIELD_DEFINITION
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
ModelId: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql
Profile: GraphQL
Conforms: false
Number of results: 5

Level: Violation

- Constraint: http://a.ml/vocabularies/amf/parser#required-fields
Message: field 'grid' required by interface 'Chess' can't be nullable because it's definition is non-nullable
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard1
Property: http://a.ml/vocabularies/shapes#schema
Range: [(10,4)-(10,19)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#required-fields
Message: field 'grid' required by interface 'Chess' can't be nullable because it's definition is non-nullable
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard2
Property: http://a.ml/vocabularies/shapes#schema
Range: [(14,4)-(14,18)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#required-fields
Message: field 'grid' required by interface 'Chess' can't be nullable because it's definition is non-nullable
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard3
Property: http://a.ml/vocabularies/shapes#schema
Range: [(18,4)-(18,17)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-extension-argument-type
Message: Missing required argument matrix
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/shape/ChessBoard4/property/property/grid/customDomainProperties/matrixDirective
Property: http://a.ml/vocabularies/document#definedBy
Range: [(22,21)-(22,83)]
Location:

- Constraint: http://a.ml/vocabularies/amf/parser#invalid-input-type
Message: Field 'matrix' must be an input type, 'a list of a list of String' it's not
Severity: Violation
Target: file://amf-cli/shared/src/test/resources/graphql/tck/apis/invalid/missing-matrix-field.graphql#/declares/matrixDirective/shape/default-node
Property: http://www.w3.org/ns/shacl#property
Range: [(25,27)-(25,45)]
Location:
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
type Query {
name: String
}
type ChessBoard {
grid : [[Int!]!]!

type ChessBoard implements Chess {
grid: [[Int!]!]! @matrixDirective
}

interface Chess {
grid: [[Int!]!]!
}

directive @matrixDirective(matrix: [[String]]) on FIELD_DEFINITION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ type Query {
name: String
}

type ChessBoard {
interface Chess {
grid: [[Int!]!]!
}

type ChessBoard implements Chess {
grid: [[Int!]!]! @matrixDirective(matrix: [["this"],["is"], ["a"], ["string"]])
}

}
directive @matrixDirective(matrix: [[String]]) on FIELD_DEFINITION
Loading

0 comments on commit c7c2afd

Please sign in to comment.