diff --git a/api/src/database/run-ast.ts b/api/src/database/run-ast.ts index 8c37baba7e..e600d0d9e7 100644 --- a/api/src/database/run-ast.ts +++ b/api/src/database/run-ast.ts @@ -148,18 +148,10 @@ async function parseCurrentLevel( for (const child of children) { if (child.type === 'field') { - const fieldKey = stripFunction(child.name); + const fieldName = stripFunction(child.name); - if (columnsInCollection.includes(fieldKey) || fieldKey === '*') { - columnsToSelectInternal.push(child.name); // maintain original name here (includes functions) - - if (query.alias) { - columnsToSelectInternal.push( - ...Object.entries(query.alias) - .filter(([_key, value]) => value === child.name) - .map(([key]) => key) - ); - } + if (columnsInCollection.includes(fieldName)) { + columnsToSelectInternal.push(child.fieldKey); } continue; @@ -193,9 +185,7 @@ async function parseCurrentLevel( const fieldNodes = columnsToSelect.map( (column: string) => - children.find( - (childNode) => childNode.type === 'field' && (childNode.fieldKey === column || childNode.name === column) - ) ?? { + children.find((childNode) => childNode.type === 'field' && childNode.fieldKey === column) ?? { type: 'field', name: column, fieldKey: column, @@ -209,6 +199,12 @@ function getColumnPreprocessor(knex: Knex, schema: SchemaOverview, table: string const helpers = getHelpers(knex); return function (fieldNode: FieldNode | M2ONode): Knex.Raw { + let alias = undefined; + + if (fieldNode.name !== fieldNode.fieldKey) { + alias = fieldNode.fieldKey; + } + let field; if (fieldNode.type === 'field') { @@ -217,13 +213,7 @@ function getColumnPreprocessor(knex: Knex, schema: SchemaOverview, table: string field = schema.collections[fieldNode.relation.collection].fields[fieldNode.relation.field]; } - let alias = undefined; - - if (fieldNode.name !== fieldNode.fieldKey) { - alias = fieldNode.fieldKey; - } - - if (field.type.startsWith('geometry')) { + if (field?.type?.startsWith('geometry')) { return helpers.st.asText(table, field.field); } diff --git a/api/src/services/graphql.ts b/api/src/services/graphql.ts index 2792989b26..2d36b37e81 100644 --- a/api/src/services/graphql.ts +++ b/api/src/services/graphql.ts @@ -1453,6 +1453,7 @@ export class GraphQLService { // add nested aliases into deep query if (selection.selectionSet) { if (!query.deep) query.deep = {}; + set( query.deep, parent, @@ -1492,9 +1493,9 @@ export class GraphQLService { set( query.deep, - current, + currentAlias ?? current, merge( - get(query.deep, current), + get(query.deep, currentAlias ?? current), mapKeys(sanitizeQuery(args, this.accountability), (value, key) => `_${key}`) ) ); diff --git a/api/src/utils/get-ast-from-query.ts b/api/src/utils/get-ast-from-query.ts index e39afd0fd5..b243f8b858 100644 --- a/api/src/utils/get-ast-from-query.ts +++ b/api/src/utils/get-ast-from-query.ts @@ -119,12 +119,6 @@ export default async function getASTFromQuery( if (name in query.alias) { name = query.alias[fieldKey]; } - - // check for junction alias (it is one of the value instead of the key) - if (Object.values(query.alias).includes(name)) { - const aliasKey = Object.keys(query.alias).find((key) => query.alias?.[key] === name); - if (aliasKey && fieldKey !== aliasKey) name = aliasKey; - } } const isRelational = @@ -137,7 +131,7 @@ export default async function getASTFromQuery( if (isRelational) { // field is relational - const parts = name.split('.'); + const parts = fieldKey.split('.'); let rootField = parts[0]; let collectionScope: string | null = null;