From 8d5ac033ac3b9a454bef97ec95e3efe2e6654ad8 Mon Sep 17 00:00:00 2001 From: Michael Hayes Date: Thu, 23 Feb 2023 11:52:45 -1000 Subject: [PATCH] update readmes --- .../deno/packages/plugin-dataloader/README.md | 2 +- .../plugin-dataloader/schema-builder.ts | 31 +++++++++++++++++-- packages/plugin-dataloader/README.md | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/packages/deno/packages/plugin-dataloader/README.md b/packages/deno/packages/plugin-dataloader/README.md index 64254bc85..c7836ee40 100644 --- a/packages/deno/packages/plugin-dataloader/README.md +++ b/packages/deno/packages/plugin-dataloader/README.md @@ -306,7 +306,7 @@ const UserNode = builder.loadableNode('UserNode', { id: { resolve: (user) => user.id, }, - // For loadable objects we always need to include an isTypeOf check + // For loadable nodes, we need to include an isTypeOf check if the first arg is a string isTypeOf: (obj) => obj instanceof User, load: (ids: string[], context: ContextType) => context.loadUsersById(ids), fields: (t) => ({}), diff --git a/packages/deno/packages/plugin-dataloader/schema-builder.ts b/packages/deno/packages/plugin-dataloader/schema-builder.ts index 77fb0b6b0..4c1a25aaa 100644 --- a/packages/deno/packages/plugin-dataloader/schema-builder.ts +++ b/packages/deno/packages/plugin-dataloader/schema-builder.ts @@ -1,5 +1,6 @@ // @ts-nocheck -import SchemaBuilder, { InterfaceParam, ObjectParam, PothosSchemaError, SchemaTypes, ShapeFromTypeParam, } from '../core/index.ts'; +import type { GraphQLResolveInfo } from 'https://cdn.skypack.dev/graphql?dts'; +import SchemaBuilder, { InterfaceParam, ObjectParam, OutputRef, PothosSchemaError, SchemaTypes, ShapeFromTypeParam, } from '../core/index.ts'; import { ImplementableLoadableNodeRef } from './refs/index.ts'; import { ImplementableLoadableInterfaceRef } from './refs/interface.ts'; import { ImplementableLoadableObjectRef } from './refs/object.ts'; @@ -74,7 +75,33 @@ schemaBuilderProto.loadableNode = function loadableNode(this, name, options); - ref.implement(options); + ref.implement({ + ...options, + isTypeOf: options.isTypeOf ?? + (typeof nameOrRef === "function" + ? (maybeNode: unknown, context: object, info: GraphQLResolveInfo) => { + if (!maybeNode) { + return false; + } + if (maybeNode instanceof (nameOrRef as Function)) { + return true; + } + const proto = Object.getPrototypeOf(maybeNode) as { + constructor: unknown; + }; + try { + if (proto?.constructor) { + const config = this.configStore.getTypeConfig(proto.constructor as OutputRef); + return config.name === name; + } + } + catch { + // ignore + } + return false; + } + : undefined), + }); if (typeof nameOrRef !== "string") { this.configStore.associateRefWithName(nameOrRef, name); } diff --git a/packages/plugin-dataloader/README.md b/packages/plugin-dataloader/README.md index 64254bc85..c7836ee40 100644 --- a/packages/plugin-dataloader/README.md +++ b/packages/plugin-dataloader/README.md @@ -306,7 +306,7 @@ const UserNode = builder.loadableNode('UserNode', { id: { resolve: (user) => user.id, }, - // For loadable objects we always need to include an isTypeOf check + // For loadable nodes, we need to include an isTypeOf check if the first arg is a string isTypeOf: (obj) => obj instanceof User, load: (ids: string[], context: ContextType) => context.loadUsersById(ids), fields: (t) => ({}),