diff --git a/src/main/kotlin/org/neo4j/graphql/GraphQLSchemaBuilder.kt b/src/main/kotlin/org/neo4j/graphql/GraphQLSchemaBuilder.kt index bb59f3d..0793c60 100644 --- a/src/main/kotlin/org/neo4j/graphql/GraphQLSchemaBuilder.kt +++ b/src/main/kotlin/org/neo4j/graphql/GraphQLSchemaBuilder.kt @@ -109,7 +109,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection) { // .fetchField().dataFetcher((env) -> null) .type(Scalars.GraphQLID).build()) - metaData.labels.forEach { builder = builder.withInterface(interfaceDefinitions.get(it)) } + metaData.labels.mapNotNull { interfaceDefinitions.get(it) }.forEach { builder = builder.withInterface(it) } // todo relationships, labels etc. @@ -205,6 +205,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection) { ): GraphQLFieldDefinition { val labelMd = GraphSchemaScanner.getMetaData(label)!! val graphQLType: GraphQLOutputType = if (multi) GraphQLList(GraphQLTypeReference(label)) else GraphQLTypeReference(label) + val hasProperties = labelMd.properties.isNotEmpty() val field = newFieldDefinition() .name(name) /* @@ -218,7 +219,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection) { .description(md.type + " " + name + " " + label) .argument(propertiesAsArguments(labelMd)) .argument(propertiesAsListArguments(labelMd)) - .argument(orderByArgument(labelMd)) + .argumentIf(hasProperties, {orderByArgument(labelMd)}) .argument(toArguments(parameters)) .type(graphQLType) @@ -227,6 +228,9 @@ class GraphQLSchemaBuilder(val metaDatas: Collection) { } else field.build() } + fun GraphQLFieldDefinition.Builder.argumentIf(pred: Boolean, arg: () -> GraphQLArgument) = + if (pred) this.argument(arg.invoke()) else this + private fun toArguments(parameters: Iterable?): List { return parameters?.map { newArgument().name(it.name).type(graphQlInType(it.type, false)).defaultValue(it.defaultValue).build() } ?: emptyList() } @@ -303,7 +307,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection) { val enums: MutableMap = enumsFromDefinitions(definitions).toMutableMap() val inputTypes: Map = inputTypesFromDefinitions(definitions, enums) - private fun buildSchema() : GraphQLSchema { + fun buildSchema() : GraphQLSchema { val dictionary = graphQlTypes(metaDatas) @@ -390,13 +394,14 @@ class GraphQLSchemaBuilder(val metaDatas: Collection) { fun queryFields(metaDatas: Iterable): List { return metaDatas .map { md -> + val hasProperties = md.properties.isNotEmpty() withFirstOffset( newFieldDefinition() .name(md.type) .type(GraphQLList(GraphQLTypeReference(md.type))) // todo .argument(propertiesAsArguments(md)) .argument(propertiesAsListArguments(md)) - .argument(orderByArgument(md)) + .argumentIf(hasProperties,{orderByArgument(md)}) .dataFetcher({ env -> fetchGraphData(md, env) }) ).build() } diff --git a/src/test/java/org/neo4j/graphql/GraphQLSchemaBuilderTest.kt b/src/test/java/org/neo4j/graphql/GraphQLSchemaBuilderTest.kt index 857b6a2..8ddae86 100644 --- a/src/test/java/org/neo4j/graphql/GraphQLSchemaBuilderTest.kt +++ b/src/test/java/org/neo4j/graphql/GraphQLSchemaBuilderTest.kt @@ -7,8 +7,26 @@ import graphql.parser.Parser import graphql.schema.* import org.junit.Assert.assertEquals import org.junit.Test +import kotlin.test.assertNull class GraphQLSchemaBuilderTest { + @Test + fun emptyNode() { + val md = MetaData("Actor") + val schema = GraphQLSchemaBuilder(listOf(md)) + val type: GraphQLObjectType = schema.toGraphQLObjectType(md) + assertEquals("Actor", type.name) + assertEquals(listOf("_id"), type.fieldDefinitions.map { it.name }) + + val queryType = schema.queryFields(listOf(md)).first() + assertEquals("Actor", queryType.name) + assertEquals(setOf("first","offset"), queryType.arguments.map { it.name }.toSet()) + + val graphQLSchema = schema.buildSchema() + val ordering = graphQLSchema.getType("_ActorOrdering") as GraphQLEnumType? + assertNull(ordering) + } + @Test fun mutationField() { val md = MetaData("Actor") diff --git a/src/test/java/org/neo4j/graphql/MetaDataTest.java b/src/test/java/org/neo4j/graphql/MetaDataTest.java index 81b462b..5a206a4 100644 --- a/src/test/java/org/neo4j/graphql/MetaDataTest.java +++ b/src/test/java/org/neo4j/graphql/MetaDataTest.java @@ -41,7 +41,7 @@ public class MetaDataTest { public void setUp() throws Exception { db = new TestGraphDatabaseFactory().newImpermanentDatabase(); ((GraphDatabaseAPI)db).getDependencyResolver().resolveDependency(Procedures.class).registerFunction(GraphQLProcedure.class); - db.execute("CREATE (berlin:Location {name:'Berlin',longitude:13.4, latitude: 52.5, coord:[13.4,52.5]}) WITH berlin UNWIND range(1,5) as id CREATE (:User {name:'John '+id, id:id, age:id})-[:LIVES_IN]->(berlin)").close(); + db.execute("CREATE (:Country),(berlin:Location {name:'Berlin',longitude:13.4, latitude: 52.5, coord:[13.4,52.5]}) WITH berlin UNWIND range(1,5) as id CREATE (:User {name:'John '+id, id:id, age:id})-[:LIVES_IN]->(berlin)").close(); GraphQLSchema graphQLSchema = GraphQLSchemaBuilder.buildSchema(db); graphql = new GraphQL(graphQLSchema); } @@ -69,6 +69,11 @@ public void allUsersQuery() throws Exception { Map> result = executeQuery("query UserQuery { User {id,name,age} User {age,name}}", map()); assertEquals(2*5, result.get("User").size()); } + @Test + public void countryQuery() throws Exception { + Map> result = executeQuery("query { Country { _id } }", map()); + assertEquals(1, result.get("Country").size()); + } @Test public void firstUserQuery() throws Exception {