Skip to content

Commit 8d252fc

Browse files
authored
fix: type error when using typesPrefix and terminateCircularRelationships simultaneously (#151)
1 parent 1eaf6ab commit 8d252fc

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

src/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,26 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
366366
}
367367
}
368368
if (opts.terminateCircularRelationships) {
369-
return handleValueGeneration(
370-
opts,
371-
null,
372-
() =>
373-
`relationshipsToOmit.has('${casedName}') ? {} as ${casedName} : ${toMockName(
369+
return handleValueGeneration(opts, null, () => {
370+
if (opts.typesPrefix) {
371+
const typeNameConverter = createNameConverter(
372+
opts.typeNamesConvention,
373+
opts.transformUnderscore,
374+
);
375+
const casedNameWithPrefix = typeNameConverter(name, opts.typesPrefix);
376+
return `relationshipsToOmit.has('${casedName}') ? {} as ${casedNameWithPrefix} : ${toMockName(
377+
name,
378+
casedName,
379+
opts.prefix,
380+
)}({}, relationshipsToOmit)`;
381+
} else {
382+
return `relationshipsToOmit.has('${casedName}') ? {} as ${casedName} : ${toMockName(
374383
name,
375384
casedName,
376385
opts.prefix,
377-
)}({}, relationshipsToOmit)`,
378-
);
386+
)}({}, relationshipsToOmit)`;
387+
}
388+
});
379389
} else {
380390
return handleValueGeneration(opts, null, () => `${toMockName(name, casedName, opts.prefix)}()`);
381391
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`should support typesPrefix and terminateCircularRelationships at the same time 1`] = `
4+
"
5+
export const mockA = (overrides?: Partial<MockA>, _relationshipsToOmit: Set<string> = new Set()): MockA => {
6+
const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
7+
relationshipsToOmit.add('A');
8+
return {
9+
b: overrides && overrides.hasOwnProperty('b') ? overrides.b! : relationshipsToOmit.has('B') ? {} as MockB : mockB({}, relationshipsToOmit),
10+
};
11+
};
12+
13+
export const mockB = (overrides?: Partial<MockB>, _relationshipsToOmit: Set<string> = new Set()): MockB => {
14+
const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
15+
relationshipsToOmit.add('B');
16+
return {
17+
a: overrides && overrides.hasOwnProperty('a') ? overrides.a! : relationshipsToOmit.has('A') ? {} as MockA : mockA({}, relationshipsToOmit),
18+
};
19+
};
20+
"
21+
`;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { buildSchema } from 'graphql';
2+
3+
export default buildSchema(/* GraphQL */ `
4+
type A {
5+
b: B!
6+
}
7+
type B {
8+
a: A!
9+
}
10+
`);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { plugin } from '../../src';
2+
import testSchema from './schema';
3+
4+
it('should support typesPrefix and terminateCircularRelationships at the same time', async () => {
5+
const result = await plugin(testSchema, [], {
6+
prefix: 'mock',
7+
typesPrefix: 'Mock',
8+
terminateCircularRelationships: true,
9+
});
10+
11+
expect(result).toBeDefined();
12+
expect(result).toContain(
13+
"a: overrides && overrides.hasOwnProperty('a') ? overrides.a! : relationshipsToOmit.has('A') ? {} as MockA : mockA({}, relationshipsToOmit)",
14+
);
15+
expect(result).toMatchSnapshot();
16+
});

0 commit comments

Comments
 (0)