-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): can expand into the fields of subclasses
- Loading branch information
Showing
5 changed files
with
137 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...emx2-graphql/src/test/java/org/molgenis/emx2/graphql/TestTableQueriesWithInheritance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.molgenis.emx2.graphql; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.molgenis.emx2.Column.column; | ||
import static org.molgenis.emx2.Row.row; | ||
import static org.molgenis.emx2.TableMetadata.table; | ||
import static org.molgenis.emx2.graphql.GraphqlApiFactory.convertExecutionResultToJson; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import graphql.GraphQL; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.molgenis.emx2.ColumnType; | ||
import org.molgenis.emx2.Database; | ||
import org.molgenis.emx2.MolgenisException; | ||
import org.molgenis.emx2.Schema; | ||
import org.molgenis.emx2.sql.TestDatabaseFactory; | ||
import org.molgenis.emx2.tasks.TaskService; | ||
import org.molgenis.emx2.tasks.TaskServiceInMemory; | ||
|
||
public class TestTableQueriesWithInheritance { | ||
private static final String schemaName = TestTableQueriesWithInheritance.class.getSimpleName(); | ||
private static GraphQL grapql; | ||
private static Database database; | ||
private static TaskService taskService; | ||
private static Schema schema; | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
database = TestDatabaseFactory.getTestDatabase(); | ||
schema = database.dropCreateSchema(schemaName); | ||
schema.create(table("Person", column("name").setPkey())); | ||
schema.create( | ||
table("Employee", column("salary").setType(ColumnType.INT)).setInheritName("Person")); | ||
schema.getTable("Employee").insert(row("name", "pooky", "salary", 1000)); | ||
taskService = new TaskServiceInMemory(); | ||
grapql = new GraphqlApiFactory().createGraphqlForSchema(schema, taskService); | ||
} | ||
|
||
@Test | ||
public void testQueriesIncludingSubclassColumns() throws IOException { | ||
JsonNode result = | ||
execute( | ||
"{Person{name,salary}}"); // note, Person.salary doesn't exist, but Employee.salary does | ||
assertEquals(1000, result.at("/Person/0/salary").asInt()); | ||
} | ||
|
||
private JsonNode execute(String query) throws IOException { | ||
String result = convertExecutionResultToJson(grapql.execute(query)); | ||
JsonNode node = new ObjectMapper().readTree(result); | ||
if (node.get("errors") != null) { | ||
throw new MolgenisException(node.get("errors").get(0).get("message").asText()); | ||
} | ||
return new ObjectMapper().readTree(result).get("data"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
backend/molgenis-emx2/src/test/java/org/molgenis/emx2/TestTableMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.molgenis.emx2; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.molgenis.emx2.Column.column; | ||
import static org.molgenis.emx2.TableMetadata.table; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestTableMetadata { | ||
@Test | ||
void testGetColumnsFromSubclasses() { | ||
SchemaMetadata s = | ||
new SchemaMetadata() | ||
.create( | ||
table("Person", column("name")), | ||
table("Employee", column("details").setType(ColumnType.HEADING), column("salary")) | ||
.setInheritName("Person")); | ||
|
||
List<Column> result = s.getTableMetadata("Person").getColumnsIncludingSubclasses(); | ||
assertEquals(3, result.size()); | ||
|
||
Column salary = result.get(2); | ||
assertEquals("salary", salary.getName()); | ||
assertEquals("Employee", salary.getTableName()); | ||
|
||
result = s.getTableMetadata("Person").getColumnsIncludingSubclassesExcludingHeadings(); | ||
assertEquals(2, result.size()); | ||
} | ||
} |