Ordering nodes with Prisma




I’m a bit confused about how to deal with lists of nodes that needs to have an order.

For example, having this schema:

type Course {
  id: ID! @unique
  sections: [Section!]!

type Section {
  id: ID! @unique

What would be the best way to write the resolvers for operations like create/update and delete a Section? I guess an index field will be needed in order to set a position for each Section type and, any time we want to run any operation said before, we’ll need to update ALL the sections that are into each Course.

A little guidance will be appreciated!


Not sure whether you’re using the graphcool framework or Prisma

Start by reading this:

Prisma docs - ordering by field


I’m using Prisma.

I already know about the orderBy argument when sending queries. It’ll be useful when getting the results.

What I’m a bit confused is about how to deal with the mutations for creating/updating and deleting this sorted nodes. The idea is that the user can drag & drop the Card in the order they want.

For example, if I delete the first node in the list, I’ll have to update all the nodes to set their index-1. Maybe a batch mutation can help in this operation.

Not sure if there is a better way!


Oh I understand your situation better now.

I imagine creating an index attribute on your objects is the way to go. Some what cumbersome tho but better than nothing


For anyone interested, I ended up writing this resolver to support nodes sorting:

async moveCourseSection(parent, { id, sectionId, position }, ctx, info) {
	// Get all sections that we'll need to increase their position
	const sectionsToMove = await ctx.db.query.courseSections({
		orderBy: 'index_ASC',
		where: {
			index_gte: position,
			id_not: sectionId,
			course: {
	}, `{

	const sectionsToMoveMutations = [];
	for (const section of sectionsToMove) {
					data: { index: section.index + 1 },
					where: { id: section.id }

	// Update all sections in parallel
	await Promise.all(sectionsToMoveMutations);

	// Update current section to its new position
	await ctx.db.mutation.updateCourseSection(
			data: { index: position },
			where: { id: sectionId }

	return ctx.db.query.course({ where: { id } }, info);