use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class SerializerExecutionStrategy method execute.
@Override
public CompletableFuture<ExecutionResult> execute(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException {
GraphQLObjectType parentType = (GraphQLObjectType) parameters.getExecutionStepInfo().getUnwrappedNonNullType();
return super.execute(executionContext, parameters).thenApply(sourceResult -> {
Map<String, Object> data = sourceResult.getData();
if (parameters.getSource() instanceof TypedValue) {
String value = ((TypedValue) parameters.getSource()).getValue();
String typename = ((TypedValue) parameters.getSource()).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.getSource() instanceof SubjectReference) {
final String uri = ((SubjectReference) parameters.getSource()).getSubjectUri();
final Set<String> types = ((SubjectReference) parameters.getSource()).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 RDFS_RESOURCE");
type = RdfConstants.RDFS_RESOURCE;
}
}
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.getSource() instanceof PaginatedList) {
PaginatedList<? extends DatabaseResult> source = (PaginatedList) parameters.getSource();
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.mutations.Change.Value in project timbuctoo by HuygensING.
the class OpenIdConnectUserValidator method getUserFromAccessToken.
@Override
public Optional<User> getUserFromAccessToken(String accessToken) throws UserValidationException {
if (StringUtils.isBlank(accessToken)) {
return Optional.empty();
}
final User local = users.getIfPresent(accessToken);
if (local != null) {
return Optional.of(local);
}
try {
final Optional<UserInfo> userInfoOpt = openIdClient.getUserInfo(accessToken);
if (userInfoOpt.isEmpty()) {
return Optional.empty();
}
final UserInfo userInfo = userInfoOpt.get();
final String subject = userInfo.getSubject().getValue();
final Optional<User> user = userStore.userFor(subject);
if (user.isPresent()) {
user.ifPresent(value -> users.put(accessToken, value));
return user;
} else {
final User newUser = userStore.saveNew(userInfo.getNickname(), subject);
users.put(subject, newUser);
return Optional.of(newUser);
}
} catch (AuthenticationUnavailableException | IOException | ParseException e) {
throw new UserValidationException(e);
}
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class ViewConfigMutation method executeAction.
@Override
public Object executeAction(DataFetchingEnvironment env) {
String collectionUri = env.getArgument("collectionUri");
Object viewConfig = env.getArgument("viewConfig");
DataSet dataSet = MutationHelpers.getDataSet(env, dataSetRepository::getDataSet);
MutationHelpers.checkPermission(env, dataSet.getMetadata(), Permission.CONFIG_VIEW);
try {
MutationHelpers.addMutation(dataSet, new PredicateMutation().entity(collectionUri, replace(HAS_VIEW_CONFIG, value(OBJECT_MAPPER.writeValueAsString(viewConfig)))));
return viewConfig;
} catch (LogStorageFailedException | InterruptedException | ExecutionException | JsonProcessingException e) {
throw new RuntimeException(e);
}
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class JsonLdSerialization method writeContext.
private void writeContext(Set<PredicateInfo> context) throws IOException {
generator.writeFieldName("@context");
generator.writeStartObject();
// ignore the data wrapper by marking it as an index map and as the @graph container
generator.writeFieldName("data");
generator.writeStartObject();
generator.writeStringField("@id", "@graph");
generator.writeStringField("@container", "@index");
generator.writeEndObject();
generator.writeStringField("value", "@value");
generator.writeStringField("type", "@type");
for (PredicateInfo entry : context) {
if (entry.getUri().isPresent()) {
if (entry.getDirection() == Direction.IN) {
generator.writeFieldName(entry.getSafeName());
generator.writeStartObject();
if (entry.getDirection() == Direction.IN) {
generator.writeStringField("@reverse", entry.getUri().get());
} else {
generator.writeStringField("@id", entry.getUri().get());
}
generator.writeEndObject();
} else {
generator.writeStringField(entry.getSafeName(), entry.getUri().get());
}
} else {
generator.writeNullField(entry.getSafeName());
}
}
generator.writeEndObject();
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.mutations.Change.Value in project timbuctoo by HuygensING.
the class SummaryPropDataRetrieverTest method createSummaryPropertyWalksTheSecondPathIfTheFirstGivesNoValue.
@Test
public void createSummaryPropertyWalksTheSecondPathIfTheFirstGivesNoValue() {
List<SummaryProp> defaultProperties = Lists.newArrayList(summaryPropertyWithPath(DEFAULT_PATH, DEFAULT_PATH_2));
SummaryPropDataRetriever instance = new SummaryPropDataRetriever(USER_CONFIGURED_PATH, defaultProperties);
QuadStore quadStore = mock(QuadStore.class);
CursorQuad foundQuad1 = quadWithObject(OBJECT_1, Optional.empty());
CursorQuad foundQuad2 = quadWithObject(OBJECT_2, Optional.empty());
given(quadStore.getQuadsInGraph(SOURCE, DEFAULT_PATH, Direction.OUT, "", Optional.of(new Graph(GRAPH)))).willReturn(Stream.of(foundQuad1, foundQuad2));
CursorQuad objectOfPath2 = quadWithObject(OBJECT_OF_PATH2, Optional.empty());
given(quadStore.getQuadsInGraph(OBJECT_2, DEFAULT_PATH_2, Direction.OUT, "", Optional.of(new Graph(GRAPH)))).willReturn(Stream.of(objectOfPath2));
Optional<TypedValue> summaryProperty = instance.createSummaryProperty(subjectWithUriAndGraph(SOURCE, Optional.of(new Graph(GRAPH))), dataSetWithQuadStore(quadStore), COLLECTION);
assertThat(summaryProperty, is(present()));
assertThat(summaryProperty.get(), hasProperty("value", is(OBJECT_OF_PATH2)));
verify(quadStore).getQuadsInGraph(SOURCE, DEFAULT_PATH, Direction.OUT, "", Optional.of(new Graph(GRAPH)));
verify(quadStore).getQuadsInGraph(OBJECT_1, DEFAULT_PATH_2, Direction.OUT, "", Optional.of(new Graph(GRAPH)));
verify(quadStore).getQuadsInGraph(OBJECT_2, DEFAULT_PATH_2, Direction.OUT, "", Optional.of(new Graph(GRAPH)));
}
Aggregations