How to avoid errors on rapid queries?

Moved from GitHub dgraph-js/36

Posted by good-idea:

I’m setting up some integration tests, and coming across the “Error: Transaction has been aborted. Please retry” error. There’s a function that resets the data I am mutating before and after each test. It works about 80% of the time, but otherwise is throwing the error.

If I put a short async ‘sleep’ delay in the beforeEach/afterEach functions, it works as expected. But, this doesn’t leave me confident that my code can handle requests which might fail because they are submitted in rapid succession. Simply retrying the query once more doesn’t feel good either.

Am I missing something?

Here’s a simplified version of the test.

// @flow
/* eslint-disable no-undef */
import { request } from '../../../__tests__/utils'
import { sampleUser } from '../../../database/stubs/users'
import { updateUser, getUser } from '../UserModel'

const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const resetUserData = async () => {
	await updateUser(sampleUser.uid, { input: sampleUser })

describe('[updateViewerProfile]', () => {
	beforeAll(async () => {
		await resetUserData()
		// await sleep(100) // <-- uncommenting these two gets it working
	afterAll(async () => {
		// await sleep(100) // <--
		await resetUserData()

	it('should update existing data', async () => {
		const newBioLine = 'Test bio line'

		// Note: this is an actual GraphQL mutation that goes through my resolvers.
		// This is not querying dgraph directly.
		const mutation = /* GraphQL */ `
		mutation updateProfile($bioLine: String) {
			updateViewerProfile(input: {bioLine: $bioLine }) {
		const variables = {
			bioLine: newBioLine,
		const context = { viewer: joseph }
		const result = await request(mutation, { variables, context })

gpahal commented :

An aborted error is returned if other transactions concurrently modify the same data that was
modified in this transaction. It is up to the user to retry transactions when they fail. This is more of a dgraph issue rather than the client’s. You can create a new issue in the main dgraph repo.

Similar issues in dgraph:

good-idea commented :

Got it, thanks @gpahal

Also, for anyone coming here concerning dgraph-js, I was simply forgetting to await a txn.commit() in one of the functions used (but not shown) above. :man_facepalming:t3: