Skip to content

Commit

Permalink
use builder.nodeRef in prisma plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Oct 21, 2024
1 parent 2f91f2b commit 54019c9
Showing 1 changed file with 9 additions and 37 deletions.
46 changes: 9 additions & 37 deletions packages/plugin-prisma/src/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ schemaBuilderProto.prismaNode = function prismaNode(
}: PrismaNodeOptions<SchemaTypes, PrismaModelTypes, [], never, {}, {}, undefined>,
) {
const fieldName = field as unknown as string;
const interfaceRef = this.nodeInterfaceRef?.();
const resolve = rawResolve ?? getDefaultIDSerializer(type, fieldName, this);
const idParser = fieldName ? getDefaultIDParser(type, fieldName, this) : undefined;
const typeName = variant ?? name ?? type;
Expand All @@ -135,16 +134,20 @@ schemaBuilderProto.prismaNode = function prismaNode(
rawFindUnique(resolve(parent as never, context) as string, context)
: ModelLoader.getFindUniqueForField(nodeRef, type, fieldName, this);

if (!interfaceRef) {
throw new PothosError('builder.prismaNode requires @pothos/plugin-relay to be installed');
}

const extendedOptions = {
...options,
name,
variant,
interfaces: [interfaceRef],
findUnique,
};

const ref = this.prismaObject(type, extendedOptions as never);

(this as typeof this & { nodeRef: (ref: unknown, options: unknown) => unknown }).nodeRef(ref, {
id: {
...idOptions,
resolve: (parent: never, _args: object, context: object) => resolve(parent, context),
},
loadWithoutCache: async (
id: string,
context: SchemaTypes['Context'],
Expand All @@ -168,37 +171,6 @@ schemaBuilderProto.prismaNode = function prismaNode(

return record;
},
};

const ref = this.prismaObject(type, extendedOptions as never);

if (options.interfaces) {
ref.addInterfaces(options.interfaces);
}

this.configStore.onTypeConfig(ref, (nodeConfig) => {
this.objectField(
ref,
(this.options as { relay?: { idFieldName?: string } }).relay?.idFieldName ??
'id',
(t) =>
(
t as unknown as {
globalID: (options: Record<string, unknown>) => FieldRef<SchemaTypes, unknown>;
}
).globalID({
...(this.options as { relay?: { idFieldOptions?: object } }).relay
?.idFieldOptions,
...idOptions,
nullable: false,
args: {},
resolve: (parent: never, _args: object, context: object) =>
completeValue(resolve(parent, context), (id) => ({
type: nodeConfig.name,
id,
})),
}),
);
});

this.configStore.associateParamWithRef(nodeRef, ref);
Expand Down

0 comments on commit 54019c9

Please sign in to comment.