use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult in project timbuctoo by HuygensING.
the class RdfWiringFactory method getDataFetcher.
@Override
public DataFetcher getDataFetcher(FieldWiringEnvironment environment) {
if (environment.getFieldDefinition().getDirective("passThrough") != null) {
return DataFetchingEnvironment::getSource;
} else if (environment.getFieldDefinition().getDirective("related") != null) {
final Directive directive = environment.getFieldDefinition().getDirective("related");
String source = ((StringValue) directive.getArgument("source").getValue()).getValue();
String predicate = ((StringValue) directive.getArgument("predicate").getValue()).getValue();
String direction = ((StringValue) directive.getArgument("direction").getValue()).getValue();
return new CollectionFetcherWrapper(argumentsHelper, new RelationsOfSubjectDataFetcher(source, predicate, Direction.valueOf(direction)));
} else if (environment.getFieldDefinition().getDirective("fromCollection") != null) {
final Directive directive = environment.getFieldDefinition().getDirective("fromCollection");
String uri = ((StringValue) directive.getArgument("uri").getValue()).getValue();
boolean listAll = ((BooleanValue) directive.getArgument("listAll").getValue()).isValue();
if (listAll) {
return new CollectionFetcherWrapper(argumentsHelper, new CollectionDataFetcher(uri));
} else {
return lookupFetcher;
}
} else if (environment.getFieldDefinition().getDirective("rdf") != null) {
final Directive directive = environment.getFieldDefinition().getDirective("rdf");
String uri = ((StringValue) directive.getArgument("predicate").getValue()).getValue();
Direction direction = valueOf(((StringValue) directive.getArgument("direction").getValue()).getValue());
boolean isList = ((BooleanValue) directive.getArgument("isList").getValue()).isValue();
boolean isObject = ((BooleanValue) directive.getArgument("isObject").getValue()).isValue();
boolean isValue = ((BooleanValue) directive.getArgument("isValue").getValue()).isValue();
if (isObject && isValue) {
return new DataFetcherWrapper(argumentsHelper, isList, new UnionDataFetcher(uri, direction));
} else {
if (isObject) {
return new DataFetcherWrapper(argumentsHelper, isList, new RelationDataFetcher(uri, direction));
} else {
return new DataFetcherWrapper(argumentsHelper, isList, new TypedLiteralDataFetcher(uri));
}
}
} else if (environment.getFieldDefinition().getDirective("uri") != null) {
return uriFetcher;
} else if (environment.getFieldDefinition().getDirective("dataSet") != null) {
final Directive directive = environment.getFieldDefinition().getDirective("dataSet");
String userId = ((StringValue) directive.getArgument("userId").getValue()).getValue();
String dataSetId = ((StringValue) directive.getArgument("dataSetId").getValue()).getValue();
final DataSet dataSet = dataSetRepository.unsafeGetDataSetWithoutCheckingPermissions(userId, dataSetId).orElse(null);
return dataFetchingEnvironment -> new DatabaseResult() {
@Override
public DataSet getDataSet() {
return dataSet;
}
};
} else if (environment.getFieldDefinition().getDirective("entityTitle") != null) {
return entityTitleFetcher;
} else if (environment.getFieldDefinition().getDirective("entityDescription") != null) {
return entityDescriptionFetcher;
} else if (environment.getFieldDefinition().getDirective("entityImage") != null) {
return entityImageFetcher;
}
return null;
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult in project timbuctoo by HuygensING.
the class LookUpSubjectByUriFetcherWrapper method get.
@Override
public Object get(DataFetchingEnvironment environment) {
final DatabaseResult source = environment.getSource();
ParsedURI baseUri = new ParsedURI(source.getDataSet().getMetadata().getBaseUri());
String uri = environment.getArgument(uriArgument);
return lookUpSubjectByUriFetcher.getItem(baseUri.resolve(uri).toString(), source.getDataSet());
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult in project timbuctoo by HuygensING.
the class PaginationArgumentsHelper method getPaginationArguments.
public PaginationArguments getPaginationArguments(DataFetchingEnvironment environment) {
String cursor = "";
int count = DEFAULT_COUNT;
if (environment.containsArgument("cursor") && (environment.getArgument("cursor") instanceof String)) {
cursor = new String(DECODER.decode((String) environment.getArgument("cursor")), Charsets.UTF_8);
}
if (environment.containsArgument("count")) {
count = environment.getArgument("count");
}
ConfiguredFilter filter = null;
for (Map.Entry<String, CollectionFilter> entry : collectionFilters.entrySet()) {
if (environment.containsArgument(entry.getKey())) {
String searchQuery = environment.getArgument(entry.getKey());
String cursorArg = cursor;
int countArg = count;
String dataSetId = ((DatabaseResult) environment.getSource()).getDataSet().getMetadata().getCombinedId();
String fieldName = environment.getFieldDefinition().getName();
filter = () -> entry.getValue().query(dataSetId, fieldName, searchQuery, cursorArg, countArg);
break;
}
}
return PaginationArguments.create(count, cursor, Optional.ofNullable(filter));
}
use of nl.knaw.huygens.timbuctoo.v5.graphql.datafetchers.dto.DatabaseResult 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());
}
});
}
Aggregations