Skip to content
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

Object Type mocks #314

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/deno/packages/plugin-mocks/global-types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion packages/deno/packages/plugin-mocks/types.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions packages/plugin-mocks/src/global-types.ts
@@ -1,5 +1,5 @@
import { SchemaTypes } from '@pothos/core';
import { ResolverMap } from './types';
import { ObjectParam, Resolver, SchemaTypes, ShapeFromTypeParam } from '@pothos/core';
import { Mock, ResolverMap } from './types';
import { MocksPlugin } from '.';

declare global {
Expand All @@ -10,6 +10,19 @@ declare global {

export interface BuildSchemaOptions<Types extends SchemaTypes> {
mocks?: ResolverMap<Types>;
typeMocks?: Mock<Types>[];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather have this be an object with type names as keys (to match the pattern we use for normal mocks

}

export interface SchemaBuilder<Types extends SchemaTypes> {
createObjectMock: <
Shape extends NameOrRef extends ObjectParam<Types>
? ShapeFromTypeParam<Types, NameOrRef, false>
: object,
NameOrRef extends ObjectParam<Types> | string,
>(
nameOrRef: NameOrRef,
resolver: Resolver<unknown, unknown, Types['Context'], Partial<Shape>>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if you have a resolveList third argument that defaults to (...args) => [resolver(...args)] (just returning a list of 1 item by default, but lets you customize list behavior if you want)

) => Mock<Types>;
}
}
}
60 changes: 49 additions & 11 deletions packages/plugin-mocks/src/index.ts
@@ -1,7 +1,14 @@
import './global-types';
import './schema-builder';
import { GraphQLFieldResolver } from 'graphql';
import SchemaBuilder, { BasePlugin, PothosOutputFieldConfig, SchemaTypes } from '@pothos/core';
import { ResolverMap } from './types';
import SchemaBuilder, {
BasePlugin,
PothosOutputFieldConfig,
PothosOutputFieldType,
Resolver,
SchemaTypes,
} from '@pothos/core';
import { Mock, ResolverMap } from './types';

const pluginName = 'mocks' as const;

Expand All @@ -11,13 +18,14 @@ export class MocksPlugin<Types extends SchemaTypes> extends BasePlugin<Types> {
resolver: GraphQLFieldResolver<unknown, Types['Context'], object>,
fieldConfig: PothosOutputFieldConfig<Types>,
): GraphQLFieldResolver<unknown, Types['Context'], object> {
const { mocks } = this.options;
const { mocks, typeMocks = [] } = this.options;
const { parentType: typeName, name: fieldName, type: outputType } = fieldConfig;

if (!mocks) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will prevent mocks from working if you only have typeMocks

return resolver;
}

const resolveMock = this.resolveMock(fieldConfig.parentType, fieldConfig.name, mocks);
const resolveMock = this.resolveMock(typeName, fieldName, outputType, mocks, typeMocks);

return resolveMock ?? resolver;
}
Expand All @@ -37,18 +45,48 @@ export class MocksPlugin<Types extends SchemaTypes> extends BasePlugin<Types> {
return subscribeMock ?? subscribe;
}

resolveMock(typename: string, fieldName: string, mocks: ResolverMap<Types>) {
const fieldMock = mocks[typename]?.[fieldName] || null;
resolveMock(
typeName: string,
fieldName: string,
outputType: PothosOutputFieldType<Types>,
mocks: ResolverMap<Types>,
typeMocks: Mock<Types>[],
): Resolver<unknown, {}, Types['Context'], unknown> | null {
const fieldMock = mocks[typeName]?.[fieldName] || null;

if (fieldMock) {
if (typeof fieldMock === 'function') {
return fieldMock;
}

return fieldMock.resolve ?? null;
}

if (!fieldMock) {
return null;
if (outputType.kind === 'Object') {
const outputName = (outputType.ref as { name: string }).name;
const mock = typeMocks.find((v) => v.name === outputName);

return mock?.resolver ?? null;
}

if (typeof fieldMock === 'function') {
return fieldMock;
if (outputType.kind === 'Interface') {
const outputName = (outputType.ref as { name: string }).name;
const implementers = this.builder.configStore
.getImplementers(outputName)
.map((implementer) => implementer.name);
const mock = typeMocks.find((v) => implementers.includes(v.name));

return mock?.resolver ?? null;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs a way to make it more configurable.... They way this is written, if you mock any node, any node/nodes query will return that type regardless of what id is being queried. Not sure how to solve this though

}

if (outputType.kind === 'List') {
const result = this.resolveMock(typeName, fieldName, outputType.type, mocks, typeMocks);
if (result) {
return (...args) => [result(...args)];
}
}

return fieldMock.resolve || null;
return null;
}

subscribeMock(typename: string, fieldName: string, mocks: ResolverMap<Types>) {
Expand Down
12 changes: 12 additions & 0 deletions packages/plugin-mocks/src/schema-builder.ts
@@ -0,0 +1,12 @@
import SchemaBuilder, { SchemaTypes } from '@pothos/core';

const schemaBuilderProto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder<SchemaTypes>;

schemaBuilderProto.createObjectMock = function createMock(nameOrRef, resolver) {
const name = typeof nameOrRef === 'string' ? nameOrRef : (nameOrRef as { name: string }).name;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't safe. You can use this.configStore.getTypeConfig(nameOrRef).name


return {
name,
resolver: resolver as never,
};
};
5 changes: 5 additions & 0 deletions packages/plugin-mocks/src/types.ts
Expand Up @@ -10,3 +10,8 @@ export type Resolvers<Types extends SchemaTypes, Parent> = Record<
>;

export type ResolverMap<Types extends SchemaTypes> = Record<string, Resolvers<Types, unknown>>;

export interface Mock<Types extends SchemaTypes> {
name: string;
resolver: Resolver<unknown, unknown, Types['Context'], unknown>;
}
25 changes: 25 additions & 0 deletions packages/plugin-mocks/tests/__snapshots__/index.test.ts.snap
Expand Up @@ -9,3 +9,28 @@ Object {
},
}
`;

exports[`mocked type mocks queries lists 1`] = `
Object {
"data": Object {
"r2d2": Object {
"friends": Array [
Object {
"name": "C-3PO",
},
],
"name": "C-3PO",
},
},
}
`;

exports[`mocked type mocks queries stuff 1`] = `
Object {
"data": Object {
"r2d2": Object {
"name": "C-3PO",
},
},
}
`;
59 changes: 59 additions & 0 deletions packages/plugin-mocks/tests/index.test.ts
Expand Up @@ -29,4 +29,63 @@ describe('mocked', () => {

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

describe('type mocks', () => {
it('queries stuff', async () => {
const DroidMock = builder.createObjectMock('Droid', () => ({
name: 'C-3PO',
}));
const mockedSchema = builder.toSchema({
mocks: {},
typeMocks: [DroidMock],
});

const query = gql`
query {
r2d2 {
name
}
}
`;

const result = await execute({
schema: mockedSchema,
document: query,
contextValue: {},
});

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

it('queries lists', async () => {
const DroidMock = builder.createObjectMock('Droid', () => ({
type: 'Droid' as const,
name: 'C-3PO',
friends: ['1002', '1003'],
}));
const mockedSchema = builder.toSchema({
mocks: {},
typeMocks: [DroidMock],
});

const query = gql`
query {
r2d2 {
name
friends {
name
}
}
}
`;

const result = await execute({
schema: mockedSchema,
document: query,
contextValue: {},
});

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