Skip to content

Commit

Permalink
feat(aliases.spec.ts): update aliases in test queries to use 'job' in…
Browse files Browse the repository at this point in the history
…stead of 'jobAd' and 'company' instead of 'publisherCompany' for improved clarity and consistency

feat(aliases.spec.ts): refactor test queries to use 'findJobs' instead of 'findJobApplications' for better naming and semantics
fix(index.ts): add validation to prevent duplicate operation names in queryMap to avoid conflicts and improve data integrity
  • Loading branch information
danil-iglu committed Sep 18, 2024
1 parent a8843b0 commit 8a183a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
57 changes: 26 additions & 31 deletions src/__tests__/aliases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ import { getAllowedQueryForRequest } from '../get-allowed-query';
import { mergeQueries } from '../merge';

const allowedQueries = {
'FindMyTalentJobApplications.findJobApplications': `query FindMyTalentJobApplications {
data: findJobApplications {
'FindMyJobs.findJobs': `query FindMyJobs {
data: findJobs {
id
createdAt
deletedAt
jobAd {
job {
id
location
title
publisherCompany {
company {
name
}
workMode
}
}
}`,
'FindMyCompanyTalentJobApplications.findJobApplications': `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
data: findJobApplications(where: $where, orderBy: $orderBy) {
'FindMyCompanyTalentJobApplications.findJobs': `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
data: findJobs(where: $where, orderBy: $orderBy) {
createdAt
id
jobAd {
job {
title
}
talentProfile {
Expand All @@ -33,17 +32,16 @@ const allowedQueries = {
};

describe('aliases', () => {
test('FindMyTalentJobApplications should handle aliases (request talentProfile when it is not allowed)', () => {
const requestQuery = `query FindMyTalentJobApplications {
data: findJobApplications {
test('FindMyJobs should handle aliases (request talentProfile when it is not allowed)', () => {
const requestQuery = `query FindMyJobs {
data: findJobs {
id
createdAt
deletedAt
jobAd {
job {
id
location
title
publisherCompany {
company {
name
}
workMode
Expand All @@ -54,16 +52,15 @@ describe('aliases', () => {
}
}`;

const expected = `query FindMyTalentJobApplications {
data: findJobApplications {
const expected = `query FindMyJobs {
data: findJobs {
id
createdAt
deletedAt
jobAd {
job {
id
location
title
publisherCompany {
company {
name
}
workMode
Expand All @@ -79,10 +76,10 @@ describe('aliases', () => {

test('FindMyCompanyTalentJobApplications should handle aliases2 (request workMode when it is not allowed)', () => {
const requestQuery = `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
data: findJobApplications(where: $where, orderBy: $orderBy) {
data: findJobs(where: $where, orderBy: $orderBy) {
createdAt
id
jobAd {
job {
title
__typename
}
Expand All @@ -95,10 +92,10 @@ describe('aliases', () => {
}
}`;
const expected = `query FindMyCompanyTalentJobApplications($where: TalentJobApplicationWhereInput, $orderBy: [TalentJobApplicationOrderByWithRelationInput!]) {
data: findJobApplications(where: $where, orderBy: $orderBy) {
data: findJobs(where: $where, orderBy: $orderBy) {
createdAt
id
jobAd {
job {
title
}
talentProfile {
Expand All @@ -115,23 +112,21 @@ describe('aliases', () => {
});

test('Exploit with Aliased Fields to bypass restrictions', () => {
const requestQuery = `query FindMyTalentJobApplications {
data: findJobApplications {
const requestQuery = `query FindMyJobs {
data: findJobs {
id
jobAd {
job {
id
location
secretTitle: secret
workMode
}
}
}`;
const expected = `query FindMyTalentJobApplications {
data: findJobApplications {
const expected = `query FindMyJobs {
data: findJobs {
id
jobAd {
job {
id
location
workMode
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ export class GraphQLQueryPurifier {
const firstFieldName = firstField ? firstField.name.value : '';

const key = `${operationName}.${firstFieldName}`.trim();
this.queryMap[key] = content;

if (this.queryMap[key]) {
throw new Error(
`Duplicate operation name detected: ${key}. File: ${file}`
);
} else {
this.queryMap[key] = content;
}
}
console.log('this.queryMap', this.queryMap);
});
}

Expand Down

0 comments on commit 8a183a9

Please sign in to comment.