From 31f0ade726a8fa40defcd486a91fd9398812dbb8 Mon Sep 17 00:00:00 2001 From: Vyacheslav Matyukhin Date: Wed, 10 Jan 2024 15:07:51 -0600 Subject: [PATCH] fix 404 bug in relative values --- packages/hub/schema.graphql | 4 +- .../[variableName]/CacheMenu/index.tsx | 25 ++++----- .../[variableName]/RelativeValuesExport.ts | 25 +++++++++ .../RelativeValuesModelLayout.tsx | 52 +++++++++++++------ .../RelativeValuesModelRevision.ts | 38 -------------- .../hub/src/graphql/types/ModelRevision.ts | 13 ++--- .../relative-values/values/ModelEvaluator.ts | 6 +-- 7 files changed, 80 insertions(+), 83 deletions(-) create mode 100644 packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesExport.ts delete mode 100644 packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesModelRevision.ts diff --git a/packages/hub/schema.graphql b/packages/hub/schema.graphql index 5e2b5b1730..191b4fce42 100644 --- a/packages/hub/schema.graphql +++ b/packages/hub/schema.graphql @@ -185,7 +185,7 @@ type ModelRevision implements Node { content: ModelContent! createdAtTimestamp: Float! exports: [ModelExport!]! - forRelativeValues(input: ModelRevisionForRelativeValuesInput): RelativeValuesExport + forRelativeValues(input: ModelRevisionForRelativeValuesInput!): ModelRevisionForRelativeValuesResult! id: ID! model: Model! relativeValuesExports: [RelativeValuesExport!]! @@ -206,6 +206,8 @@ input ModelRevisionForRelativeValuesInput { variableName: String! } +union ModelRevisionForRelativeValuesResult = BaseError | NotFoundError | RelativeValuesExport + input ModelRevisionForRelativeValuesSlugOwnerInput { owner: String! slug: String! diff --git a/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/CacheMenu/index.tsx b/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/CacheMenu/index.tsx index d9ab547845..aef312f919 100644 --- a/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/CacheMenu/index.tsx +++ b/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/CacheMenu/index.tsx @@ -17,26 +17,22 @@ import { BuildRelativeValuesCacheAction } from "./BuildRelativeValuesCacheAction import { ClearRelativeValuesCacheAction } from "./ClearRelativeValuesCacheAction"; import { RelativeValuesDefinitionRevision$key } from "@/__generated__/RelativeValuesDefinitionRevision.graphql"; -import { RelativeValuesModelRevision$data } from "@/__generated__/RelativeValuesModelRevision.graphql"; +import { RelativeValuesExport$data } from "@/__generated__/RelativeValuesExport.graphql"; export const CacheMenu: FC<{ - revision: RelativeValuesModelRevision$data; + relativeValuesExport: RelativeValuesExport$data; isEditable: boolean; -}> = ({ revision, isEditable }) => { - if (!revision.forRelativeValues) { - throw new Error("Not found"); - } - +}> = ({ relativeValuesExport, isEditable }) => { const definition = useFragment( RelativeValuesDefinitionRevisionFragment, - revision.forRelativeValues.definition.currentRevision + relativeValuesExport.definition.currentRevision ); - const isEmpty = revision.forRelativeValues.cache.length === 0; + const isEmpty = relativeValuesExport.cache.length === 0; const fullyCached = !isEmpty && - revision.forRelativeValues.cache.length >= + relativeValuesExport.cache.length >= definition.items.length * definition.items.length; const internals = ( @@ -63,27 +59,24 @@ export const CacheMenu: FC<{ const withDropdown = (internals: ReactElement) => ( { - if (!revision.forRelativeValues?.id) { - return null; // shouldn't happen, this is mostly for type safety - } return ( {isEmpty ? "Not cached" - : `${revision.forRelativeValues.cache.length}/${ + : `${relativeValuesExport.cache.length}/${ definition.items.length * definition.items.length } pairs cached`} {!fullyCached && ( )} {isEmpty ? null : ( )} diff --git a/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesExport.ts b/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesExport.ts new file mode 100644 index 0000000000..670e011d9e --- /dev/null +++ b/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesExport.ts @@ -0,0 +1,25 @@ +import { graphql } from "relay-runtime"; + +// Used in ModelEvaluator and other relative-values functions. +// Since those functions are not components or hooks, we pass around a RelativeValuesExport$data value. +export const RelativeValuesExport = graphql` + fragment RelativeValuesExport on RelativeValuesExport { + id + ...RelativeValuesExportItem + definition { + slug + owner { + slug + } + currentRevision { + ...RelativeValuesDefinitionRevision + } + } + cache { + firstItem + secondItem + resultJSON + errorString + } + } +`; diff --git a/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesModelLayout.tsx b/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesModelLayout.tsx index a3a13ff941..dd960860c6 100644 --- a/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesModelLayout.tsx +++ b/packages/hub/src/app/models/[owner]/[slug]/relative-values/[variableName]/RelativeValuesModelLayout.tsx @@ -1,6 +1,5 @@ "use client"; -import { notFound } from "next/navigation"; import { FC, PropsWithChildren, useEffect, useState } from "react"; import Skeleton from "react-loading-skeleton"; import { graphql, useFragment } from "react-relay"; @@ -28,11 +27,11 @@ import { } from "@/routes"; import { CacheMenu } from "./CacheMenu"; -import { RelativeValuesModelRevisionFragment } from "./RelativeValuesModelRevision"; +import { RelativeValuesExport } from "./RelativeValuesExport"; import { RelativeValuesDefinitionRevision$key } from "@/__generated__/RelativeValuesDefinitionRevision.graphql"; +import { RelativeValuesExport$key } from "@/__generated__/RelativeValuesExport.graphql"; import { RelativeValuesModelLayoutQuery } from "@/__generated__/RelativeValuesModelLayoutQuery.graphql"; -import { RelativeValuesModelRevision$key } from "@/__generated__/RelativeValuesModelRevision.graphql"; export const RelativeValuesModelLayout: FC< PropsWithChildren<{ @@ -58,10 +57,25 @@ export const RelativeValuesModelLayout: FC< slug } currentRevision { + id content { __typename + ... on SquiggleSnippet { + id + code + version + } + } + + forRelativeValues(input: $forRelativeValues) { + __typename + ... on BaseError { + message + } + ... on RelativeValuesExport { + ...RelativeValuesExport + } } - ...RelativeValuesModelRevision } } } @@ -70,25 +84,28 @@ export const RelativeValuesModelLayout: FC< query ); const model = extractFromGraphqlErrorUnion(result, "Model"); - const revision = useFragment( - RelativeValuesModelRevisionFragment, - model.currentRevision - ); + const revision = model.currentRevision; const content = extractFromGraphqlErrorUnion( revision.content, "SquiggleSnippet" ); - if (!revision.forRelativeValues) { - notFound(); - } + const forRelativeValuesKey = extractFromGraphqlErrorUnion( + revision.forRelativeValues, + "RelativeValuesExport" + ); + + const forRelativeValues = useFragment( + RelativeValuesExport, + forRelativeValuesKey + ); - const definition = revision.forRelativeValues.definition; + const definition = forRelativeValues.definition; const definitionRevision = useFragment( RelativeValuesDefinitionRevisionFragment, - revision.forRelativeValues.definition.currentRevision + forRelativeValues.definition.currentRevision ); const [evaluatorResult, setEvaluatorResult] = useState< @@ -97,10 +114,10 @@ export const RelativeValuesModelLayout: FC< useEffect(() => { // ModelEvaluator.create is async because SqProject.run is async - ModelEvaluator.create(content.code, revision.forRelativeValues?.cache).then( + ModelEvaluator.create(content.code, forRelativeValues.cache).then( setEvaluatorResult ); - }, [content.code, revision.forRelativeValues]); + }, [content.code, forRelativeValues]); const body = evaluatorResult ? ( evaluatorResult.ok ? ( @@ -141,7 +158,10 @@ export const RelativeValuesModelLayout: FC<
{definitionLink} - + (items: result[]): A[] => { return items @@ -100,9 +100,7 @@ export class ModelEvaluator { static async create( modelCode: string, - cache?: NonNullable< - RelativeValuesModelRevision$data["forRelativeValues"] - >["cache"] + cache?: RelativeValuesExport$data["cache"] ): Promise> { // TODO - versioned SqProject const project = SqProject.create({