Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
- Remove Gql prefix from type names
- Return object on mutations
  • Loading branch information
geichelberger committed Apr 8, 2024
1 parent c53eed2 commit 3ce4171
Show file tree
Hide file tree
Showing 54 changed files with 942 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,30 @@
import org.opencastproject.graphql.provider.GraphQLAdditionalTypeProvider;
import org.opencastproject.graphql.provider.GraphQLDynamicTypeProvider;
import org.opencastproject.graphql.provider.GraphQLExtensionProvider;
import org.opencastproject.graphql.provider.GraphQLTypeFunctionProvider;
import org.opencastproject.graphql.type.DateTimeFunction;
import org.opencastproject.graphql.type.DurationFunction;
import org.opencastproject.graphql.type.JsonFunction;
import org.opencastproject.graphql.type.MapFunction;
import org.opencastproject.graphql.type.TimeFunction;
import org.opencastproject.graphql.type.input.DublinCoreMetadataInput;
import org.opencastproject.graphql.type.input.GqlCommonEventMetadataInput;
import org.opencastproject.graphql.type.input.GqlCommonSeriesMetadataInput;
import org.opencastproject.graphql.type.output.GqlAccessControlGenericEntry;
import org.opencastproject.graphql.type.output.GqlAccessControlGroupEntry;
import org.opencastproject.graphql.type.output.GqlAccessControlUserEntry;
import org.opencastproject.graphql.type.output.GqlCommonEventMetadata;
import org.opencastproject.graphql.type.output.GqlCommonEventMetadataV2;
import org.opencastproject.graphql.type.output.GqlCommonSeriesMetadata;
import org.opencastproject.graphql.type.output.GqlCommonSeriesMetadataV2;
import org.opencastproject.graphql.type.output.GqlDublinCoreMetadata;
import org.opencastproject.graphql.type.output.field.GqlDurationMetadataField;
import org.opencastproject.graphql.type.output.field.GqlIntMetadataField;
import org.opencastproject.graphql.type.output.field.GqlJsonMetadataField;
import org.opencastproject.graphql.type.output.field.GqlListMetadataField;
import org.opencastproject.graphql.type.output.field.GqlLongMetadataField;
import org.opencastproject.graphql.util.MetadataFieldToGraphQLConverter;
import org.opencastproject.graphql.util.MetadataFieldToGraphQLFieldMapper;
import org.opencastproject.index.service.api.IndexService;
import org.opencastproject.index.service.catalog.adapter.ConfigurableDCCatalogUIAdapter;
import org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter;
Expand All @@ -53,6 +67,7 @@
import java.util.Set;

import graphql.annotations.processor.GraphQLAnnotations;
import graphql.annotations.processor.typeFunctions.TypeFunction;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
Expand All @@ -63,7 +78,7 @@
@Component
@ServiceDescription("Opencast GraphQL Provider")
public class OpencastGraphQLProvider implements GraphQLExtensionProvider, GraphQLDynamicTypeProvider,
GraphQLAdditionalTypeProvider {
GraphQLAdditionalTypeProvider, GraphQLTypeFunctionProvider {

private final IndexService indexService;

Expand All @@ -78,6 +93,20 @@ public OpencastGraphQLProvider(
this.securityService = securityService;
}

@Override
public Set<Class<?>> getAdditionalOutputTypes() {
return Set.of(
GqlAccessControlUserEntry.class,
GqlAccessControlGroupEntry.class,
GqlAccessControlGenericEntry.class,
GqlJsonMetadataField.class,
GqlLongMetadataField.class,
GqlDurationMetadataField.class,
GqlIntMetadataField.class,
GqlListMetadataField.class
);
}

@Override
public Map<String, GraphQLOutputType> getDynamicOutputTypes(Organization organization,
GraphQLAnnotations graphQLAnnotations) {
Expand Down Expand Up @@ -134,7 +163,7 @@ private void commonMetadataInput(HashMap<String, GraphQLInputType> inputTypes, G
adapter.getRawFields().getFields().stream().filter(f -> !f.isReadOnly()).forEach(
f -> fieldDefinitions.add(
GraphQLInputObjectField.newInputObjectField()
.name(f.getInputID())
.name(f.getOutputID())
.type((GraphQLInputType) MetadataFieldToGraphQLConverter.convertType(f))
.description(f.getLabel())
.build()
Expand Down Expand Up @@ -231,8 +260,9 @@ private void commonMetadataV2(HashMap<String, GraphQLOutputType> outputTypes, Gr
var outputId = m.getOutputID();

builder.name(outputId != null ? outputId : m.getInputID());
var outputType = MetadataFieldToGraphQLFieldMapper.mapToClass(m.getType());
builder.type((GraphQLOutputType) graphQLAnnotations.getObjectHandler().getTypeRetriever()
.getGraphQLType(GqlJsonMetadataField.class, graphQLAnnotations.getContainer(), false));
.getGraphQLType(outputType, graphQLAnnotations.getContainer(), false));
builder.description(m.getLabel());

fieldDefinitions.add(builder.build());
Expand All @@ -247,4 +277,14 @@ private void commonMetadataV2(HashMap<String, GraphQLOutputType> outputTypes, Gr
outputTypes.put(graphQLTypeName, transformedObjectType);
}

@Override
public Set<TypeFunction> getTypeFunctions() {
return Set.of(
new DateTimeFunction(),
new DurationFunction(),
new JsonFunction(),
new MapFunction(),
new TimeFunction()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.opencastproject.graphql.execution.context.OpencastContext;
import org.opencastproject.graphql.execution.context.OpencastContextManager;
import org.opencastproject.graphql.type.input.GqlCommonEventMetadataInput;
import org.opencastproject.graphql.util.MetadataFieldToGraphQLConverter;
import org.opencastproject.index.service.api.IndexService;
import org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter;
import org.opencastproject.index.service.exception.IndexServiceException;
Expand All @@ -40,8 +41,14 @@
import org.opencastproject.security.api.UnauthorizedException;
import org.opencastproject.util.NotFoundException;

import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLTypeUtil;

public class UpdateEventCommand extends AbstractCommand<GqlEvent> {

private final String eventId;
Expand All @@ -58,7 +65,6 @@ public GqlEvent execute() {
final IndexService indexService = context.getService(IndexService.class);

final Map<String, Object> eventMetadata = environment.getArgument("metadata");

try {
indexService.updateEventMetadata(eventId, createMetadataList(eventMetadata, indexService), index);
} catch (IndexServiceException | SearchIndexException e) {
Expand All @@ -76,7 +82,10 @@ public GqlEvent execute() {
}
}

private MetadataList createMetadataList(Map<String, Object> eventMetadata, IndexService indexService) {
private MetadataList createMetadataList(
Map<String, Object> eventMetadata,
IndexService indexService
) {
CommonEventCatalogUIAdapter adapter = (CommonEventCatalogUIAdapter) indexService
.getCommonEventCatalogUIAdapter();

Expand All @@ -88,10 +97,58 @@ private MetadataList createMetadataList(Map<String, Object> eventMetadata, Index

final DublinCoreMetadataCollection collection = list.getMetadataByFlavor(flavor.toString());

eventMetadata.keySet().forEach(k -> {
final MetadataField target = collection.getOutputFields().get(k);
target.setValue(eventMetadata.get(k));
});
for (Map.Entry<String, Object> entry: eventMetadata.entrySet()) {
String key = entry.getKey();
final MetadataField target = collection.getOutputFields().get(key);
var type = (GraphQLScalarType) GraphQLTypeUtil.unwrapNonNull(MetadataFieldToGraphQLConverter.convertType(target));

Object value = type.getCoercing().parseValue(
eventMetadata.get(key),
environment.getGraphQlContext(),
environment.getLocale()
);

if (value == null) {
continue;
}

switch (target.getType()) {
case DATE:
case START_DATE:
target.setValue(DateTimeFormatter.ofPattern(target.getPattern()).format((OffsetDateTime)value));
break;
case LONG:
target.setValue(value);
break;
case TEXT:
target.setValue(value);
break;
case BOOLEAN:
target.setValue(value);
break;
case DURATION:
target.setValue(((Duration) value).toMillis());
break;
case TEXT_LONG:
target.setValue(value);
break;
case MIXED_TEXT:
target.setValue(value);
break;
case START_TIME:
target.setValue(value);
break;
case ORDERED_TEXT:
target.setValue(value);
break;
case ITERABLE_TEXT:
target.setValue(value);
break;
default:
target.setValue(value);
break;
}
}

return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
import org.opencastproject.graphql.exception.GraphQLRuntimeException;
import org.opencastproject.graphql.exception.OpencastErrorType;
import org.opencastproject.graphql.execution.context.OpencastContext;
import org.opencastproject.graphql.type.output.field.GqlJsonMetadataField;
import org.opencastproject.graphql.type.output.GqlMetadataFieldInterface;
import org.opencastproject.graphql.util.MetadataFieldToGraphQLFieldMapper;
import org.opencastproject.index.service.api.IndexService;
import org.opencastproject.index.service.impl.util.EventUtils;
import org.opencastproject.metadata.dublincore.DublinCoreMetadataCollection;
Expand All @@ -44,10 +45,10 @@

import graphql.schema.DataFetchingEnvironment;

public class CommonEventMetadataV2DataFetcher implements ContextDataFetcher<Map<String, GqlJsonMetadataField>> {
public class CommonEventMetadataV2DataFetcher implements ContextDataFetcher<Map<String, GqlMetadataFieldInterface>> {

@Override
public Map<String, GqlJsonMetadataField> get(OpencastContext opencastContext,
public Map<String, GqlMetadataFieldInterface> get(OpencastContext opencastContext,
DataFetchingEnvironment dataFetchingEnvironment) {
String eventId = ((GqlEvent)dataFetchingEnvironment.getSource()).id();
ElasticsearchIndex searchIndex = opencastContext.getService(ElasticsearchIndex.class);
Expand All @@ -60,9 +61,11 @@ public Map<String, GqlJsonMetadataField> get(OpencastContext opencastContext,
}
Event event = opt.get();
EventCatalogUIAdapter eventCatalogUiAdapter = indexService.getCommonEventCatalogUIAdapter();
Map<String, GqlJsonMetadataField> result = new HashMap<>();
Map<String, GqlMetadataFieldInterface> result = new HashMap<>();
DublinCoreMetadataCollection collection = EventUtils.getEventMetadata(event, eventCatalogUiAdapter);
collection.getOutputFields().forEach((key, value) -> result.put(key, new GqlJsonMetadataField(value)));
collection.getOutputFields().forEach(
(key, value) -> result.put(key, MetadataFieldToGraphQLFieldMapper.mapType(value))
);
return result;
} catch (SearchIndexException | ParseException e) {
throw new GraphQLRuntimeException(OpencastErrorType.InternalError, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
package org.opencastproject.graphql.datafetcher.event;

import org.opencastproject.elasticsearch.api.SearchIndexException;
import org.opencastproject.elasticsearch.api.SearchResult;
import org.opencastproject.elasticsearch.index.ElasticsearchIndex;
import org.opencastproject.elasticsearch.index.objects.event.Event;
import org.opencastproject.elasticsearch.index.objects.event.EventSearchQuery;
import org.opencastproject.graphql.datafetcher.ContextDataFetcher;
import org.opencastproject.graphql.event.GqlEvent;
import org.opencastproject.graphql.exception.GraphQLRuntimeException;
Expand Down Expand Up @@ -55,8 +58,19 @@ public GqlEvent get(OpencastContext opencastContext, DataFetchingEnvironment dat
ElasticsearchIndex searchIndex = opencastContext.getService(ElasticsearchIndex.class);

try {
return searchIndex.getEvent(eventId, securityService.getOrganization().toString(), securityService.getUser())
.map(GqlEvent::new).orElse(null);
SearchResult<Event> result = searchIndex.getByQuery(
new EventSearchQuery(securityService.getOrganization().getId(), securityService.getUser())
.withIdentifier(eventId)
);
if (result.getDocumentCount() == 0) {
return null;
} else if (result.getDocumentCount() == 1) {
return new GqlEvent(result.getItems()[0].getSource());
}
throw new GraphQLRuntimeException(
"Multiple events found with the same identifier",
OpencastErrorType.InternalError
);
} catch (SearchIndexException e) {
throw new GraphQLRuntimeException(OpencastErrorType.InternalError, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
package org.opencastproject.graphql.datafetcher.series;

import org.opencastproject.elasticsearch.api.SearchIndexException;
import org.opencastproject.elasticsearch.api.SearchResult;
import org.opencastproject.elasticsearch.index.ElasticsearchIndex;
import org.opencastproject.elasticsearch.index.objects.series.Series;
import org.opencastproject.elasticsearch.index.objects.series.SeriesSearchQuery;
import org.opencastproject.graphql.datafetcher.ContextDataFetcher;
import org.opencastproject.graphql.exception.GraphQLRuntimeException;
import org.opencastproject.graphql.exception.OpencastErrorType;
Expand Down Expand Up @@ -57,8 +60,19 @@ public GqlSeries get(OpencastContext opencastContext, DataFetchingEnvironment da
ElasticsearchIndex searchIndex = opencastContext.getService(ElasticsearchIndex.class);

try {
return searchIndex.getSeries(seriesId, securityService.getOrganization().toString(), securityService.getUser())
.map(GqlSeries::new).orElse(null);
SearchResult<Series> result = searchIndex.getByQuery(
new SeriesSearchQuery(securityService.getOrganization().getId(), securityService.getUser())
.withIdentifier(seriesId)
);
if (result.getDocumentCount() == 0) {
return null;
} else if (result.getDocumentCount() == 1) {
return new GqlSeries(result.getItems()[0].getSource());
}
throw new GraphQLRuntimeException(
"Multiple series found with the same identifier",
OpencastErrorType.InternalError
);
} catch (SearchIndexException e) {
throw new GraphQLRuntimeException(OpencastErrorType.InternalError, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
import graphql.schema.DataFetchingEnvironment;

@GraphQLTypeExtension(GqlCurrentUser.class)
public class EventUserExtension {
public class EventCurrentUserExtension {

private final GqlCurrentUser currentUser;

public EventUserExtension(GqlCurrentUser currentUser) {
public EventCurrentUserExtension(GqlCurrentUser currentUser) {
this.currentUser = currentUser;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ private EventMutationExtension() {
@GraphQLField
@GraphQLNonNull
@GraphQLDescription("Update event metadata")
public static Boolean updateEvent(
public static GqlEvent updateEvent(
@GraphQLName("id") @GraphQLNonNull String id,
@GraphQLName("metadata") @GraphQLNonNull GqlCommonEventMetadataInput eventMetadataInput,
final DataFetchingEnvironment environment) {
UpdateEventCommand
return UpdateEventCommand
.create(id, eventMetadataInput)
.environment(environment)
.build()
.execute();
return true;
}

@GraphQLField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@GraphQLName(GqlDeleteEventPayload.TYPE_NAME)
@GraphQLDescription("DeleteEventPayload")
public class GqlDeleteEventPayload {
public static final String TYPE_NAME = "GqlDeleteEventPayload";
public static final String TYPE_NAME = "DeleteEventPayload";

public GqlDeleteEventPayload() {

Expand Down
Loading

0 comments on commit 3ce4171

Please sign in to comment.