Skip to content

Commit

Permalink
support options.raw=true for relay connections
Browse files Browse the repository at this point in the history
  • Loading branch information
mickhansen committed Dec 17, 2018
1 parent dbba02b commit 3c1a74c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
};

Expand Down Expand Up @@ -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);
},
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion test/integration/relay/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 3c1a74c

Please sign in to comment.