-
Notifications
You must be signed in to change notification settings - Fork 17
Add self-relation support when relationsDepthLimit is set #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dmythro
wants to merge
2
commits into
drizzle-team:main
Choose a base branch
from
dmythro:feat/self-relations-with-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,7 +150,8 @@ beforeEach(async () => { | |
| await ctx.db.execute(sql`CREATE TABLE IF NOT EXISTS "posts" ( | ||
| "id" serial PRIMARY KEY NOT NULL, | ||
| "content" text, | ||
| "author_id" integer | ||
| "author_id" integer, | ||
| "parent_id" integer | ||
| );`); | ||
|
|
||
| await ctx.db.execute(sql`CREATE TABLE IF NOT EXISTS "users" ( | ||
|
|
@@ -1605,7 +1606,7 @@ describe.sequential('Query tests', async () => { | |
| x: 20 | ||
| y: 20.3 | ||
| } | ||
| geoTuple: [20, 20.3] | ||
| geoTuple: [20, 20.3] | ||
| } | ||
| ) { | ||
| a | ||
|
|
@@ -4282,4 +4283,312 @@ describe.sequential('__typename with data tests', async () => { | |
| }, | ||
| }); | ||
| }); | ||
|
|
||
| describe('Self-Relations', () => { | ||
| let selfRelCtx: { gql: GraphQLClient }; | ||
| let selfRelServer: any; | ||
|
|
||
| beforeAll(async () => { | ||
| // Build schema with relationsDepthLimit to enable self-relations | ||
| const { schema: gqlSchema, entities } = buildSchema(ctx.db, { relationsDepthLimit: 3 }); | ||
| const yoga = createYoga({ | ||
| schema: gqlSchema, | ||
| }); | ||
| selfRelServer = createServer(yoga); | ||
|
|
||
| const selfRelPort = 4003; | ||
| selfRelServer.listen(selfRelPort); | ||
|
|
||
| const gql = new GraphQLClient(`http://localhost:${selfRelPort}/graphql`); | ||
| selfRelCtx = { gql }; | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| selfRelServer?.close(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| // Add foreign key constraint for self-relation | ||
| await ctx.db.execute(sql`DO $$ BEGIN | ||
| ALTER TABLE "posts" ADD CONSTRAINT "posts_parent_id_posts_id_fk" FOREIGN KEY ("parent_id") REFERENCES "posts"("id") ON DELETE cascade ON UPDATE cascade; | ||
| EXCEPTION | ||
| WHEN duplicate_object THEN null; | ||
| END $$;`); | ||
|
|
||
| // Insert test data with parent-child relationship | ||
| await ctx.db.insert(schema.Posts).values([ | ||
| { id: 1, content: 'Parent post', authorId: 1, parentId: null }, | ||
| { id: 2, content: 'Reply to parent', authorId: 2, parentId: 1 }, | ||
| ]); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await ctx.db.execute(sql`DELETE FROM "posts";`); | ||
| await ctx.db.execute(sql`DO $$ BEGIN | ||
| ALTER TABLE "posts" DROP CONSTRAINT IF EXISTS "posts_parent_id_posts_id_fk"; | ||
| EXCEPTION | ||
| WHEN others THEN null; | ||
| END $$;`); | ||
| }); | ||
|
|
||
| it('Should allow querying self-relations with nested relations', async () => { | ||
| const res = await selfRelCtx.gql.queryGql(/* GraphQL */ ` | ||
| query { | ||
| postsSelectSingle(where: { id: { eq: 2 } }) { | ||
| id | ||
| content | ||
| author { | ||
| id | ||
| name | ||
| } | ||
| parent { | ||
| id | ||
| content | ||
| author { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| expect(res).toStrictEqual({ | ||
| data: { | ||
| postsSelectSingle: { | ||
| id: 2, | ||
| content: 'Reply to parent', | ||
| author: { | ||
| id: 2, | ||
| name: 'SecondUser', | ||
| email: '[email protected]', | ||
| }, | ||
| parent: { | ||
| id: 1, | ||
| content: 'Parent post', | ||
| author: { | ||
| id: 1, | ||
| name: 'FirstUser', | ||
| email: '[email protected]', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Self-Relations Behavior', () => { | ||
| describe('With undefined relationsDepthLimit (default)', () => { | ||
| // Uses the default ctx which has no relationsDepthLimit set | ||
| beforeEach(async () => { | ||
| // Add foreign key constraint for self-relation | ||
| await ctx.db.execute(sql`DO $$ BEGIN | ||
| ALTER TABLE "posts" ADD CONSTRAINT "posts_parent_id_posts_id_fk" FOREIGN KEY ("parent_id") REFERENCES "posts"("id") ON DELETE cascade ON UPDATE cascade; | ||
| EXCEPTION | ||
| WHEN duplicate_object THEN null; | ||
| END $$;`); | ||
|
|
||
| // Insert test data with parent-child relationship | ||
| await ctx.db.insert(schema.Posts).values([ | ||
| { id: 1, content: 'Parent post', authorId: 1, parentId: null }, | ||
| { id: 2, content: 'Reply to parent', authorId: 2, parentId: 1 }, | ||
| ]); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await ctx.db.execute(sql`DELETE FROM "posts";`); | ||
| await ctx.db.execute(sql`DO $$ BEGIN | ||
| ALTER TABLE "posts" DROP CONSTRAINT IF EXISTS "posts_parent_id_posts_id_fk"; | ||
| EXCEPTION | ||
| WHEN others THEN null; | ||
| END $$;`); | ||
| }); | ||
|
|
||
| it('Should block self-relations when relationsDepthLimit is undefined', async () => { | ||
| // Query that would work if self-relations were allowed | ||
| const res = await ctx.gql.queryGql(/* GraphQL */ ` | ||
| query { | ||
| postsSelectSingle(where: { id: { eq: 2 } }) { | ||
| id | ||
| content | ||
| author { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| // Should work for non-self relations (author) | ||
| expect(res.data.postsSelectSingle.author).toBeDefined(); | ||
| expect(res.data.postsSelectSingle.author.id).toBe(2); | ||
| expect(res.data.postsSelectSingle.author.name).toBe('SecondUser'); | ||
|
|
||
| // But parent field should not be available in the schema due to usedTables check | ||
| // Let's verify the schema doesn't include parent relation | ||
| const schemaRes = await ctx.gql.queryGql(/* GraphQL */ ` | ||
| query { | ||
| __type(name: "PostsSelectItem") { | ||
| fields { | ||
| name | ||
| type { | ||
| name | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| const fieldNames = schemaRes.data.__type.fields.map((field: any) => field.name); | ||
| expect(fieldNames).toContain('author'); // Should have author relation | ||
| expect(fieldNames).not.toContain('parent'); // Should NOT have parent relation due to usedTables check | ||
| expect(fieldNames).not.toContain('replies'); // Should NOT have replies relation due to usedTables check | ||
| }); | ||
| }); | ||
|
|
||
| describe('With defined relationsDepthLimit', () => { | ||
| let selfRelCtx: { gql: GraphQLClient }; | ||
| let selfRelServer: any; | ||
|
|
||
| beforeAll(async () => { | ||
| // Build schema with relationsDepthLimit to enable self-relations | ||
| const { schema: gqlSchema, entities } = buildSchema(ctx.db, { relationsDepthLimit: 3 }); | ||
| const yoga = createYoga({ | ||
| schema: gqlSchema, | ||
| }); | ||
| selfRelServer = createServer(yoga); | ||
|
|
||
| const selfRelPort = 4003; | ||
| selfRelServer.listen(selfRelPort); | ||
|
|
||
| const gql = new GraphQLClient(`http://localhost:${selfRelPort}/graphql`); | ||
| selfRelCtx = { gql }; | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| selfRelServer?.close(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| // Add foreign key constraint for self-relation | ||
| await ctx.db.execute(sql`DO $$ BEGIN | ||
| ALTER TABLE "posts" ADD CONSTRAINT "posts_parent_id_posts_id_fk" FOREIGN KEY ("parent_id") REFERENCES "posts"("id") ON DELETE cascade ON UPDATE cascade; | ||
| EXCEPTION | ||
| WHEN duplicate_object THEN null; | ||
| END $$;`); | ||
|
|
||
| // Insert test data with parent-child relationship | ||
| await ctx.db.insert(schema.Posts).values([ | ||
| { id: 1, content: 'Parent post', authorId: 1, parentId: null }, | ||
| { id: 2, content: 'Reply to parent', authorId: 2, parentId: 1 }, | ||
| ]); | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| await ctx.db.execute(sql`DELETE FROM "posts";`); | ||
| await ctx.db.execute(sql`DO $$ BEGIN | ||
| ALTER TABLE "posts" DROP CONSTRAINT IF EXISTS "posts_parent_id_posts_id_fk"; | ||
| EXCEPTION | ||
| WHEN others THEN null; | ||
| END $$;`); | ||
| }); | ||
|
|
||
| it('Should allow self-relations when relationsDepthLimit is defined', async () => { | ||
| // First, verify the schema includes self-relations when depth limit is set | ||
| const schemaRes = await selfRelCtx.gql.queryGql(/* GraphQL */ ` | ||
| query { | ||
| __type(name: "PostsSelectItem") { | ||
| fields { | ||
| name | ||
| type { | ||
| name | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| const fieldNames = schemaRes.data.__type.fields.map((field: any) => field.name); | ||
| expect(fieldNames).toContain('author'); // Should have author relation | ||
| expect(fieldNames).toContain('parent'); // Should have parent relation when depth limit is set | ||
| expect(fieldNames).toContain('replies'); // Should have replies relation when depth limit is set | ||
| }); | ||
|
|
||
| it('Should allow querying self-relations with nested relations (post.parent.author)', async () => { | ||
| const res = await selfRelCtx.gql.queryGql(/* GraphQL */ ` | ||
| query { | ||
| postsSelectSingle(where: { id: { eq: 2 } }) { | ||
| id | ||
| content | ||
| author { | ||
| id | ||
| name | ||
| } | ||
| parent { | ||
| id | ||
| content | ||
| author { | ||
| id | ||
| name | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| expect(res).toStrictEqual({ | ||
| data: { | ||
| postsSelectSingle: { | ||
| id: 2, | ||
| content: 'Reply to parent', | ||
| author: { | ||
| id: 2, | ||
| name: 'SecondUser', | ||
| email: '[email protected]', | ||
| }, | ||
| parent: { | ||
| id: 1, | ||
| content: 'Parent post', | ||
| author: { | ||
| id: 1, | ||
| name: 'FirstUser', | ||
| email: '[email protected]', | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('Should respect depth limit even with self-relations', async () => { | ||
| // Try to query beyond the depth limit (depth=3: post->parent->author->posts would be depth 4) | ||
| const res = await selfRelCtx.gql.queryGql(/* GraphQL */ ` | ||
| query { | ||
| postsSelectSingle(where: { id: { eq: 2 } }) { | ||
| id | ||
| parent { | ||
| id | ||
| author { | ||
| id | ||
| posts { | ||
| id | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| `); | ||
|
|
||
| // Should work up to depth 3 but posts under author should be empty due to depth limit | ||
| expect(res.data.postsSelectSingle.parent.author.id).toBe(1); | ||
| expect(res.data.postsSelectSingle.parent.author.posts).toEqual([]); // Depth limit prevents further traversal | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.