Skip to content

Commit

Permalink
feat: allow unionType types to be a thunk
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Feb 22, 2023
1 parent 4b3ebc7 commit 42bf619
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/great-suits-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@pothos/core': minor
'@pothos/deno': minor
---

Allow unionType to receive types as a thunk
11 changes: 7 additions & 4 deletions packages/core/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,25 @@ export default class SchemaBuilder<Types extends SchemaTypes> {
ParentShape<Types, Member>
>(name);

options.types.forEach((type) => {
verifyRef(type);
});
if (Array.isArray(options.types)) {
options.types.forEach((type) => {
verifyRef(type);
});
}

const config: PothosUnionTypeConfig = {
kind: 'Union',
graphqlKind: 'Union',
name,
types: (options.types || []) as ObjectParam<SchemaTypes>[],
types: [],
description: options.description,
resolveType: options.resolveType as GraphQLTypeResolver<unknown, object>,
pothosOptions: options as unknown as PothosSchemaTypes.UnionTypeOptions,
extensions: options.extensions,
};

this.configStore.addTypeConfig(config, ref);
this.configStore.addUnionTypes(name, options.types);

return ref;
}
Expand Down
34 changes: 34 additions & 0 deletions packages/core/src/config-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
InputType,
InputTypeParam,
InterfaceParam,
ObjectParam,
OutputRef,
OutputType,
PothosFieldConfig,
Expand Down Expand Up @@ -62,6 +63,8 @@ export default class ConfigStore<Types extends SchemaTypes> {

private pendingInterfaces = new Map<string, (() => InterfaceParam<Types>[])[]>();

private pendingUnionTypes = new Map<string, (() => ObjectParam<Types>[])[]>();

private pendingRefResolutions = new Map<
ConfigurableRef<Types>,
((config: PothosTypeConfig) => void)[]
Expand Down Expand Up @@ -98,6 +101,31 @@ export default class ConfigStore<Types extends SchemaTypes> {
return this.refsToName.has(typeParam);
}

addUnionTypes(typeName: string, unionTypes: ObjectParam<Types>[] | (() => ObjectParam<Types>[])) {
if (typeof unionTypes === 'function' && this.pending) {
if (this.pendingUnionTypes.has(typeName)) {
this.pendingUnionTypes.get(typeName)!.push(unionTypes);
} else {
this.pendingUnionTypes.set(typeName, [unionTypes]);
}
} else {
const typeConfig = this.getTypeConfig(typeName);

if (typeConfig.graphqlKind !== 'Union') {
throw new PothosSchemaError(
`Can not add types to ${typeName} because it is a ${typeConfig.kind}`,
);
}

typeConfig.types = [
...typeConfig.types,
...((typeof unionTypes === 'function'
? unionTypes()
: unionTypes) as ObjectParam<SchemaTypes>[]),
];
}
}

addInterfaces(
typeName: string,
interfaces: InterfaceParam<Types>[] | (() => InterfaceParam<Types>[]),
Expand Down Expand Up @@ -428,6 +456,12 @@ export default class ConfigStore<Types extends SchemaTypes> {
);
}

for (const [typeName, unionFns] of this.pendingUnionTypes) {
for (const fn of unionFns) {
this.addUnionTypes(typeName, fn);
}
}

for (const [typeName, interfacesFns] of this.pendingInterfaces) {
for (const fn of interfacesFns) {
this.addInterfaces(typeName, fn);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/global/type-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ declare global {
Member extends ObjectParam<Types> = ObjectParam<Types>,
ResolveType = unknown,
> extends BaseTypeOptions<Types> {
types: Member[];
types: Member[] | (() => Member[]);
resolveType?: ResolveType &
((
parent: ParentShape<Types, Member>,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/examples/giraffes/unions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const GiraffeNumericFact = builder.objectType('GiraffeNumericFact', {
});

const GiraffeFact = builder.unionType('GiraffeFact', {
types: ['GiraffeStringFact', GiraffeNumericFact],
types: () => ['GiraffeStringFact', GiraffeNumericFact],
resolveType: (fact) => {
switch (fact.factKind) {
case 'number':
Expand Down
5 changes: 4 additions & 1 deletion packages/deno/packages/core/build-cache.ts

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

10 changes: 6 additions & 4 deletions packages/deno/packages/core/builder.ts

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

2 changes: 1 addition & 1 deletion packages/deno/packages/core/types/configs.ts

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

2 changes: 1 addition & 1 deletion packages/deno/packages/core/types/global/type-options.ts

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

8 changes: 1 addition & 7 deletions packages/deno/packages/plugin-tracing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -767,13 +767,7 @@ import { schema } from './schema';

const yoga = createYoga({
schema,
plugins: [
useSentry({
// Disable resolver tracking since this is covered by the pothos tracing plugin
// If all resolvers are being traced, you could use the Sentry envelop plug instead of the pothos tracing plugin
trackResolvers: false,
}),
],
plugins: [useSentry({})],
});

const server = createServer(yoga);
Expand Down
2 changes: 1 addition & 1 deletion website/pages/docs/api/schema-builder.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ all types in SchemaTypes, or import the actual implementation of each interface
```typescript
type UnionTypeOptions = {
description?: string;
types: Member[];
types: Member[] | (() => Member[]);
resolveType: (parent: UnionShape, context) => MaybePromise<GraphQLObjectType | TypeName>;
};
```
Expand Down

0 comments on commit 42bf619

Please sign in to comment.