Search in sources :

Example 1 with GraphqlApiFactory

use of org.molgenis.emx2.graphql.GraphqlApiFactory in project molgenis-emx2 by molgenis.

the class MolgenisSession method getGraphqlForSchema.

public GraphQL getGraphqlForSchema(String schemaName) {
    logger.info("getting graphql schema '{}' for user '{}'", schemaName, getSessionUser());
    if (graphqlPerSchema.get(schemaName) == null) {
        Schema schema = database.getSchema(schemaName);
        if (schema == null)
            throw new MolgenisException("Schema not found: Schema with name '" + schemaName + "' does not exist or permission denied");
        graphqlPerSchema.put(schemaName, new GraphqlApiFactory().createGraphqlForSchema(schema, TaskApi.taskService));
        logger.info("created graphql schema '{}' for user '{}'", schemaName, getSessionUser());
    }
    logger.info("return graphql schema '{}' for user '{}'", schemaName, getSessionUser());
    return graphqlPerSchema.get(schemaName);
}
Also used : Schema(org.molgenis.emx2.Schema) MolgenisException(org.molgenis.emx2.MolgenisException) GraphqlApiFactory(org.molgenis.emx2.graphql.GraphqlApiFactory)

Example 2 with GraphqlApiFactory

use of org.molgenis.emx2.graphql.GraphqlApiFactory in project molgenis-emx2 by molgenis.

the class TestGraphQLCompositeKeys method setup.

// need ref
// need mref
// need ref_array
@BeforeClass
public static void setup() {
    database = TestDatabaseFactory.getTestDatabase();
    Schema schema = database.dropCreateSchema(schemaName);
    grapql = new GraphqlApiFactory().createGraphqlForSchema(schema);
}
Also used : Schema(org.molgenis.emx2.Schema) BeforeClass(org.junit.BeforeClass)

Example 3 with GraphqlApiFactory

use of org.molgenis.emx2.graphql.GraphqlApiFactory in project molgenis-emx2 by molgenis.

the class TestGraphQLCompositeKeys method execute.

// @Test
// public void testMref() throws IOException {
// // create schema (best edit this in graphql editor)
// execute(
// "mutation {\n"
// + "  change(\n"
// + "    tables: [\n"
// + "      { name: \"TargetTable2\", columns: [{ name: \"firstName\", key: 1 },{ name:
// \"lastName\", key: 1 }] }\n"
// + "      {\n"
// + "        name: \"RefTable2\"\n"
// + "        columns: [\n"
// + "          { name: \"id\", key: 1 }\n"
// + "          { name: \"ref\", columnType: \"MREF\", refTable: \"TargetTable2\"}\n"
// + "        ]\n"
// + "      }\n"
// + "    ]\n"
// + "  ) {\n"
// + "    message\n"
// + "  }\n"
// + "}");
// 
// // have to reload graphql
// grapql =
// new GraphqlApiFactory()
// .createGraphqlForSchema(
// database.getSchema(TestGraphQLCompositeKeys.class.getSimpleName()));
// 
// // insert some data, enough to check if foreign keys are joined correctly
// execute(
// "mutation{insert(TargetTable2:["
// + "\n{firstName:\"Donald\",lastName:\"Duck\"}"
// + "\n{firstName:\"Katrien\",lastName:\"Duck\"}"
// + "\n{firstName:\"Katrien\",lastName:\"Mouse\"}"
// + "\n{firstName:\"Donald\",lastName:\"Mouse\"}"
// + "\n], RefTable2:["
// + "\n{id:\"1\",
// ref:[{firstName:\"Katrien\",lastName:\"Mouse\"},{firstName:\"Donald\",lastName:\"Duck\"}]}"
// + "\n{id:\"2\",
// ref:[{firstName:\"Katrien\",lastName:\"Duck\"},{firstName:\"Donald\",lastName:\"Mouse\"}]}"
// + "\n{id:\"3\", ref:[{firstName:\"Katrien\",lastName:\"Duck\"}]}"
// + "]){message}}");
// 
// // test query
// JsonNode result = execute("{RefTable2{id,ref{firstName,lastName}}}");
// System.out.println(result.toPrettyString());
// 
// assertEquals(2, result.at("/RefTable2/0/ref").size());
// // order is determined by TargetTable unfortunately
// assertEquals("Katrien", result.at("/RefTable2/0/ref/1/firstName").asText());
// assertEquals("Mouse", result.at("/RefTable2/0/ref/1/lastName").asText());
// assertEquals("Donald", result.at("/RefTable2/0/ref/0/firstName").asText());
// assertEquals("Duck", result.at("/RefTable2/0/ref/0/lastName").asText());
// }
private static 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());
    } else {
        if (node.get("data") != null && node.get("data").get("message") != null) {
            System.out.println("MUTATION MESSAGE: " + node.get("data").get("message").asText());
        }
    }
    return new ObjectMapper().readTree(result).get("data");
}
Also used : MolgenisException(org.molgenis.emx2.MolgenisException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with GraphqlApiFactory

use of org.molgenis.emx2.graphql.GraphqlApiFactory in project molgenis-emx2 by molgenis.

the class TestGraphqSchemaFields method setup.

@BeforeClass
public static void setup() {
    database = TestDatabaseFactory.getTestDatabase();
    schema = database.dropCreateSchema(schemaName);
    new PetStoreLoader().load(schema, true);
    taskService = new TaskServiceInMemory();
    grapql = new GraphqlApiFactory().createGraphqlForSchema(schema, taskService);
}
Also used : TaskServiceInMemory(org.molgenis.emx2.tasks.TaskServiceInMemory) PetStoreLoader(org.molgenis.emx2.datamodels.PetStoreLoader) BeforeClass(org.junit.BeforeClass)

Example 5 with GraphqlApiFactory

use of org.molgenis.emx2.graphql.GraphqlApiFactory in project molgenis-emx2 by molgenis.

the class TestGraphqlAdminFields method testUsers.

@Test
public void testUsers() {
    // put in transaction so user count is not affected by other operations
    database.tx(tdb -> {
        tdb.becomeAdmin();
        Schema schema = tdb.dropCreateSchema(schemaName);
        grapql = new GraphqlApiFactory().createGraphqlForSchema(schema);
        try {
            JsonNode result = execute("{_admin{users{username} userCount}}");
            TestCase.assertTrue(result.at("/_admin/userCount").intValue() > 0);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // test that only admin can do this
        tdb.setActiveUser(ANONYMOUS);
        grapql = new GraphqlApiFactory().createGraphqlForSchema(schema);
        try {
            TestCase.assertEquals(null, execute("{_admin{userCount}}").textValue());
        } catch (Exception e) {
            TestCase.assertTrue(e.getMessage().contains("FieldUndefined"));
        }
        tdb.becomeAdmin();
    });
}
Also used : Schema(org.molgenis.emx2.Schema) JsonNode(com.fasterxml.jackson.databind.JsonNode) MolgenisException(org.molgenis.emx2.MolgenisException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

BeforeClass (org.junit.BeforeClass)3 MolgenisException (org.molgenis.emx2.MolgenisException)3 Schema (org.molgenis.emx2.Schema)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 Test (org.junit.Test)1 Database (org.molgenis.emx2.Database)1 PetStoreLoader (org.molgenis.emx2.datamodels.PetStoreLoader)1 GraphqlApiFactory (org.molgenis.emx2.graphql.GraphqlApiFactory)1 TaskServiceInMemory (org.molgenis.emx2.tasks.TaskServiceInMemory)1