-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
base: main
Are you sure you want to change the base?
Object Type mocks #314
Conversation
|
NameOrRef extends ObjectParam<Types> | string, | ||
>( | ||
nameOrRef: NameOrRef, | ||
resolver: Resolver<unknown, unknown, Types['Context'], Partial<Shape>>, |
There was a problem hiding this comment.
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>[]; |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
496adda
to
d10db94
Compare
The idea is to be able to mock certain returned objects whenever they are seen in the schema.
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.