Skip to content

Commit

Permalink
Merge pull request #25 from smartive/fix/delete
Browse files Browse the repository at this point in the history
fix: Delete
  • Loading branch information
dwirz authored Aug 9, 2023
2 parents 25a8c6a + 8bf98a2 commit a61e071
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": ["@smartive/eslint-config"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
"project": "./tsconfig.jest.json"
},
"rules": {
"@typescript-eslint/no-explicit-any": "off",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"deps": "docker-compose up",
"test": "npm run lint && npm run test:unit && npm run test:api && npm run build",
"test": "npm run lint && npm run test:all && npm run build",
"test:all": "jest tests --no-cache --no-watchman",
"test:unit": "jest tests/unit --no-cache --no-watchman",
"test:api": "jest tests/api --no-cache --no-watchman",
"generate-migration": "esbuild tests/utils/generate-migration.ts --bundle --platform=node --outdir=tmp --out-extension:.js=.cjs --format=cjs --packages=external && node tmp/generate-migration.cjs",
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const del = async (model: Model, { where, dryRun }: { where: any; dryRun: boolea
if (entity.id in toDelete[currentModel.name]) {
return;
}
entity[currentModel.displayField || 'id'] || entity.id;
toDelete[currentModel.name][entity.id] = entity[currentModel.displayField || 'id'] || entity.id;

if (!dryRun) {
const normalizedInput = { deleted: true, deletedAt: ctx.now, deletedById: ctx.user.id };
Expand Down
18 changes: 18 additions & 0 deletions tests/api/__snapshots__/delete.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`delete works with self-referential entities 1`] = `
{
"deleteAnotherObject": "226a20e8-5c18-4423-99ca-eb0df6ff4fdd",
}
`;

exports[`delete works with self-referential entities 2`] = `
{
"anotherObjects": [
{
"deleted": true,
"id": "226a20e8-5c18-4423-99ca-eb0df6ff4fdd",
},
],
}
`;
28 changes: 28 additions & 0 deletions tests/api/delete.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { gql } from '../../src';
import { ANOTHER_ID } from '../utils/database/seed';
import { withServer } from '../utils/server';

describe('delete', () => {
it('works with self-referential entities', async () => {
await withServer(async (request) => {
expect(
await request(gql`
mutation DeleteAnotherObject {
deleteAnotherObject(where: { id: "${ANOTHER_ID}" })
}
`)
).toMatchSnapshot();

expect(
await request(gql`
query GetAnotherObject {
anotherObjects(where: { id: "${ANOTHER_ID}", deleted: true }) {
id
deleted
}
}
`)
).toMatchSnapshot();
});
});
});
14 changes: 14 additions & 0 deletions tests/unit/__snapshots__/generate.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
exports[`generate generates a schema 1`] = `
"type AnotherObject {
id: ID!
myself: AnotherObject
deleted: Boolean!
deletedAt: DateTime
deletedBy: User
self(where: AnotherObjectWhere, orderBy: [AnotherObjectOrderBy!], limit: Int, offset: Int): AnotherObject
manyObjects(where: SomeObjectWhere, search: String, orderBy: [SomeObjectOrderBy!], limit: Int, offset: Int): [SomeObject!]!
}
input AnotherObjectOrderBy {
deletedAt: Order
}
input AnotherObjectWhere {
id: [ID!]
deleted: [Boolean!] = false
}
input AnotherObjectWhereUnique {
Expand All @@ -21,6 +31,8 @@ input CreateSomeObject {
scalar DateTime
type Mutation {
deleteAnotherObject(where: AnotherObjectWhereUnique!, dryRun: Boolean): ID!
restoreAnotherObject(where: AnotherObjectWhereUnique!): ID!
createSomeObject(data: CreateSomeObject!): SomeObject!
updateSomeObject(where: SomeObjectWhereUnique!, data: UpdateSomeObject!): SomeObject!
deleteSomeObject(where: SomeObjectWhereUnique!, dryRun: Boolean): ID!
Expand All @@ -35,6 +47,7 @@ enum Order {
type Query {
me: User
someObject(where: SomeObjectWhereUnique!): SomeObject!
anotherObjects(where: AnotherObjectWhere, orderBy: [AnotherObjectOrderBy!], limit: Int, offset: Int): [AnotherObject!]!
manyObjects(where: SomeObjectWhere, search: String, orderBy: [SomeObjectOrderBy!], limit: Int, offset: Int): [SomeObject!]!
}
Expand Down Expand Up @@ -95,6 +108,7 @@ type User {
id: ID!
username: String
role: Role
deletedAnotherObjects(where: AnotherObjectWhere, orderBy: [AnotherObjectOrderBy!], limit: Int, offset: Int): [AnotherObject!]!
createdManyObjects(where: SomeObjectWhere, search: String, orderBy: [SomeObjectOrderBy!], limit: Int, offset: Int): [SomeObject!]!
updatedManyObjects(where: SomeObjectWhere, search: String, orderBy: [SomeObjectOrderBy!], limit: Int, offset: Int): [SomeObject!]!
deletedManyObjects(where: SomeObjectWhere, search: String, orderBy: [SomeObjectOrderBy!], limit: Int, offset: Int): [SomeObject!]!
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/__snapshots__/resolve.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ exports[`resolvers are generated correctly 1`] = `
{
"Mutation": {
"createSomeObject": [Function],
"deleteAnotherObject": [Function],
"deleteSomeObject": [Function],
"restoreAnotherObject": [Function],
"restoreSomeObject": [Function],
"updateSomeObject": [Function],
},
"Query": {
"anotherObjects": [Function],
"manyObjects": [Function],
"me": [Function],
"someObject": [Function],
Expand Down
11 changes: 11 additions & 0 deletions tests/utils/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ export const setupSchema = async (knex: Knex) => {

await knex.schema.createTable('AnotherObject', (table) => {
table.uuid('id').notNullable().primary();
table.uuid('myselfId').notNullable();
table.foreign('myselfId').references('id').inTable('AnotherObject');
table.boolean('deleted').notNullable().defaultTo(false);
table.timestamp('deletedAt').nullable();
table.uuid('deletedById').nullable();
table.foreign('deletedById').references('id').inTable('User');
});

await knex.schema.createTable('AnotherObjectRevision', (table) => {
table.uuid('id').notNullable().primary();
table.boolean('deleted').notNullable();
});

await knex.schema.createTable('SomeObject', (table) => {
table.uuid('id').notNullable().primary();
table.string('field', undefined).nullable();
Expand Down
1 change: 1 addition & 0 deletions tests/utils/database/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const seed = {
AnotherObject: [
{
id: ANOTHER_ID,
myselfId: ANOTHER_ID,
},
],
SomeObject: [
Expand Down
12 changes: 11 additions & 1 deletion tests/utils/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ export const rawModels: RawModels = [
{
type: 'object',
name: 'AnotherObject',
fields: [],
listQueriable: true,
deletable: true,
fields: [
{
type: 'AnotherObject',
name: 'myself',
toOne: true,
reverse: 'self',
relation: true
}
],
},
{
type: 'object',
Expand Down

0 comments on commit a61e071

Please sign in to comment.