Skip to content

Commit

Permalink
Fixed #37 not-available interfaces for multi-labels, ordering-enum-is…
Browse files Browse the repository at this point in the history
…sue Fixed #62
  • Loading branch information
jexp committed Sep 3, 2017
1 parent 371c10d commit d91eb7d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/main/kotlin/org/neo4j/graphql/GraphQLSchemaBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection<MetaData>) {
// .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.

Expand Down Expand Up @@ -205,6 +205,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection<MetaData>) {
): 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)
/*
Expand All @@ -218,7 +219,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection<MetaData>) {
.description(md.type + " " + name + " " + label)
.argument(propertiesAsArguments(labelMd))
.argument(propertiesAsListArguments(labelMd))
.argument(orderByArgument(labelMd))
.argumentIf(hasProperties, {orderByArgument(labelMd)})
.argument(toArguments(parameters))
.type(graphQLType)

Expand All @@ -227,6 +228,9 @@ class GraphQLSchemaBuilder(val metaDatas: Collection<MetaData>) {
} else field.build()
}

fun GraphQLFieldDefinition.Builder.argumentIf(pred: Boolean, arg: () -> GraphQLArgument) =
if (pred) this.argument(arg.invoke()) else this

private fun toArguments(parameters: Iterable<MetaData.ParameterInfo>?): List<GraphQLArgument> {
return parameters?.map { newArgument().name(it.name).type(graphQlInType(it.type, false)).defaultValue(it.defaultValue).build() } ?: emptyList()
}
Expand Down Expand Up @@ -303,7 +307,7 @@ class GraphQLSchemaBuilder(val metaDatas: Collection<MetaData>) {
val enums: MutableMap<String, GraphQLEnumType> = enumsFromDefinitions(definitions).toMutableMap()
val inputTypes: Map<String, GraphQLInputObjectType> = inputTypesFromDefinitions(definitions, enums)

private fun buildSchema() : GraphQLSchema {
fun buildSchema() : GraphQLSchema {

val dictionary = graphQlTypes(metaDatas)

Expand Down Expand Up @@ -390,13 +394,14 @@ class GraphQLSchemaBuilder(val metaDatas: Collection<MetaData>) {
fun queryFields(metaDatas: Iterable<MetaData>): List<GraphQLFieldDefinition> {
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()
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/neo4j/graphql/GraphQLSchemaBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/org/neo4j/graphql/MetaDataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -69,6 +69,11 @@ public void allUsersQuery() throws Exception {
Map<String, List<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<String, List<Map>> result = executeQuery("query { Country { _id } }", map());
assertEquals(1, result.get("Country").size());
}

@Test
public void firstUserQuery() throws Exception {
Expand Down

0 comments on commit d91eb7d

Please sign in to comment.