GraphQLDataFactory and order of revert factories

I’m trying to find the answer which would lead to the solution to a problem I’m facing using GraphQLDataFactory:

  1. I have a “users” and “events” table
  2. The “events” table has a “userId” column and a constraint with the “users” table:
    • I can’t delete a record in “users” without first deleting the corresponding records in the “events” table.
    • I can’t enable ON DELETE CASCADE.
  3. I have a factory called createUser to create records in the “users” table.
  4. I have another factory called createUserEvent that receives a user’s ID and a bunch of other data to create a record in the “events” table.
  5. In a test, I create a user and an event
  6. When completing the tests, GraphQLDataFactory runs the revert function for each factory in the same order: this causes a problem because I can’t delete the user without deleting the event first.

There are several solutions I can think of, but none is feasible:

  • As I wrote, I can’t enable ON DELETE CASCADE.
  • I could use a single mutation that creates both the user and the events, then another that deletes users and events in the proper order. However, that looks more like a hack to me, and it would generate a bunch of factories that are too specialised.

A better solution would be to instruct GraphQLDataFactory to run the revert functions in the correct order.

I’m pretty sure I’m not the first and only person with this problem and that the answer might be somewhere out there, but I couldn’t find it yet.

Any suggestion is more than welcome.

Hi @misamee,

I am trying to integrate GraphQLDataFactory helper in automation framework, but I am facing issue in API execution. Looking at above query I guess your basic execution is working, so It will be really helpful if you can help me in resolving my query.

Following is the mutation of API which I am trying to execute:

mutation {
createUser(id:“codeceptjs”,name:“codeceptjs”)
{message}
}

I have added below configurations in codecept.conf.js

GraphQLDataFactory: {
endpoint: “https://test.com/user/graphql”,
cleanup: true,
headers: {
‘Content-Type’: ‘application/json’,
‘Accept’: ‘application/json’,
‘Authorization’ : ‘authToken’
},
factories: {
createUser: {
query: ‘mutation { createUser(input: $input) { id name }}’,
factory: ‘./factories/user’,
revert: (data) => ({
query: ‘mutation deleteUser($id: ID!) { deleteUser(id: $id) }’,
variables: { id : data.id},
}),
},
}
}

But after execution I am getting random console output as displayed in following image and data is not getting generated

Can you please help me understand if anything wrong in my configuration or how can I execute api with GraphQLDataFactory.

Thanks in Advance.