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

Object Type mocks #314

wants to merge 1 commit into from

Conversation

Jomik
Copy link
Contributor

@Jomik Jomik commented Feb 12, 2022

The idea is to be able to mock certain returned objects whenever they are seen in the schema.

const DroidMock = createObjectMock('Droid', (...) => fakeDroid);

const mockedSchema = builder.toSchema({
  typeMocks: [DroidMock],
});

It would be really amazing if our mock resolver could get some sensible arguments, like the id of the object being resolved, if using dataloader/relay node. I sadly do not think that is possible, given that we have to run the resolver that we try to wrap, to figure out if it is an id or a full object, and that is probably not what is wanted in most cases.
It would also be nice if we were able to be a bit more clever or dynamic with the length of the lists that are requested, but I have found no good way for that.

@changeset-bot
Copy link

changeset-bot bot commented Feb 12, 2022

⚠️ No Changeset found

Latest commit: 04e70a3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Jomik Jomik marked this pull request as draft February 12, 2022 14:53
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)

@@ -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

@@ -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

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

.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

@hayes hayes force-pushed the main branch 2 times, most recently from 496adda to d10db94 Compare March 8, 2022 21:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants