Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Expected SQL item" bug when using edge field with order by computed column on mutation payload #740

Closed
benjie opened this issue May 14, 2021 · 2 comments
Labels

Comments

@benjie
Copy link
Member

benjie commented May 14, 2021

Summary

The edge field on mutation payloads throws an error when you attempt to use it with an orderBy that includes a computed column (with @sortable smart tag).

Steps to reproduce

See: https://github.com/pszafer/postgraphile_errors/tree/delete_error

Expected results

No error, correct edge/cursor returned.

Actual results

"Expected SQL item, instead received '({\n        queryBuilder\n      }) => sql.fragment`(${sql.identifier(proc.namespaceName, proc.name)}(${queryBuilder.getTableAlias()}))`'.",

Additional context

PostGraphile v4.11.0.

Possible Solution

The error lies here:

const expr = isString(col)
? sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(
col
)}`
: col;

Specifically we've assumed if it's not a string then it's an SQL node, but that's not the case - it can also be a thunk.

Normally when ordering we call queryBuilder.orderBy with a value like the above:

queryBuilder.orderBy(expr, ascending, nullsFirst);

QueryBuilder automatically calls thunks when generating the compiled data:

} else if (type === "orderBy") {
this.compiledData[type] = this.data[type].map(([a, b, c]) => [
callIfNecessary(a, context),
b,
c,
]);

which we've not done in the edge plugin. This is done because it passes a reference to the context:

lockContext: {
queryBuilder: QueryBuilder,
};

which includes the QueryBuilder so that orders may use things like the query builder's alias whilst building their fragments.

The PgMutationPayloadEdgePlugin does have access to a QueryBuilder so we should be able to fix this by changing the code above to something along the lines of:

const expr = isString(col)
  ? sql.fragment`${queryBuilder.getTableAlias()}.${sql.identifier(col)}`
  : typeof col === 'function'
  ? col({ queryBuilder })
  : col;
@benjie benjie added the bug label May 14, 2021
@benjie
Copy link
Member Author

benjie commented May 14, 2021

@benjie
Copy link
Member Author

benjie commented May 26, 2021

Fixed in #741

@benjie benjie closed this as completed May 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant