Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ardeois/graphql-codegen-typescript-mock-data
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: slab/graphql-codegen-typescript-mock-data
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Feb 3, 2025

  1. Copy the full SHA
    44aeb89 View commit details

Commits on Feb 4, 2025

  1. Update package name

    mateusz-slab committed Feb 4, 2025
    Copy the full SHA
    775ebff View commit details
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "graphql-codegen-typescript-mock-data",
"name": "slab-graphql-codegen-typescript-mock-data",
"version": "4.1.0",
"description": "GraphQL Codegen plugin for building mock data",
"main": "dist/commonjs/index.js",
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -371,14 +371,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}`;
}
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();
});