Open abhi-dasgupta opened 4 years ago
Thanks for reporting! Will definitely check this for the new book edition 👍
so I removed the graphql-iso-date package and used String scalar, with momentjs to format it
import moment from "moment";
import Sequelize from "sequelize";
const toCursorString = string => Buffer.from(string).toString("base64");
const fromCursorString = string =>
Buffer.from(string, "base64").toString("ascii");
export default {
Query: {
messages: async (parent, { limit = 100, cursor }, { models }) => {
const cursorOptions = cursor
? {
where: {
createdAt: {
[Sequelize.Op.lt]: fromCursorString(cursor)
}
}
}
: null;
const messages = await models.Message.findAll({
order: [["createdAt", "DESC"]],
limit: limit + 1,
...cursorOptions
});
const hasNextPage = messages.length > limit;
const edges = hasNextPage ? messages.slice(0, -1) : messages;
return {
edges,
pageInfo: {
hasNextPage,
endCursor: toCursorString(
moment(edges[edges.length - 1].createdAt)
.local()
.format()
)
}
};
}
},
Message: {
createdAt: message => {
return moment
.utc(message.createdAt)
.local()
.format();
}
}
};
Oh thanks! Maybe I will consider this for the book if it doesn't work out with the Scalar. I found the custom Scalar interesting for readers for the sake of learning about it :)
ok, thank you for your consideration. I have another small suggestion for this topic, In the function to seed the database for development, instead of setting +1 sec to the date, it will make more sense if we did -1 sec. If we do +1 sec then the createdAt is always greater than updatedAt. Not an issue at all as it will not hold true in production, but I was writing a client-side app simultaneously and faced some issues with logic while doing +1 sec
Oh yeah, makes totally sense! Thanks for all the feedback :)
First of all, thank you so much for your awesome books and other resources! I am on Road To GraphQL now! Pagination part
I am getting below mentioned depreciation warning, while fromCursorHash is called. Everything works fine, but I want to know what is causing this warning, can't figure. Anyone else?
Also, I am eagerly waiting for the 2020 update of this book. Thank you again :)