diff --git a/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/ResolverUtils.java b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/ResolverUtils.java index 61b1e6a893bdd5..142a1c77f719e6 100644 --- a/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/ResolverUtils.java +++ b/datahub-graphql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/ResolverUtils.java @@ -96,7 +96,7 @@ public static VersionedAspect getAspectFromLocalContext(DataFetchingEnvironment Object localContext = environment.getLocalContext(); // if we have context & the version is 0, we should try to retrieve it from the fetched entity // otherwise, we should just fetch the entity from the aspect resource - if (localContext == null && version == 0 || version == null) { + if (localContext != null && version == 0 || version == null) { if (localContext instanceof Map) { // de-register the prefetched aspect from local context. Since aspects will only // ever be first-class properties of an entity type, local context will always diff --git a/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanEntityService.java b/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanEntityService.java index 0dbaa24c0b916c..2c1e1820cb5db9 100644 --- a/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanEntityService.java +++ b/metadata-io/src/main/java/com/linkedin/metadata/entity/ebean/EbeanEntityService.java @@ -91,12 +91,22 @@ public Map> getLatestAspects(@Nonnull final Set u return urnToAspects; } + /* + * When a user tries to fetch a negative version, we want to index most recent to least recent snapshots. + * To do this, we want to fetch the maximum version and subtract the negative version from that. Since -1 represents + * the maximum version, we need to add 1 to the final result. + */ + private long calculateVersionNumber(@Nonnull final Urn urn, @Nonnull final String aspectName, @Nonnull long version) { + if (version < 0) { + return _entityDao.getMaxVersion(urn.toString(), aspectName) + version + 1; + } + return version; + } + @Override @Nullable public RecordTemplate getAspect(@Nonnull final Urn urn, @Nonnull final String aspectName, @Nonnull long version) { - if (version < 0) { - version = _entityDao.getMaxVersion(urn.toString(), aspectName) - version + 1; - } + version = calculateVersionNumber(urn, aspectName, version); final EbeanAspectV2.PrimaryKey primaryKey = new EbeanAspectV2.PrimaryKey(urn.toString(), aspectName, version); final Optional maybeAspect = Optional.ofNullable(_entityDao.getAspect(primaryKey)); return maybeAspect @@ -108,9 +118,7 @@ public RecordTemplate getAspect(@Nonnull final Urn urn, @Nonnull final String as public VersionedAspect getVersionedAspect(@Nonnull Urn urn, @Nonnull String aspectName, long version) { VersionedAspect result = new VersionedAspect(); - if (version < 0) { - version = _entityDao.getMaxVersion(urn.toString(), aspectName) + version + 1; - } + version = calculateVersionNumber(urn, aspectName, version); final EbeanAspectV2.PrimaryKey primaryKey = new EbeanAspectV2.PrimaryKey(urn.toString(), aspectName, version); final Optional maybeAspect = Optional.ofNullable(_entityDao.getAspect(primaryKey));