Search in sources :

Example 6 with SubjectReference

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference in project timbuctoo by HuygensING.

the class SummaryPropDataRetrieverTest method subjectWithUri.

private SubjectReference subjectWithUri(String uri) {
    SubjectReference source = mock(SubjectReference.class);
    given(source.getSubjectUri()).willReturn(uri);
    return source;
}
Also used : SubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference)

Example 7 with SubjectReference

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference in project timbuctoo by HuygensING.

the class EntityImageFetcher method get.

@Override
public TypedValue get(DataFetchingEnvironment env) {
    if (env.getSource() instanceof SubjectReference) {
        SubjectReference source = env.getSource();
        DataSet dataSet = source.getDataSet();
        Optional<TypedValue> summaryProperty = summaryPropDataRetriever.createSummaryProperty(source, dataSet);
        if (summaryProperty.isPresent()) {
            return summaryProperty.get();
        }
    }
    return null;
}
Also used : DataSet(nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet) SubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference) TypedValue(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue)

Example 8 with SubjectReference

use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference in project timbuctoo by HuygensING.

the class SerializerExecutionStrategy method execute.

@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
    Map<String, java.util.List<Field>> fields = parameters.fields();
    GraphQLObjectType parentType = parameters.typeInfo().castType(GraphQLObjectType.class);
    return super.execute(executionContext, parameters).thenApply(sourceResult -> {
        Map<String, Object> data = sourceResult.getData();
        if (parameters.source() instanceof TypedValue) {
            String value = ((TypedValue) parameters.source()).getValue();
            String typename = ((TypedValue) parameters.source()).getType();
            Value result;
            if (value == null) {
                result = null;
            } else if (data.containsKey("__typename")) {
                result = Value.create(value, typename, (String) data.get("__typename"));
            } else {
                result = Value.create(value, typename);
            }
            return new ExecutionResultImpl(result, sourceResult.getErrors(), sourceResult.getExtensions());
        } else if (parameters.source() instanceof SubjectReference) {
            final String uri = ((SubjectReference) parameters.source()).getSubjectUri();
            final Set<String> types = ((SubjectReference) parameters.source()).getTypes();
            final String graphqlType = getDirectiveArgument(parentType, "rdfType", "uri").orElse(null);
            String type;
            if (graphqlType != null && types.contains(graphqlType)) {
                type = graphqlType;
            } else {
                Optional<String> firstType = types.stream().sorted().findFirst();
                if (firstType.isPresent()) {
                    type = firstType.get();
                } else {
                    LOG.error("No type present on " + uri + ". Expected at least TIM_UNKNOWN");
                    type = RdfConstants.UNKNOWN;
                }
            }
            LinkedHashMap<PredicateInfo, Serializable> copy = new LinkedHashMap<>();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                final String graphqlFieldName = entry.getKey();
                final GraphQLFieldDefinition fieldDesc = parentType.getFieldDefinition(entry.getKey());
                Optional<String> predicateUri = getDirectiveArgument(fieldDesc, "rdf", "predicate");
                Optional<Direction> direction = getDirectiveArgument(fieldDesc, "rdf", "direction").map(Direction::valueOf);
                final PredicateInfo predicateInfo;
                predicateInfo = predicateUri.map(predUri -> PredicateInfo.predicateInfo(graphqlFieldName, predUri, direction.orElse(Direction.OUT))).orElseGet(() -> PredicateInfo.predicateInfo(graphqlFieldName, null, Direction.OUT));
                if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
                    copy.put(predicateInfo, (Serializable) entry.getValue());
                } else {
                    copy.put(predicateInfo, Value.fromRawJavaType(entry.getValue()));
                }
            }
            return new ExecutionResultImpl(Entity.entity(uri, type, copy), sourceResult.getErrors(), sourceResult.getExtensions());
        } else if (parameters.source() instanceof PaginatedList) {
            PaginatedList<? extends DatabaseResult> source = (PaginatedList) parameters.source();
            return new ExecutionResultImpl(serializableList(source.getPrevCursor().orElse(null), source.getNextCursor().orElse(null), ((GraphqlIntrospectionList) data.get("items")).getItems()), sourceResult.getErrors(), sourceResult.getExtensions());
        } else if (executionContext.getGraphQLSchema().getQueryType() == parentType) {
            return new ExecutionResultImpl(QueryContainer.queryContainer(sourceResult.getData()), sourceResult.getErrors(), sourceResult.getExtensions());
        } else {
            LinkedHashMap<String, Serializable> copy = new LinkedHashMap<>();
            for (Map.Entry<String, Object> entry : data.entrySet()) {
                if (entry.getValue() == null || entry.getValue() instanceof Serializable) {
                    copy.put(entry.getKey(), (Serializable) entry.getValue());
                } else {
                    copy.put(entry.getKey(), GraphqlIntrospectionValue.fromRawJavaType(entry.getValue()));
                }
            }
            return new ExecutionResultImpl(graphqlIntrospectionObject(copy), sourceResult.getErrors(), sourceResult.getExtensions());
        }
    });
}
Also used : TypedValue(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue) SerializableList.serializableList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.SerializableList.serializableList) GraphqlIntrospectionValue(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionValue) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) CompletableFuture(java.util.concurrent.CompletableFuture) ExecutionContext(graphql.execution.ExecutionContext) GraphqlIntrospectionObject.graphqlIntrospectionObject(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionObject.graphqlIntrospectionObject) SubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference) ExecutionStrategyParameters(graphql.execution.ExecutionStrategyParameters) Value(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Value) ExecutionResult(graphql.ExecutionResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AsyncExecutionStrategy(graphql.execution.AsyncExecutionStrategy) ExecutionResultImpl(graphql.ExecutionResultImpl) QueryContainer(nl.knaw.huygens.timbuctoo.v5.serializable.dto.QueryContainer) GraphQLObjectType(graphql.schema.GraphQLObjectType) GraphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList) Logger(org.slf4j.Logger) NonNullableFieldWasNullException(graphql.execution.NonNullableFieldWasNullException) RdfConstants(nl.knaw.huygens.timbuctoo.v5.util.RdfConstants) Set(java.util.Set) Serializable(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Serializable) Field(graphql.language.Field) DatabaseResult(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult) Collectors(java.util.stream.Collectors) List(java.util.List) PaginatedList(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.PaginatedList) StringValue(graphql.language.StringValue) PredicateInfo(nl.knaw.huygens.timbuctoo.v5.serializable.dto.PredicateInfo) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Optional(java.util.Optional) Direction(nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction) GraphqlIntrospectionList.graphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList.graphqlIntrospectionList) Entity(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Entity) Serializable(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Serializable) Set(java.util.Set) Optional(java.util.Optional) GraphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList) SubjectReference(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference) GraphQLFieldDefinition(graphql.schema.GraphQLFieldDefinition) LinkedHashMap(java.util.LinkedHashMap) ExecutionResultImpl(graphql.ExecutionResultImpl) GraphQLObjectType(graphql.schema.GraphQLObjectType) TypedValue(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue) GraphqlIntrospectionValue(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionValue) Value(nl.knaw.huygens.timbuctoo.v5.serializable.dto.Value) StringValue(graphql.language.StringValue) SerializableList.serializableList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.SerializableList.serializableList) GraphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList) List(java.util.List) PaginatedList(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.PaginatedList) GraphqlIntrospectionList.graphqlIntrospectionList(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionList.graphqlIntrospectionList) GraphqlIntrospectionObject.graphqlIntrospectionObject(nl.knaw.huygens.timbuctoo.v5.serializable.dto.GraphqlIntrospectionObject.graphqlIntrospectionObject) PredicateInfo(nl.knaw.huygens.timbuctoo.v5.serializable.dto.PredicateInfo) PaginatedList(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.PaginatedList) TypedValue(nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue)

Aggregations

SubjectReference (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.SubjectReference)8 DataSet (nl.knaw.huygens.timbuctoo.v5.dataset.dto.DataSet)6 TypedValue (nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue)5 List (java.util.List)4 Optional (java.util.Optional)4 Direction (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.Direction)4 Logger (org.slf4j.Logger)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 IOException (java.io.IOException)3 Map (java.util.Map)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 QuadStore (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.QuadStore)3 CursorQuad (nl.knaw.huygens.timbuctoo.v5.datastores.quadstore.dto.CursorQuad)3 LoggerFactory (org.slf4j.LoggerFactory)3 ArrayList (java.util.ArrayList)2 TypeNameStore (nl.knaw.huygens.timbuctoo.v5.datastores.prefixstore.TypeNameStore)2 Type (nl.knaw.huygens.timbuctoo.v5.datastores.schemastore.dto.Type)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1