arangodb / arangojs

The official ArangoDB JavaScript driver.
https://arangodb.github.io/arangojs
Apache License 2.0
600 stars 106 forks source link

Nesting of AQL literal does not work #808

Closed robross0606 closed 5 months ago

robross0606 commented 5 months ago

While nesting AQL seems to work and nesting aql literal in aql seems to work, nesting an aql literal inside another aql literal does not work:

const { literal: aqlLiteral } = require('arangojs/aql')

const literal1 = aqlLiteral('myObj')
const literal2 = aqlLiteral(`RETURN ${literal1}`)
const query = aql`
  LET myObj = { test: 'test' }
  ${literal2}
`

Results in the following query:

  LET myObj = { test: 'test' }
  RETURN [object Object]
pluma4345 commented 5 months ago

Yes, that is expected. Note that you're passing an AQL literal into the template string and then passing the resulting string to literal. Unlike a template tag, the function has no control over its input.

However I'm not sure what your use case for literal is. It only exists as an escape hatch for when you need to tag dynamic strings (e.g. from a database or file) as an AQL fragment. In your example every use of aqlLiteral can be replaced with aql.

robross0606 commented 5 months ago

Yeah, I see what you're saying. aqlLiteral() isn't the same as the aql template tag. So the backticks are being evaluated before aqlLiteral is even involved. My bad.