use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue 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;
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue 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());
}
});
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue in project timbuctoo by HuygensING.
the class SummaryPropDataRetrieverTest method createSummaryPropertyWalksTheSecondPathIfTheFirstGivesNoValue.
@Test
public void createSummaryPropertyWalksTheSecondPathIfTheFirstGivesNoValue() {
List<SummaryProp> defaultProperties = Lists.newArrayList(summaryPropertyWithPath("http://example.org/path", "http://example.org/path2"));
SummaryPropDataRetriever instance = new SummaryPropDataRetriever("http://example.org/userConfigured", defaultProperties);
QuadStore quadStore = mock(QuadStore.class);
CursorQuad foundQuad1 = quadWithObject("http://example.org/objectFound1", Optional.empty());
CursorQuad foundQuad2 = quadWithObject("http://example.org/objectFound2", Optional.empty());
given(quadStore.getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "")).willReturn(Stream.of(foundQuad1, foundQuad2));
CursorQuad quadWithObjectUriOfPath2 = quadWithObject("http://example.org/objectOfPath2", Optional.empty());
given(quadStore.getQuads("http://example.org/objectFound2", "http://example.org/path2", Direction.OUT, "")).willReturn(Stream.of(quadWithObjectUriOfPath2));
Optional<TypedValue> summaryProperty = instance.createSummaryProperty(subjectWithUri("http://example.org/source"), dataSetWithQuadStore(quadStore));
assertThat(summaryProperty, is(present()));
assertThat(summaryProperty.get(), hasProperty("value", is("http://example.org/objectOfPath2")));
verify(quadStore).getQuads("http://example.org/source", "http://example.org/path", Direction.OUT, "");
verify(quadStore).getQuads("http://example.org/objectFound1", "http://example.org/path2", Direction.OUT, "");
verify(quadStore).getQuads("http://example.org/objectFound2", "http://example.org/path2", Direction.OUT, "");
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.TypedValue in project timbuctoo by HuygensING.
the class SummaryPropDataRetrieverTest method createSummaryPropertySearchesUntilItFindsAValue.
@Test
public void createSummaryPropertySearchesUntilItFindsAValue() {
List<SummaryProp> defaultProperties = Lists.newArrayList(summaryPropertyWithPath("http://example.org/prop1"), summaryPropertyWithPath("http://example.org/prop2"), summaryPropertyWithPath("http://example.org/prop3"));
SummaryPropDataRetriever instance = new SummaryPropDataRetriever("http://example.org/userConfigured", defaultProperties);
QuadStore quadStore = mock(QuadStore.class);
CursorQuad foundQuad = quadWithObject("http://example.org/objectFound1", Optional.empty());
given(foundQuad.getObject()).willReturn("http://example.org/value");
given(quadStore.getQuads("http://example.org/source", "http://example.org/prop2", Direction.OUT, "")).willReturn(Stream.of(foundQuad));
Optional<TypedValue> found = instance.createSummaryProperty(subjectWithUri("http://example.org/source"), dataSetWithQuadStore(quadStore));
assertThat(found, is(present()));
assertThat(found.get(), hasProperty("value", is("http://example.org/value")));
verify(quadStore).getQuads("http://example.org/source", "http://example.org/prop1", Direction.OUT, "");
verify(quadStore).getQuads("http://example.org/source", "http://example.org/prop2", Direction.OUT, "");
verify(quadStore, never()).getQuads(anyString(), eq("http://example.org/prop3"), eq(Direction.OUT), eq(""));
}
Aggregations