Skip to content

Commit

Permalink
minor cleanups for QueryBuilderState.initalize()
Browse files Browse the repository at this point in the history
  • Loading branch information
akphi committed Sep 12, 2022
1 parent 36be642 commit 7728881
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ test(

// simpleProjection
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__simpleProjection.parameters,
TEST_DATA__simpleProjection.body,
Expand Down Expand Up @@ -167,7 +167,7 @@ test(
// chainedProperty
const CHAINED_PROPERTY_ALIAS = 'Firm/Legal Name';
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__projectionWithChainedProperty.parameters,
TEST_DATA__projectionWithChainedProperty.body,
Expand Down Expand Up @@ -214,7 +214,7 @@ test(
// result set modifiers
const RESULT_LIMIT = 500;
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__projectionWithResultSetModifiers.parameters,
TEST_DATA__projectionWithResultSetModifiers.body,
Expand Down Expand Up @@ -292,7 +292,7 @@ test(
// filter with simple condition
await waitFor(() => renderResult.getByText('Add a filter condition'));
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__getAllWithOneConditionFilter.parameters,
TEST_DATA__getAllWithOneConditionFilter.body,
Expand Down Expand Up @@ -322,7 +322,7 @@ test(
});
await waitFor(() => renderResult.getByText('Add a filter condition'));
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__getAllWithGroupedFilter.parameters,
TEST_DATA__getAllWithGroupedFilter.body,
Expand Down Expand Up @@ -356,7 +356,7 @@ test(
});
await waitFor(() => renderResult.getByText('Add a filter condition'));
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__projectWithDerivedProperty.parameters,
TEST_DATA__projectWithDerivedProperty.body,
Expand Down Expand Up @@ -440,7 +440,7 @@ test(

// simpleProjection with subType
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__simpleProjectionWithSubtypeFromSubtypeModel.parameters,
TEST_DATA__simpleProjectionWithSubtypeFromSubtypeModel.body,
Expand Down Expand Up @@ -493,7 +493,7 @@ test(

// simple graph fetch
act(() => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__simpleGraphFetch.parameters,
TEST_DATA__simpleGraphFetch.body,
Expand All @@ -503,7 +503,7 @@ test(

// switch to complex graph fetch
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(
TEST_DATA__complexGraphFetch.parameters,
TEST_DATA__complexGraphFetch.body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe(integrationTest('Build property mapping data'), () => {
getByText(setupPanel, extractElementNameFromPath(runtimePath)),
);
await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(rawLambda.parameters, rawLambda.body),
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe(integrationTest('Query builder milestoning'), () => {
});

await act(async () => {
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
create_RawLambda(rawLambda.parameters, rawLambda.body),
);
});
Expand Down
32 changes: 2 additions & 30 deletions packages/legend-application-query/src/stores/QueryEditorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ import {
guaranteeNonNullable,
ActionState,
StopWatch,
getNullableFirstElement,
guaranteeType,
} from '@finos/legend-shared';
import {
type LightQuery,
type RawLambda,
GraphManagerState,
getAllClassMappings,
toLightQuery,
Query,
PureExecution,
Expand All @@ -49,7 +47,6 @@ import {
type GraphBuilderReport,
GraphManagerTelemetry,
extractElementNameFromPath,
isSystemElement,
Mapping,
type Runtime,
} from '@finos/legend-graph';
Expand Down Expand Up @@ -469,31 +466,6 @@ export class MappingQueryCreatorStore extends QueryEditorStore {
),
);

// try to find a class to set
// first, find classes which is mapped by the mapping
// then, find any classes except for class coming from system
// if none found, default to a dummy blank query
const defaultClass =
getNullableFirstElement(
queryBuilderState.mapping
? getAllClassMappings(queryBuilderState.mapping).map(
(classMapping) => classMapping.class.value,
)
: [],
) ??
getNullableFirstElement(
queryBuilderState.graphManagerState.graph.classes.filter(
(el) => !isSystemElement(el),
),
);
if (defaultClass) {
queryBuilderState.changeClass(defaultClass);
} else {
queryBuilderState.initialize(
this.graphManagerState.graphManager.createDefaultBasicRawLambda(),
);
}

return queryBuilderState;
}

Expand Down Expand Up @@ -570,7 +542,7 @@ export class ServiceQueryCreatorStore extends QueryEditorStore {
);

// leverage initialization of query builder state to ensure we handle unsupported queries
queryBuilderState.initialize(service.execution.func);
queryBuilderState.initializeWithQuery(service.execution.func);

return queryBuilderState;
}
Expand Down Expand Up @@ -664,7 +636,7 @@ export class ExistingQueryEditorStore extends QueryEditorStore {
);

// leverage initialization of query builder state to ensure we handle unsupported queries
queryBuilderState.initialize(
queryBuilderState.initializeWithQuery(
await this.graphManagerState.graphManager.pureCodeToLambda(query.content),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ export abstract class QueryBuilderState {
changeClass: action,
changeMapping: action,

initializeWithQuery: flow,
rebuildWithQuery: action,
compileQuery: flow,
saveQuery: action,
compileQuery: flow,
});

this.applicationStore = applicationStore;
Expand Down Expand Up @@ -258,7 +259,27 @@ export abstract class QueryBuilderState {
this.setRuntimeValue(val);
}

initialize(rawLambda: RawLambda, options?: { notifyError: boolean }): void {
buildQuery(options?: { keepSourceInformation: boolean }): RawLambda {
if (!this.isQuerySupported) {
const parameters = this.parametersState.parameterStates.map((e) =>
this.graphManagerState.graphManager.serializeValueSpecification(
e.parameter,
),
);
this.unsupportedQueryState.setRawLambda(
new RawLambda(parameters, this.unsupportedQueryState.rawLambda?.body),
);
return guaranteeNonNullable(this.unsupportedQueryState.rawLambda);
}
return buildRawLambdaFromLambdaFunction(
buildLambdaFunction(this, {
keepSourceInformation: Boolean(options?.keepSourceInformation),
}),
this.graphManagerState,
);
}

initializeWithQuery(rawLambda: RawLambda): void {
try {
this.rebuildWithQuery(rawLambda);
if (this.parametersState.parameterStates.length > 0) {
Expand All @@ -280,32 +301,7 @@ export abstract class QueryBuilderState {
)
.filter(filterByType(VariableExpression));
processParameters(parameters, this);
if (options?.notifyError) {
this.applicationStore.notifyError(
`Can't initialize query builder: ${error.message}`,
);
}
}
}

buildQuery(options?: { keepSourceInformation: boolean }): RawLambda {
if (!this.isQuerySupported) {
const parameters = this.parametersState.parameterStates.map((e) =>
this.graphManagerState.graphManager.serializeValueSpecification(
e.parameter,
),
);
this.unsupportedQueryState.setRawLambda(
new RawLambda(parameters, this.unsupportedQueryState.rawLambda?.body),
);
return guaranteeNonNullable(this.unsupportedQueryState.rawLambda);
}
return buildRawLambdaFromLambdaFunction(
buildLambdaFunction(this, {
keepSourceInformation: Boolean(options?.keepSourceInformation),
}),
this.graphManagerState,
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class QueryBuilderTextEditorState extends LambdaEditorState {
`Can't parse query. Please fix error before closing: ${this.parserError.message}`,
);
} else {
this.queryBuilderState.initialize(this.rawLambdaState.lambda);
this.queryBuilderState.initializeWithQuery(this.rawLambdaState.lambda);
this.setMode(undefined);
}
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export const MappingExecutionQueryBuilder = observer(
queryBuilderExtension.editorStore.applicationStore,
queryBuilderExtension.editorStore.graphManagerState,
);
queryBuilderState.initialize(executionState.queryState.query);
queryBuilderState.initializeWithQuery(
executionState.queryState.query,
);
queryBuilderState.changeDetectionState.setQueryHashCode(
hashObject(executionState.queryState.query),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const MappingTestQueryBuilder = observer(
queryBuilderExtension.editorStore.applicationStore,
queryBuilderExtension.editorStore.graphManagerState,
);
queryBuilderState.initialize(testState.queryState.query);
queryBuilderState.initializeWithQuery(testState.queryState.query);
queryBuilderState.changeDetectionState.setQueryHashCode(
hashObject(testState.queryState.query),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export const ServiceQueryBuilder = observer(
? selectedExecutionState.executionContext.key
: undefined,
);
queryBuilderState.initialize(executionState.execution.func);
queryBuilderState.initializeWithQuery(
executionState.execution.func,
);
queryBuilderState.changeDetectionState.setQueryHashCode(
hashObject(executionState.execution.func),
);
Expand Down

0 comments on commit 7728881

Please sign in to comment.