-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
|
@@ -10,6 +10,19 @@ declare global { | |
|
||
export interface BuildSchemaOptions<Types extends SchemaTypes> { | ||
mocks?: ResolverMap<Types>; | ||
typeMocks?: Mock<Types>[]; | ||
} | ||
|
||
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>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if you have a |
||
) => Mock<Types>; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
|
@@ -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 commentThe 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; | ||
} | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this isn't safe. You can use |
||
|
||
return { | ||
name, | ||
resolver: resolver as never, | ||
}; | ||
}; |
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