From 3c1a74c83975f102346989a4e6e2fe3d2725cfa0 Mon Sep 17 00:00:00 2001 From: Mick Hansen Date: Mon, 17 Dec 2018 15:58:14 +0100 Subject: [PATCH] support options.raw=true for relay connections --- src/relay.js | 12 ++++++++---- test/integration/relay/connection.test.js | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/relay.js b/src/relay.js index 71364125..6c07d2d6 100644 --- a/src/relay.js +++ b/src/relay.js @@ -143,13 +143,15 @@ export function createConnectionResolver({ /** * Creates a cursor given a item returned from the Database - * @param {Object} item sequelize model instance + * @param {Object} item sequelize row * @param {Integer} index the index of this item within the results, 0 indexed * @return {String} The Base64 encoded cursor string */ let toCursor = function (item, index) { - const {primaryKeyAttribute} = getModelOfInstance(item); - const id = typeof primaryKeyAttribute === 'string' ? item.get(primaryKeyAttribute) : null; + const model = getModelOfInstance(item); + const id = model ? + typeof model.primaryKeyAttribute === 'string' ? item[model.primaryKeyAttribute] : null : + item[Object.keys(item)[0]]; return base64(JSON.stringify([id, index])); }; @@ -261,6 +263,8 @@ export function createConnectionResolver({ if (startIndex >= 0) options.offset = startIndex + 1; } + + options.attributes.unshift(model.primaryKeyAttribute); // Ensure the primary key is always the first selected attribute options.attributes = _.uniq(options.attributes); return before(options, args, context, info); }, @@ -282,7 +286,7 @@ export function createConnectionResolver({ let firstEdge = edges[0]; let lastEdge = edges[edges.length - 1]; - let fullCount = values[0] && values[0].dataValues.full_count && parseInt(values[0].dataValues.full_count, 10); + let fullCount = values[0] && (values[0].dataValues || values[0]).full_count && parseInt((values[0].dataValues || values[0]).full_count, 10); if (!values[0]) { fullCount = 0; diff --git a/test/integration/relay/connection.test.js b/test/integration/relay/connection.test.js index 833ae078..a4984ca7 100644 --- a/test/integration/relay/connection.test.js +++ b/test/integration/relay/connection.test.js @@ -124,6 +124,7 @@ describe('relay', function () { } }), before: (options) => { + options.raw = true; if (options.order && options.order[0][0] === 'updatedAt') { if (sequelize.dialect.name === 'postgres') { options.order = Sequelize.literal(` @@ -582,7 +583,7 @@ describe('relay', function () { `, null, {}); if (result.errors) throw new Error(result.errors[0]); - + const node = result.data.user.projects2.edges[0].node; expect(+fromGlobalId(node.id).id).to.equal(5); });