Skip to content

Update graphql java #1

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

Merged
merged 5 commits into from
Aug 31, 2020
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
14 changes: 7 additions & 7 deletions src/main/kotlin/org/neo4j/graphql/BuildingEnv.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class BuildingEnv(val types: MutableMap<String, GraphQLNamedType>) {
.forEach { field ->
val typeDefinition = field.type.inner()
val filterType = when {
typeDefinition.isNeo4jType() -> simplePrint(getInputType(typeDefinition))
typeDefinition.isNeo4jType() -> getInputType(typeDefinition).requiredName()
typeDefinition.isScalar() -> typeDefinition.innerName()
typeDefinition is GraphQLEnumType -> typeDefinition.innerName()
else -> addFilterType(getInnerFieldsContainer(typeDefinition), createdTypes)
Expand Down Expand Up @@ -124,7 +124,7 @@ class BuildingEnv(val types: MutableMap<String, GraphQLNamedType>) {
val orderingName = "_${type.name}Ordering"
var existingOrderingType = types[orderingName]
if (existingOrderingType != null) {
return simplePrint(existingOrderingType as? GraphQLInputType)
return (existingOrderingType as? GraphQLInputType)?.requiredName()
?: throw IllegalStateException("Ordering type $type.name is already defined but not an input type")
}
val sortingFields = type.fieldDefinitions
Expand Down Expand Up @@ -192,7 +192,7 @@ class BuildingEnv(val types: MutableMap<String, GraphQLNamedType>) {
?: throw IllegalArgumentException("${innerType.name} is unknown")
}
return innerType as? GraphQLFieldsContainer
?: throw IllegalArgumentException("${simplePrint(innerType)} is neither an object nor an interface")
?: throw IllegalArgumentException("${innerType.name()} is neither an object nor an interface")
}

private fun getInputType(type: GraphQLType): GraphQLInputType {
Expand All @@ -202,12 +202,12 @@ class BuildingEnv(val types: MutableMap<String, GraphQLNamedType>) {
}
if (inner.isNeo4jType()) {
return neo4jTypeDefinitions
.find { it.typeDefinition == simplePrint(inner) }
.find { it.typeDefinition == inner.name() }
?.let { types[it.inputDefinition] } as? GraphQLInputType
?: throw IllegalArgumentException("Cannot find input type for ${simplePrint(inner)}")
?: throw IllegalArgumentException("Cannot find input type for ${inner.name()}")
}
return type as? GraphQLInputType
?: throw IllegalArgumentException("${simplePrint(type)} is not allowed for input")
?: throw IllegalArgumentException("${type.name()} is not allowed for input")
}

}
}
12 changes: 7 additions & 5 deletions src/main/kotlin/org/neo4j/graphql/GraphQLExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import org.neo4j.graphql.DirectiveConstants.Companion.RELATION_NAME
import org.neo4j.graphql.DirectiveConstants.Companion.RELATION_TO
import org.neo4j.graphql.handler.projection.ProjectionBase

import graphql.schema.GraphQLTypeUtil.simplePrint

fun Type<Type<*>>.name(): String? = if (this.inner() is TypeName) (this.inner() as TypeName).name else null
fun Type<Type<*>>.inner(): Type<Type<*>> = when (this) {
is ListType -> this.type.inner()
Expand All @@ -35,8 +33,11 @@ fun GraphQLType.inner(): GraphQLType = when (this) {
else -> this
}

fun GraphQLType.name(): String? = (this as? GraphQLNamedType)?.name
fun GraphQLType.requiredName(): String = requireNotNull(name()) { -> "name is required but cannot be determined for " + this.javaClass }

fun GraphQLType.isList() = this is GraphQLList || (this is GraphQLNonNull && this.wrappedType is GraphQLList)
fun GraphQLType.isScalar() = this.inner().let { it is GraphQLScalarType || simplePrint(it).startsWith("_Neo4j") }
fun GraphQLType.isScalar() = this.inner().let { it is GraphQLScalarType || it.innerName().startsWith("_Neo4j") }
fun GraphQLType.isNeo4jType() = this.innerName().startsWith("_Neo4j")
fun GraphQLType.isNeo4jSpatialType() = this.innerName().startsWith("_Neo4jPoint")
fun GraphQLFieldDefinition.isNeo4jType(): Boolean = this.type.isNeo4jType()
Expand Down Expand Up @@ -121,7 +122,7 @@ fun GraphQLType.ref(): GraphQLType = when (this) {
is GraphQLScalarType -> this
is GraphQLEnumType -> this
is GraphQLTypeReference -> this
else -> GraphQLTypeReference(simplePrint(this))
else -> GraphQLTypeReference(name())
}

fun relDetails(type: GraphQLFieldsContainer, relDirective: GraphQLDirective): RelationshipInfo {
Expand Down Expand Up @@ -185,7 +186,8 @@ data class RelationshipInfo(
}

fun Field.aliasOrName() = (this.alias ?: this.name).quote()
fun GraphQLType.innerName(): String = simplePrint(inner())
fun GraphQLType.innerName(): String = inner().name()
?: throw IllegalStateException("inner name cannot be retrieved for " + this.javaClass)

fun GraphQLFieldDefinition.propertyName() = getDirectiveArgument(PROPERTY, PROPERTY_NAME, this.name)!!

Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/org/neo4j/graphql/Neo4jTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import graphql.language.ObjectValue
import graphql.schema.GraphQLFieldDefinition
import graphql.schema.GraphQLFieldsContainer

import graphql.schema.GraphQLTypeUtil.simplePrint

const val NEO4j_FORMATTED_PROPERTY_KEY = "formatted"
const val NEO4j_POINT_DISTANCE_FILTER = "_Neo4jPointDistanceFilter"
const val NEO4j_POINT_DISTANCE_FILTER_SUFFIX = "_distance"
Expand Down Expand Up @@ -60,7 +58,7 @@ data class Neo4jQueryConversion(val name: String, val propertyName: String, val
if (!isNeo4jType) {
Neo4jQueryConversion(name, name)
}
val converter = getNeo4jTypeConverter(simplePrint(fieldDefinition.type.inner()))
val converter = getNeo4jTypeConverter(fieldDefinition.type.innerName())
val objectValue = (value as? ObjectValue)
?.objectFields
?.map { it.name to it.value }
Expand All @@ -82,4 +80,4 @@ val neo4jTypeDefinitions = listOf(
TypeDefinition("Time", "_Neo4jLocalTime"),
TypeDefinition("LocalDateTime", "_Neo4jLocalDateTime"),
TypeDefinition("Point", "_Neo4jPoint")
)
)
9 changes: 3 additions & 6 deletions src/main/kotlin/org/neo4j/graphql/Predicates.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import org.neo4j.graphql.Predicate.Companion.resolvePredicate
import org.neo4j.graphql.handler.projection.ProjectionBase
import org.slf4j.LoggerFactory

import graphql.schema.GraphQLTypeUtil.simplePrint

typealias CypherDSL = org.neo4j.cypherdsl.core.Cypher

interface Predicate {
Expand Down Expand Up @@ -114,7 +112,7 @@ data class ExpressionPredicate(
) : Predicate {
val not = if (op.not) "NOT " else ""
override fun toExpression(variable: String): Cypher {
val paramName: String = ProjectionBase.FILTER + paramName(variable, name, value).capitalize() + "_" + op.name + nestedField.replace('.','_')
val paramName: String = ProjectionBase.FILTER + paramName(variable, name, value).capitalize() + "_" + op.name + nestedField.replace('.', '_')
val query = if (fieldDefinition.isNativeId()) {
if (op.list) {
"${not}ID($variable) ${op.op} [id IN \$$paramName | toInteger(id)]"
Expand Down Expand Up @@ -256,7 +254,7 @@ enum class FieldOperator(
fun resolve(queriedField: String, field: GraphQLFieldDefinition, value: Any?): FieldOperator? {
val fieldName = field.name
if (value == null) {
return listOf(IS_NULL, IS_NOT_NULL).find { queriedField == fieldName + it.suffix } ?: return null
return listOf(IS_NULL, IS_NOT_NULL).find { queriedField == fieldName + it.suffix }
}
val ops = enumValues<FieldOperator>().filterNot { it == IS_NULL || it == IS_NOT_NULL }
return ops.find { queriedField == fieldName + it.suffix }
Expand All @@ -265,7 +263,6 @@ enum class FieldOperator(
} else {
null
}
?: return null
}

fun forType(type: GraphQLType): List<FieldOperator> =
Expand All @@ -279,7 +276,7 @@ enum class FieldOperator(
// todo list types
!type.isScalar() -> listOf(EQ, NEQ, IN, NIN)
else -> listOf(EQ, NEQ, IN, NIN, LT, LTE, GT, GTE) +
if (simplePrint(type) == "String" || simplePrint(type) == "ID") listOf(C, NC, SW, NSW, EW, NEW) else emptyList()
if (type.name() == "String" || type.name() == "ID") listOf(C, NC, SW, NSW, EW, NEW) else emptyList()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,8 @@ class MergeOrUpdateHandler private constructor(
}

init {
defaultFields.clear() // for marge or updates we do not reset to defaults
if (idField.isNativeId() || merge) {
// native id cannot be updated
// if the ID is not a native ID and we are in the update mode, we do not remove it from the properties
// b/c otherwise the id field will be unset
propertyFields.remove(idField.name)
}
defaultFields.clear() // for merge or updates we do not reset to defaults
propertyFields.remove(idField.name) // id should not be updated
}

override fun generateCypher(variable: String, field: Field, env: DataFetchingEnvironment): Cypher {
Expand All @@ -82,10 +77,9 @@ class MergeOrUpdateHandler private constructor(
val properties = properties(variable, field.arguments)
val mapProjection = projectFields(variable, field, type, env, null)

val op = if (merge) "+" else ""
val select = getSelectQuery(variable, label(), idArg, idField, isRelation)
return Cypher((if (merge && !idField.isNativeId()) "MERGE " else "MATCH ") + select.query +
" SET $variable $op= " + properties.query +
" SET $variable += " + properties.query +
" WITH $variable" +
" RETURN ${mapProjection.query} AS $variable",
select.params + properties.params + mapProjection.params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import graphql.language.*
import graphql.schema.*
import org.neo4j.graphql.*

import graphql.schema.GraphQLTypeUtil.simplePrint

open class ProjectionBase {
companion object {
const val NATIVE_ID = "_id"
Expand Down Expand Up @@ -297,8 +295,10 @@ open class ProjectionBase {
private fun relationshipInfoInCorrectDirection(fieldObjectType: GraphQLFieldsContainer, relInfo0: RelationshipInfo, parent: GraphQLFieldsContainer, relDirectiveField: RelationshipInfo?): RelationshipInfo {
val startField = fieldObjectType.getFieldDefinition(relInfo0.startField)!!
val endField = fieldObjectType.getFieldDefinition(relInfo0.endField)!!
val startFieldTypeName = simplePrint(startField.type.inner())
val inverse = startFieldTypeName != parent.name || simplePrint(startField.type) == simplePrint(endField.type) && relDirectiveField?.out != relInfo0.out
val startFieldTypeName = startField.type.innerName()
val inverse = startFieldTypeName != parent.name
|| startFieldTypeName == endField.type.innerName()
&& relDirectiveField?.out != relInfo0.out
return if (inverse) relInfo0.copy(out = relInfo0.out?.not(), startField = relInfo0.endField, endField = relInfo0.startField) else relInfo0
}

Expand Down Expand Up @@ -329,7 +329,7 @@ open class ProjectionBase {

val (endNodePattern, variableSuffix) = when {
isRelFromType -> {
val label = simplePrint(nodeType.getFieldDefinition(relInfo.endField!!)!!.type.inner())
val label = nodeType.getFieldDefinition(relInfo.endField!!)!!.type.innerName()
("$childVariable${relInfo.endField.capitalize()}:$label" to relInfo.endField)
}
else -> ("$childVariable:${nodeType.name}" to null)
Expand Down
36 changes: 0 additions & 36 deletions src/test/kotlin/org/neo4j/graphql/TranslatorExceptionTest.kt

This file was deleted.

62 changes: 35 additions & 27 deletions src/test/kotlin/org/neo4j/graphql/TranslatorExceptionTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,43 @@ package org.neo4j.graphql

import graphql.parser.InvalidSyntaxException
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.neo4j.graphql.utils.CypherTestSuite
import org.junit.jupiter.api.DynamicNode
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory
import org.neo4j.graphql.utils.AsciiDocTestSuite
import java.util.stream.Stream

class TranslatorExceptionTests {
class TranslatorExceptionTests : AsciiDocTestSuite("translator-tests1.adoc") {

private val testSuite = CypherTestSuite("translator-tests1.adoc")

@Test
fun unknownType() {
// todo better test
Assertions.assertThrows(IllegalArgumentException::class.java) {
testSuite.translate("""
{
company {
name
}
}
""".trimIndent())
}
@TestFactory
fun createTests(): Stream<DynamicNode> {
return parse(linkedSetOf())
}

@Test
fun mutation() {
Assertions.assertThrows(InvalidSyntaxException::class.java) {
testSuite.translate("""
{
createPerson()
}
""".trimIndent())
}
override fun schemaTestFactory(schema: String): List<DynamicNode> {
val translator = Translator(SchemaBuilder.buildSchema(schema))
return listOf(
DynamicTest.dynamicTest("unknownType") {
Assertions.assertThrows(IllegalArgumentException::class.java) {
translator.translate("""
{
company {
name
}
}
""")
}
},
DynamicTest.dynamicTest("mutation") {
Assertions.assertThrows(InvalidSyntaxException::class.java) {
translator.translate("""
{
createPerson()
}
""".trimIndent())
}
}

)
}
}
}
Loading