Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,26 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
}
}
if (opts.terminateCircularRelationships) {
return handleValueGeneration(
opts,
null,
() =>
`relationshipsToOmit.has('${casedName}') ? {} as ${casedName} : ${toMockName(
return handleValueGeneration(opts, null, () => {
if (opts.typesPrefix) {
const typeNameConverter = createNameConverter(
opts.typeNamesConvention,
opts.transformUnderscore,
);
const casedNameWithPrefix = typeNameConverter(name, opts.typesPrefix);
return `relationshipsToOmit.has('${casedName}') ? {} as ${casedNameWithPrefix} : ${toMockName(
name,
casedName,
opts.prefix,
)}({}, relationshipsToOmit)`;
} else {
return `relationshipsToOmit.has('${casedName}') ? {} as ${casedName} : ${toMockName(
name,
casedName,
opts.prefix,
)}({}, relationshipsToOmit)`,
);
)}({}, relationshipsToOmit)`;
}
});
} else {
return handleValueGeneration(opts, null, () => `${toMockName(name, casedName, opts.prefix)}()`);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should support typesPrefix and terminateCircularRelationships at the same time 1`] = `
"
export const mockA = (overrides?: Partial<MockA>, _relationshipsToOmit: Set<string> = new Set()): MockA => {
const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
relationshipsToOmit.add('A');
return {
b: overrides && overrides.hasOwnProperty('b') ? overrides.b! : relationshipsToOmit.has('B') ? {} as MockB : mockB({}, relationshipsToOmit),
};
};

export const mockB = (overrides?: Partial<MockB>, _relationshipsToOmit: Set<string> = new Set()): MockB => {
const relationshipsToOmit: Set<string> = new Set(_relationshipsToOmit);
relationshipsToOmit.add('B');
return {
a: overrides && overrides.hasOwnProperty('a') ? overrides.a! : relationshipsToOmit.has('A') ? {} as MockA : mockA({}, relationshipsToOmit),
};
};
"
`;
10 changes: 10 additions & 0 deletions tests/typesPrefixAndTerminateCircularRelationships/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { buildSchema } from 'graphql';

export default buildSchema(/* GraphQL */ `
type A {
b: B!
}
type B {
a: A!
}
`);
16 changes: 16 additions & 0 deletions tests/typesPrefixAndTerminateCircularRelationships/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { plugin } from '../../src';
import testSchema from './schema';

it('should support typesPrefix and terminateCircularRelationships at the same time', async () => {
const result = await plugin(testSchema, [], {
prefix: 'mock',
typesPrefix: 'Mock',
terminateCircularRelationships: true,
});

expect(result).toBeDefined();
expect(result).toContain(
"a: overrides && overrides.hasOwnProperty('a') ? overrides.a! : relationshipsToOmit.has('A') ? {} as MockA : mockA({}, relationshipsToOmit)",
);
expect(result).toMatchSnapshot();
});