Skip to content

Fix: handling useImplementingTypes and defaultNullableToNull #181

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

Merged
merged 2 commits into from
Feb 26, 2025
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
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,17 @@ const getNamedType = (opts: Options<NamedTypeNode | ObjectTypeDefinitionNode>):
)
break;

return foundTypes
.map((implementType: TypeItem) =>
getNamedImplementType({
...opts,
currentType: implementType.types,
}),
)
.join(' || ');
return (
foundTypes
.map((implementType: TypeItem) =>
getNamedImplementType({
...opts,
currentType: implementType.types,
}),
)
.filter((value) => value !== null)
.join(' || ') || null
);
default:
throw `foundType is unknown: ${foundType.name}: ${foundType.type}`;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`should support useImplementingTypes 1`] = `
"
export const mockRoot = (overrides?: Partial<Root>): Root => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
};
};

export const mockA = (overrides?: Partial<A>): A => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
};
};

export const mockB = (overrides?: Partial<B>): B => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
};
};

export const mockC = (overrides?: Partial<C>): C => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
};
};

export const mockD = (overrides?: Partial<D>): D => {
return {
id: overrides && overrides.hasOwnProperty('id') ? overrides.id! : null,
};
};

export const mockTest = (overrides?: Partial<Test>): Test => {
return {
field1: overrides && overrides.hasOwnProperty('field1') ? overrides.field1! : mockA() || mockB() || mockC() || mockD(),
field2: overrides && overrides.hasOwnProperty('field2') ? overrides.field2! : null,
};
};
"
`;
28 changes: 28 additions & 0 deletions tests/useImplementingTypesAndDefaultNullableToNull/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { buildSchema } from 'graphql';

export default buildSchema(/* GraphQL */ `
interface Root {
id: ID
}

type A implements Root {
id: ID
}

type B implements Root {
id: ID
}

type C implements Root {
id: ID
}

type D implements Root {
id: ID
}

type Test {
field1: Root!
field2: Root
}
`);
20 changes: 20 additions & 0 deletions tests/useImplementingTypesAndDefaultNullableToNull/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { plugin } from '../../src';
import testSchema from './schema';

it('should support useImplementingTypes', async () => {
const result = await plugin(testSchema, [], {
prefix: 'mock',
useImplementingTypes: true,
defaultNullableToNull: true,
});

expect(result).toBeDefined();

expect(result).toContain(
"field1: overrides && overrides.hasOwnProperty('field1') ? overrides.field1! : mockA() || mockB() || mockC() || mockD(),",
);

expect(result).toContain("field2: overrides && overrides.hasOwnProperty('field2') ? overrides.field2! : null,");

expect(result).toMatchSnapshot();
});
Loading