Skip to content

Commit

Permalink
add sqlastnode arg to sqlJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Carlson committed Aug 11, 2017
1 parent 54d8d82 commit 98a16f3
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ Function for generating a `JOIN` condition.
| childTable | <code>String</code> | The alias for the child's table. Already double-quoted. |
| args | <code>Object</code> | The GraphQL arguments for this field. |
| context | <code>Object</code> | An Object with arbitrary contextual information. |
| sqlASTNode | <code>Object</code> | Join Monster object that abstractly represents this field. Also includes a reference to its parent node. This is useful, for example, if you need to access the parent field's table alias or GraphQL arguments. |

<a name="thunk"></a>

Expand Down
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v2.0.6 (Aug. 11, 2017)
- Add SQL AST node to sqlJoin callback signature.

### v2.0.5 (Aug. 11, 2017)
- Remove the use of `Proxy` to improve compatibility.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "join-monster",
"version": "2.0.5",
"version": "2.0.6",
"description": "A GraphQL to SQL query execution layer for batch data fetching.",
"main": "dist/index.js",
"engines": {
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import { buildWhereFunction, handleUserDbCall, compileSqlAST } from './util'
* @param {String} childTable - The alias for the child's table. Already double-quoted.
* @param {Object} args - The GraphQL arguments for this field.
* @param {Object} context - An Object with arbitrary contextual information.
* @param {Object} sqlASTNode - Join Monster object that abstractly represents this field. Also includes a reference to its parent node. This is useful, for example, if you need to access the parent field's table alias or GraphQL arguments.
* @returns {String} The RAW condition for the `LEFT JOIN`. Unsafe user input must be scrubbed.
*/
/**
Expand Down
28 changes: 22 additions & 6 deletions src/stringifiers/dialects/oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const dialect = module.exports = {

handleJoinedOneToManyPaginated: async function(parent, node, context, tables, joinCondition) {
const pagingWhereConditions = [
await node.sqlJoin(`"${parent.as}"`, q(node.as), node.args || {}, context)
await node.sqlJoin(`"${parent.as}"`, q(node.as), node.args || {}, context, node)
]
if (node.where) {
pagingWhereConditions.push(await node.where(`"${node.as}"`, node.args || {}, context, node))
Expand All @@ -103,10 +103,18 @@ const dialect = module.exports = {
if (node.sortKey) {
const { limit, order, whereCondition: whereAddendum } = interpretForKeysetPaging(node, dialect)
pagingWhereConditions.push(whereAddendum)
tables.push(keysetPagingSelect(node.name, pagingWhereConditions, order, limit, node.as, { joinCondition, joinType: 'LEFT' }))
tables.push(
keysetPagingSelect(node.name, pagingWhereConditions, order, limit, node.as, {
joinCondition, joinType: 'LEFT'
})
)
} else if (node.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, { joinCondition, joinType: 'LEFT' }))
tables.push(
offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, {
joinCondition, joinType: 'LEFT'
})
)
}
},

Expand Down Expand Up @@ -142,7 +150,10 @@ const dialect = module.exports = {
} else if (node.orderBy || node.junction.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(
offsetPagingSelect(node.junction.sqlTable, pagingWhereConditions, order, limit, offset, node.junction.as, lateralJoinOptions)
offsetPagingSelect(
node.junction.sqlTable, pagingWhereConditions, order,
limit, offset, node.junction.as, lateralJoinOptions
)
)
}
},
Expand All @@ -166,7 +177,9 @@ const dialect = module.exports = {
)
} else if (node.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, { joinCondition: lateralJoinCondition }))
tables.push(offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, {
joinCondition: lateralJoinCondition
}))
}
},

Expand Down Expand Up @@ -203,7 +216,10 @@ const dialect = module.exports = {
} else if (node.orderBy || node.junction.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(
offsetPagingSelect(node.junction.sqlTable, pagingWhereConditions, order, limit, offset, node.junction.as, lateralJoinOptions)
offsetPagingSelect(
node.junction.sqlTable, pagingWhereConditions, order,
limit, offset, node.junction.as, lateralJoinOptions
)
)
}
tables.push(`LEFT JOIN ${node.name} "${node.as}" ON ${joinCondition}`)
Expand Down
20 changes: 15 additions & 5 deletions src/stringifiers/dialects/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const dialect = module.exports = {

handleJoinedOneToManyPaginated: async function(parent, node, context, tables, joinCondition) {
const pagingWhereConditions = [
await node.sqlJoin(`"${parent.as}"`, `"${node.as}"`, node.args || {}, context)
await node.sqlJoin(`"${parent.as}"`, `"${node.as}"`, node.args || {}, context, node)
]
if (node.where) {
pagingWhereConditions.push(
Expand All @@ -37,7 +37,9 @@ const dialect = module.exports = {
} else if (node.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(
offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, { joinCondition, joinType: 'LEFT' })
offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, {
joinCondition, joinType: 'LEFT'
})
)
}
},
Expand Down Expand Up @@ -78,7 +80,10 @@ const dialect = module.exports = {
} else if (node.orderBy || node.junction.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(
offsetPagingSelect(node.junction.sqlTable, pagingWhereConditions, order, limit, offset, node.junction.as, lateralJoinOptions)
offsetPagingSelect(
node.junction.sqlTable, pagingWhereConditions, order,
limit, offset, node.junction.as, lateralJoinOptions
)
)
}
tables.push(`LEFT JOIN ${node.name} AS "${node.as}" ON ${joinCondition}`)
Expand Down Expand Up @@ -116,7 +121,10 @@ const dialect = module.exports = {
} else if (node.orderBy || node.junction.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(
offsetPagingSelect(node.junction.sqlTable, pagingWhereConditions, order, limit, offset, node.junction.as, lateralJoinOptions)
offsetPagingSelect(
node.junction.sqlTable, pagingWhereConditions, order,
limit, offset, node.junction.as, lateralJoinOptions
)
)
}
},
Expand Down Expand Up @@ -168,7 +176,9 @@ const dialect = module.exports = {
} else if (node.orderBy) {
const { limit, offset, order } = interpretForOffsetPaging(node, dialect)
tables.push(
offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, { joinCondition: lateralJoinCondition })
offsetPagingSelect(node.name, pagingWhereConditions, order, limit, offset, node.as, {
joinCondition: lateralJoinCondition
})
)
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/stringifiers/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async function handleTable(parent, node, prefix, context, selections, tables, wh

// one-to-many using JOIN
if (node.sqlJoin) {
const joinCondition = await node.sqlJoin(`${q(parent.as)}`, q(node.as), node.args || {}, context)
const joinCondition = await node.sqlJoin(`${q(parent.as)}`, q(node.as), node.args || {}, context, node)

// do we need to paginate? if so this will be a lateral join
if (node.paginate) {
Expand All @@ -193,7 +193,9 @@ async function handleTable(parent, node, prefix, context, selections, tables, wh
`${q(parent.as)}.${q(node.junction.sqlBatch.parentKey.name)} AS ${q(joinPrefix(prefix) + node.junction.sqlBatch.parentKey.as)}`
)
} else {
const joinCondition = await node.junction.sqlBatch.sqlJoin(`${q(node.junction.as)}`, q(node.as), node.args || {}, context)
const joinCondition = await node.junction.sqlBatch.sqlJoin(
`${q(node.junction.as)}`, q(node.as), node.args || {}, context, node
)
if (node.paginate) {
await dialect.handleBatchedManyToManyPaginated(parent, node, context, tables, batchScope, joinCondition)
} else if (node.limit) {
Expand Down

0 comments on commit 98a16f3

Please sign in to comment.