Search in sources :

Example 6 with MergeSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas in project timbuctoo by HuygensING.

the class MergeSchemasTest method mergeSchemaReturnsMergedSchemaWithAllPredicatesForSingleType.

@Test
public void mergeSchemaReturnsMergedSchemaWithAllPredicatesForSingleType() throws Exception {
    MergeSchemas mergeSchemas = new MergeSchemas();
    Map<String, Type> generatedSchema = new HashMap<>();
    generatedSchema.put("Type", createTypeWithPredicate("generated", Direction.OUT));
    Map<String, Type> customSchema = new HashMap<>();
    customSchema.put("Type", createTypeWithPredicate("custom", Direction.IN));
    Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema, customSchema);
    assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", containsInAnyOrder(predicateMatcher().withName("generated").withDirection(Direction.OUT), predicateMatcher().withName("custom").withDirection(Direction.IN)))));
}
Also used : MergeSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas) Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with MergeSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas in project timbuctoo by HuygensING.

the class ExtendSchemaMutation method get.

@Override
public Object get(DataFetchingEnvironment env) {
    DataSet dataSet = MutationHelpers.getDataSet(env, dataSetRepository::getDataSet);
    MutationHelpers.checkAdminPermissions(env, dataSet.getMetadata());
    final SchemaStore generatedSchema = dataSet.getSchemaStore();
    Map<String, Type> customTypes = new HashMap<>();
    List<ExplicitType> customSchema;
    Map<String, List<ExplicitField>> newCustomSchema = new HashMap<>();
    try {
        String customSchemaString = OBJECT_MAPPER.writeValueAsString(env.getArgument("customSchema"));
        customSchema = OBJECT_MAPPER.readValue(customSchemaString, new TypeReference<List<ExplicitType>>() {
        });
    } catch (IOException e) {
        throw new RuntimeException("Could not parse the schema input");
    }
    for (ExplicitType explicitType : customSchema) {
        customTypes.put(explicitType.getCollectionId(), explicitType.convertToType());
        newCustomSchema.put(explicitType.getCollectionId(), explicitType.getFields());
    }
    Map<String, List<ExplicitField>> existingCustomSchema = dataSet.getCustomSchema();
    Map<String, Type> existingCustomSchemaTypes = new HashMap<>();
    for (Map.Entry<String, List<ExplicitField>> entry : existingCustomSchema.entrySet()) {
        ExplicitType tempExplicitType = new ExplicitType(entry.getKey(), entry.getValue());
        existingCustomSchemaTypes.put(entry.getKey(), tempExplicitType.convertToType());
    }
    MergeSchemas mergeSchemas = new MergeSchemas();
    customTypes = mergeSchemas.mergeSchema(existingCustomSchemaTypes, customTypes);
    MergeExplicitSchemas mergeExplicitSchemas = new MergeExplicitSchemas();
    Map<String, List<ExplicitField>> mergedExplicitSchema = mergeExplicitSchemas.mergeExplicitSchemas(existingCustomSchema, newCustomSchema);
    try {
        dataSet.saveCustomSchema(mergedExplicitSchema);
    } catch (IOException e) {
        LOG.error("Saving the custom schema failed", e);
        throw new RuntimeException(e);
    }
    Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema.getStableTypes(), customTypes);
    for (Map.Entry<String, Type> customType : customTypes.entrySet()) {
        if (!mergedSchema.containsKey(customType.getKey())) {
            return ImmutableMap.of("message", "Schema extension was unsuccessful.");
        }
    }
    return ImmutableMap.of("message", "Schema extended successfully.");
}
Also used : MergeSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas) DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) SchemaStore(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.SchemaStore) HashMap(java.util.HashMap) MergeExplicitSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeExplicitSchemas) IOException(java.io.IOException) ExplicitType(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType) Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ImmutableMap(jersey.repackaged.com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) ExplicitType(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType)

Example 8 with MergeSchemas

use of nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas in project timbuctoo by HuygensING.

the class MergeSchemasTest method mergeSchemaMergesMatchingPredicates.

@Test
public void mergeSchemaMergesMatchingPredicates() throws Exception {
    final MergeSchemas mergeSchemas = new MergeSchemas();
    Map<String, Type> generatedSchema = new HashMap<>();
    Type predType1 = createTypeWithPredicate("generated", Direction.IN);
    predType1.getPredicate("generated", Direction.IN).setHasBeenList(true);
    predType1.getPredicate("generated", Direction.IN).setOwner(new Type("testOwner"));
    generatedSchema.put("Type", predType1);
    Map<String, Type> customSchema = new HashMap<>();
    Type predType2 = createTypeWithPredicate("generated", Direction.IN);
    predType2.getPredicate("generated", Direction.IN).setHasBeenList(false);
    predType2.getPredicate("generated", Direction.IN).setOwner(new Type("testOwner"));
    customSchema.put("Type", predType2);
    Map<String, Type> mergedSchema = mergeSchemas.mergeSchema(generatedSchema, customSchema);
    assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", contains(predicateMatcher().withName("generated").withDirection(Direction.IN).withWasList(true)))));
    assertThat(mergedSchema, hasEntry(is("Type"), hasProperty("predicates", not(hasItem(predicateMatcher().withName("generated").withDirection(Direction.IN).withWasList(false))))));
}
Also used : MergeSchemas(nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas) Type(nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

HashMap (java.util.HashMap)8 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)8 MergeSchemas (nl.knaw.huygens.timbuctoo.v5.graphql.customschema.MergeSchemas)8 Test (org.junit.Test)6 IOException (java.io.IOException)2 List (java.util.List)2 Map (java.util.Map)2 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)2 ExplicitType (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.ExplicitType)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Charsets (com.google.common.base.Charsets)1 Resources (com.google.common.io.Resources)1 Resources.getResource (com.google.common.io.Resources.getResource)1 GraphQLSchema (graphql.schema.GraphQLSchema)1 RuntimeWiring (graphql.schema.idl.RuntimeWiring)1 SchemaGenerator (graphql.schema.idl.SchemaGenerator)1 SchemaParser (graphql.schema.idl.SchemaParser)1 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)1 ArrayList (java.util.ArrayList)1