Skip to content

Commit

Permalink
fix(utils): Allow invalid path in getDirectiveExtensions (#6436)
Browse files Browse the repository at this point in the history
Co-authored-by: Olaf Alexander <oalexander@indeed.com>
  • Loading branch information
oalexander and Olaf Alexander committed Aug 13, 2024
1 parent d401903 commit 6a389e8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/utils/src/getDirectiveExtensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export function getDirectiveExtensions<
if (directableObj.extensions) {
let directivesInExtensions = directableObj.extensions;
for (const pathSegment of pathToDirectivesInExtensions) {
directivesInExtensions = directivesInExtensions[pathSegment];
directivesInExtensions = directivesInExtensions?.[pathSegment];
}
if (directivesInExtensions != null) {
for (const directiveNameProp in directivesInExtensions) {
Expand Down
65 changes: 65 additions & 0 deletions packages/utils/tests/getDirectiveExtensions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { GraphQLObjectType } from 'graphql';
import { getDirectiveExtensions } from '../src';

describe('getDirectiveExtensions', () => {
it('should return directive extensions when they are in the directives path', () => {
const objectType = new GraphQLObjectType({
name: 'User',
fields: () => ({}),
extensions: {
directives: {
id: {},
},
},
});

const directiveExtensions = getDirectiveExtensions(objectType);
expect(directiveExtensions).toEqual(expect.objectContaining({ id: expect.any(Object) }));
});

it('should return directive extensions when they are in the a custom path', () => {
const objectType = new GraphQLObjectType({
name: 'TestObject',
fields: () => ({}),
extensions: {
custom: {
directives: {
path: {
id: {},
},
},
},
},
});

const directiveExtensions = getDirectiveExtensions(objectType, undefined, [
'custom',
'directives',
'path',
]);
expect(directiveExtensions).toEqual(expect.objectContaining({ id: expect.any(Object) }));
});

it('should return no directive extensions when they are no defined', () => {
const objectType = new GraphQLObjectType({
name: 'TestObject',
fields: () => ({}),
});
const directiveExtensions = getDirectiveExtensions(objectType);
expect(directiveExtensions).toEqual({});
});

it('should return no directive extensions when an invalid extension path is given', () => {
const objectType = new GraphQLObjectType({
name: 'TestObject',
fields: () => ({}),
});
const directiveExtensions = getDirectiveExtensions(objectType, undefined, [
'not',
'a',
'real',
'path',
]);
expect(directiveExtensions).toEqual({});
});
});

0 comments on commit 6a389e8

Please sign in to comment.