@pothos/plugin-relay
Advanced tools
Comparing version 4.2.1 to 4.3.0
# Change Log | ||
## 4.3.0 | ||
### Minor Changes | ||
- 6a80a7c: Add builder.nodeRef method | ||
## 4.2.1 | ||
@@ -4,0 +10,0 @@ |
import { type FieldKind, type FieldNullability, type FieldOptionsFromKind, type FieldRef, type FieldRequiredness, type InferredFieldOptionKeys, type InputFieldMap, type InputFieldsFromShape, type InputOrArgRef, type InputShapeFromFields, type InputShapeFromTypeParam, type InterfaceParam, type NormalizeArgs, type ObjectFieldThunk, type ObjectFieldsShape, type ObjectParam, type OutputShape, type OutputType, type ParentShape, type Resolver, type SchemaTypes, type ShapeFromTypeParam, inputShapeKey } from '@pothos/core'; | ||
import type { NodeRef } from './node-ref'; | ||
import type { ConnectionResultShape, ConnectionShape, ConnectionShapeForType, ConnectionShapeFromResolve, GlobalIDFieldOptions, GlobalIDInputFieldOptions, GlobalIDInputShape, GlobalIDListFieldOptions, GlobalIDListInputFieldOptions, GlobalIDShape, InputShapeWithClientMutationId, NodeFieldOptions, NodeListFieldOptions, NodeObjectOptions, PageInfoShape, RelayMutationFieldOptions, RelayMutationInputOptions, RelayMutationPayloadOptions, RelayPluginOptions } from './types'; | ||
import type { ImplementableNodeRef, NodeRef } from './node-ref'; | ||
import type { ConnectionResultShape, ConnectionShape, ConnectionShapeForType, ConnectionShapeFromResolve, GlobalIDFieldOptions, GlobalIDInputFieldOptions, GlobalIDInputShape, GlobalIDListFieldOptions, GlobalIDListInputFieldOptions, GlobalIDShape, InputShapeWithClientMutationId, NodeFieldOptions, NodeListFieldOptions, NodeObjectOptions, NodeRefOptions, PageInfoShape, RelayMutationFieldOptions, RelayMutationInputOptions, RelayMutationPayloadOptions, RelayPluginOptions } from './types'; | ||
import type { DefaultEdgesNullability, PothosRelayPlugin } from '.'; | ||
@@ -35,2 +35,3 @@ declare global { | ||
nodeInterfaceRef: () => InterfaceRef<Types, unknown>; | ||
nodeRef: <Shape, IDShape = string, Param extends string | ObjectRef<Types, Shape> = string>(param: Param, options: NodeRefOptions<Types, Shape, Shape, IDShape>, fields?: ObjectFieldsShape<Types, Shape>) => Param extends string ? ImplementableNodeRef<Types, Shape, Shape, IDShape> : Param; | ||
node: <Interfaces extends InterfaceParam<Types>[], Param extends ObjectParam<Types>, IDShape = string>(param: Param, options: NodeObjectOptions<Types, Param, Interfaces, IDShape>, fields?: ObjectFieldsShape<Types, ParentShape<Types, Param>>) => NodeRef<Types, OutputShape<Types, Param>, ParentShape<Types, Param>, IDShape>; | ||
@@ -37,0 +38,0 @@ globalConnectionFields: (fields: ObjectFieldsShape<Types, ConnectionShape<Types, {}, false>>) => void; |
@@ -1,2 +0,3 @@ | ||
import { ObjectRef, type SchemaTypes } from '@pothos/core'; | ||
import { ImplementableObjectRef, type ObjectParam, ObjectRef, type SchemaTypes } from '@pothos/core'; | ||
import type { NodeRefOptions } from './types'; | ||
export declare const relayIDShapeKey: unique symbol; | ||
@@ -6,6 +7,8 @@ export declare class NodeRef<Types extends SchemaTypes, T, P = T, IDShape = string> extends ObjectRef<Types, T, P> { | ||
parseId: ((id: string, ctx: object) => IDShape) | undefined; | ||
constructor(name: string, options: { | ||
parseId?: (id: string, ctx: object) => IDShape; | ||
}); | ||
constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, name: string, param: ObjectParam<Types>, options: NodeRefOptions<Types, T, P, IDShape>); | ||
} | ||
export declare class ImplementableNodeRef<Types extends SchemaTypes, T, P = T, IDShape = string> extends ImplementableObjectRef<Types, T, P> { | ||
parseId: ((id: string, ctx: object) => IDShape) | undefined; | ||
constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, name: string, options: NodeRefOptions<Types, T, P, IDShape>); | ||
} | ||
//# sourceMappingURL=node-ref.d.ts.map |
@@ -130,12 +130,13 @@ import { type ArgumentRef, type EmptyToOptional, type FieldKind, type FieldNullability, type FieldOptionsFromKind, type FieldRequiredness, type InferredFieldOptionKeys, type InputFieldMap, type InputFieldRef, type InputFieldsFromShape, type InputRef, type InputShapeFromFields, type InterfaceParam, type MaybePromise, type Normalize, type ObjectFieldsShape, type ObjectParam, type ObjectRef, type ObjectTypeOptions, type OutputRef, type OutputRefShape, type OutputShape, type OutputType, type ParentShape, type Resolver, type SchemaTypes, type ShapeFromListTypeParam, type ShapeFromTypeParam, inputShapeKey } from '@pothos/core'; | ||
export type NodeBaseObjectOptionsForParam<Types extends SchemaTypes, Param extends ObjectParam<Types>, Interfaces extends InterfaceParam<Types>[]> = ObjectTypeOptions<Types, Param, ParentShape<Types, Param>, Interfaces>; | ||
export type NodeObjectOptions<Types extends SchemaTypes, Param extends ObjectParam<Types>, Interfaces extends InterfaceParam<Types>[], IDShape = string> = NodeBaseObjectOptionsForParam<Types, Param, Interfaces> & { | ||
id: Omit<FieldOptionsFromKind<Types, ParentShape<Types, Param>, 'ID', false, {}, 'Object', OutputShape<Types, 'ID'>, MaybePromise<OutputShape<Types, 'ID'>>>, 'args' | 'nullable' | 'type'> & { | ||
export type NodeRefOptions<Types extends SchemaTypes, T, P = T, IDShape = string> = { | ||
id: Omit<FieldOptionsFromKind<Types, P, 'ID', false, {}, 'Object', OutputShape<Types, 'ID'>, MaybePromise<OutputShape<Types, 'ID'>>>, 'args' | 'nullable' | 'type'> & { | ||
parse?: (id: string, ctx: Types['Context']) => IDShape; | ||
}; | ||
brandLoadedObjects?: boolean; | ||
loadOne?: (id: IDShape, context: Types['Context']) => MaybePromise<OutputShape<Types, Param> | null | undefined>; | ||
loadMany?: (ids: IDShape[], context: Types['Context']) => MaybePromise<readonly MaybePromise<OutputShape<Types, Param> | null | undefined>[]>; | ||
loadWithoutCache?: (id: IDShape, context: Types['Context'], info: GraphQLResolveInfo) => MaybePromise<OutputShape<Types, Param> | null | undefined>; | ||
loadManyWithoutCache?: (ids: IDShape[], context: Types['Context']) => MaybePromise<readonly MaybePromise<OutputShape<Types, Param> | null | undefined>[]>; | ||
loadOne?: (id: IDShape, context: Types['Context']) => MaybePromise<T | null | undefined>; | ||
loadMany?: (ids: IDShape[], context: Types['Context']) => MaybePromise<readonly MaybePromise<T | null | undefined>[]>; | ||
loadWithoutCache?: (id: IDShape, context: Types['Context'], info: GraphQLResolveInfo) => MaybePromise<T | null | undefined>; | ||
loadManyWithoutCache?: (ids: IDShape[], context: Types['Context']) => MaybePromise<readonly MaybePromise<T | null | undefined>[]>; | ||
}; | ||
export type NodeObjectOptions<Types extends SchemaTypes, Param extends ObjectParam<Types>, Interfaces extends InterfaceParam<Types>[], IDShape = string> = NodeBaseObjectOptionsForParam<Types, Param, Interfaces> & NodeRefOptions<Types, OutputShape<Types, Param>, ParentShape<Types, Param>, IDShape>; | ||
export type GlobalIDFieldOptions<Types extends SchemaTypes, ParentShape, Args extends InputFieldMap, Nullable extends boolean, ResolveReturnShape, Kind extends FieldKind = FieldKind> = Omit<FieldOptionsFromKind<Types, ParentShape, 'ID', Nullable, Args, Kind, ParentShape, ResolveReturnShape>, 'type' | InferredFieldOptionKeys> & { | ||
@@ -142,0 +143,0 @@ resolve: Resolver<ParentShape, InputShapeFromFields<Args>, Types['Context'], ShapeFromTypeParam<Types, OutputRefShape<GlobalIDShape<Types> | string>, true>, ResolveReturnShape>; |
import { type FieldKind, type FieldNullability, type FieldOptionsFromKind, type FieldRef, type FieldRequiredness, type InferredFieldOptionKeys, type InputFieldMap, type InputFieldsFromShape, type InputOrArgRef, type InputShapeFromFields, type InputShapeFromTypeParam, type InterfaceParam, type NormalizeArgs, type ObjectFieldThunk, type ObjectFieldsShape, type ObjectParam, type OutputShape, type OutputType, type ParentShape, type Resolver, type SchemaTypes, type ShapeFromTypeParam, inputShapeKey } from '@pothos/core'; | ||
import type { NodeRef } from './node-ref.js'; | ||
import type { ConnectionResultShape, ConnectionShape, ConnectionShapeForType, ConnectionShapeFromResolve, GlobalIDFieldOptions, GlobalIDInputFieldOptions, GlobalIDInputShape, GlobalIDListFieldOptions, GlobalIDListInputFieldOptions, GlobalIDShape, InputShapeWithClientMutationId, NodeFieldOptions, NodeListFieldOptions, NodeObjectOptions, PageInfoShape, RelayMutationFieldOptions, RelayMutationInputOptions, RelayMutationPayloadOptions, RelayPluginOptions } from './types.js'; | ||
import type { ImplementableNodeRef, NodeRef } from './node-ref.js'; | ||
import type { ConnectionResultShape, ConnectionShape, ConnectionShapeForType, ConnectionShapeFromResolve, GlobalIDFieldOptions, GlobalIDInputFieldOptions, GlobalIDInputShape, GlobalIDListFieldOptions, GlobalIDListInputFieldOptions, GlobalIDShape, InputShapeWithClientMutationId, NodeFieldOptions, NodeListFieldOptions, NodeObjectOptions, NodeRefOptions, PageInfoShape, RelayMutationFieldOptions, RelayMutationInputOptions, RelayMutationPayloadOptions, RelayPluginOptions } from './types.js'; | ||
import type { DefaultEdgesNullability, PothosRelayPlugin } from './index.js'; | ||
@@ -39,2 +39,3 @@ declare global { | ||
nodeInterfaceRef: () => InterfaceRef<Types, unknown>; | ||
nodeRef: <Shape, IDShape = string, Param extends string | ObjectRef<Types, Shape> = string>(param: Param, options: NodeRefOptions<Types, Shape, Shape, IDShape>, fields?: ObjectFieldsShape<Types, Shape>) => Param extends string ? ImplementableNodeRef<Types, Shape, Shape, IDShape> : Param; | ||
node: <Interfaces extends InterfaceParam<Types>[], Param extends ObjectParam<Types>, IDShape = string>(param: Param, options: NodeObjectOptions<Types, Param, Interfaces, IDShape>, fields?: ObjectFieldsShape<Types, ParentShape<Types, Param>>) => NodeRef<Types, OutputShape<Types, Param>, ParentShape<Types, Param>, IDShape>; | ||
@@ -41,0 +42,0 @@ globalConnectionFields: (fields: ObjectFieldsShape<Types, ConnectionShape<Types, {}, false>>) => void; |
@@ -1,2 +0,3 @@ | ||
import { ObjectRef, type SchemaTypes } from '@pothos/core'; | ||
import { ImplementableObjectRef, type ObjectParam, ObjectRef, type SchemaTypes } from '@pothos/core'; | ||
import type { NodeRefOptions } from './types.js'; | ||
export declare const relayIDShapeKey: unique symbol; | ||
@@ -6,6 +7,8 @@ export declare class NodeRef<Types extends SchemaTypes, T, P = T, IDShape = string> extends ObjectRef<Types, T, P> { | ||
parseId: ((id: string, ctx: object) => IDShape) | undefined; | ||
constructor(name: string, options: { | ||
parseId?: (id: string, ctx: object) => IDShape; | ||
}); | ||
constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, name: string, param: ObjectParam<Types>, options: NodeRefOptions<Types, T, P, IDShape>); | ||
} | ||
export declare class ImplementableNodeRef<Types extends SchemaTypes, T, P = T, IDShape = string> extends ImplementableObjectRef<Types, T, P> { | ||
parseId: ((id: string, ctx: object) => IDShape) | undefined; | ||
constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, name: string, options: NodeRefOptions<Types, T, P, IDShape>); | ||
} | ||
//# sourceMappingURL=node-ref.d.ts.map |
@@ -15,13 +15,20 @@ function _define_property(obj, key, value) { | ||
} | ||
import { ObjectRef } from '@pothos/core'; | ||
import { ImplementableObjectRef, ObjectRef } from '@pothos/core'; | ||
import { addNodeProperties } from './utils/add-node-props.js'; | ||
export const relayIDShapeKey = Symbol.for("Pothos.relayIDShapeKey"); | ||
let _relayIDShapeKey = relayIDShapeKey; | ||
export class NodeRef extends ObjectRef { | ||
constructor(name, options) { | ||
super(name); | ||
_define_property(this, _relayIDShapeKey, void 0); | ||
_define_property(this, "parseId", void 0); | ||
this.parseId = options.parseId; | ||
constructor(builder, name, param, options) { | ||
super(name), _define_property(this, _relayIDShapeKey, void 0), _define_property(this, "parseId", void 0); | ||
this.parseId = options.id.parse; | ||
addNodeProperties(name, builder, this, param, options); | ||
} | ||
} | ||
export class ImplementableNodeRef extends ImplementableObjectRef { | ||
constructor(builder, name, options) { | ||
super(builder, name), _define_property(this, "parseId", void 0); | ||
this.parseId = options.id.parse; | ||
addNodeProperties(name, builder, this, undefined, options); | ||
} | ||
} | ||
//# sourceMappingURL=node-ref.js.map |
@@ -1,5 +0,6 @@ | ||
import SchemaBuilder, { completeValue, createContextCache, getTypeBrand, InputObjectRef, ObjectRef, PothosValidationError, verifyRef } from '@pothos/core'; | ||
import SchemaBuilder, { BaseTypeRef, completeValue, createContextCache, getTypeBrand, InputObjectRef, ObjectRef, PothosValidationError, verifyRef } from '@pothos/core'; | ||
import { defaultTypeResolver } from 'graphql'; | ||
import { NodeRef } from './node-ref.js'; | ||
import { ImplementableNodeRef, NodeRef } from './node-ref.js'; | ||
import { capitalize, resolveNodes } from './utils/index.js'; | ||
import { addNodeProperties } from './utils/add-node-props.js'; | ||
const schemaBuilderProto = SchemaBuilder.prototype; | ||
@@ -153,60 +154,28 @@ const pageInfoRefMap = new WeakMap(); | ||
}; | ||
schemaBuilderProto.node = function node(param, { interfaces, extensions, id, ...options }, fields) { | ||
schemaBuilderProto.nodeRef = function nodeRef(param, options) { | ||
if (typeof param === "string") { | ||
return new ImplementableNodeRef(this, param, options); | ||
} | ||
addNodeProperties(param.name, this, param, undefined, options); | ||
return param; | ||
}; | ||
schemaBuilderProto.node = function node(param, { id, name, loadMany, loadOne, loadWithoutCache, loadManyWithoutCache, brandLoadedObjects, ...options }, fields) { | ||
verifyRef(param); | ||
const interfacesWithNode = () => [ | ||
this.nodeInterfaceRef(), | ||
...typeof interfaces === "function" ? interfaces() : interfaces !== null && interfaces !== void 0 ? interfaces : [] | ||
]; | ||
let nodeName; | ||
var _options_isTypeOf; | ||
const ref = this.objectType(param, { | ||
...options, | ||
extensions: { | ||
...extensions, | ||
pothosParseGlobalID: id.parse | ||
}, | ||
isTypeOf: (_options_isTypeOf = options.isTypeOf) !== null && _options_isTypeOf !== void 0 ? _options_isTypeOf : typeof param === "function" ? (maybeNode, _context, _info) => { | ||
if (!maybeNode) { | ||
return false; | ||
} | ||
if (maybeNode instanceof param) { | ||
return true; | ||
} | ||
const proto = Object.getPrototypeOf(maybeNode); | ||
try { | ||
if (proto === null || proto === void 0 ? void 0 : proto.constructor) { | ||
const config = this.configStore.getTypeConfig(proto.constructor); | ||
return config.name === nodeName; | ||
} | ||
} | ||
catch { | ||
// ignore | ||
} | ||
return false; | ||
} : undefined, | ||
interfaces: interfacesWithNode | ||
const nodeName = typeof param === "string" ? param : param instanceof BaseTypeRef ? param.name : name; | ||
const ref = new NodeRef(this, nodeName, param, { | ||
id, | ||
loadMany, | ||
loadOne, | ||
loadWithoutCache, | ||
loadManyWithoutCache, | ||
brandLoadedObjects | ||
}); | ||
if (typeof param !== "string") { | ||
this.configStore.associateParamWithRef(param, ref); | ||
} | ||
this.objectType(ref, { | ||
name: nodeName, | ||
...options | ||
}, fields); | ||
this.configStore.onTypeConfig(ref, (nodeConfig) => { | ||
var _this_options_relay; | ||
nodeName = nodeConfig.name; | ||
var _this_options_relay_idFieldName; | ||
this.objectField(ref, (_this_options_relay_idFieldName = (_this_options_relay = this.options.relay) === null || _this_options_relay === void 0 ? void 0 : _this_options_relay.idFieldName) !== null && _this_options_relay_idFieldName !== void 0 ? _this_options_relay_idFieldName : "id", (t) => { | ||
var _this_options_relay; | ||
return t.globalID({ | ||
nullable: false, | ||
...(_this_options_relay = this.options.relay) === null || _this_options_relay === void 0 ? void 0 : _this_options_relay.idFieldOptions, | ||
...id, | ||
args: {}, | ||
resolve: (parent, args, context, info) => completeValue(id.resolve(parent, args, context, info), (globalId) => ({ | ||
type: nodeConfig.name, | ||
id: globalId | ||
})) | ||
}); | ||
}); | ||
}); | ||
const nodeRef = new NodeRef(ref.name, { | ||
parseId: id.parse | ||
}); | ||
this.configStore.associateParamWithRef(nodeRef, ref); | ||
return nodeRef; | ||
return ref; | ||
}; | ||
@@ -213,0 +182,0 @@ schemaBuilderProto.globalConnectionField = function globalConnectionField(name, field) { |
@@ -144,12 +144,13 @@ import { type ArgumentRef, type EmptyToOptional, type FieldKind, type FieldNullability, type FieldOptionsFromKind, type FieldRequiredness, type InferredFieldOptionKeys, type InputFieldMap, type InputFieldRef, type InputFieldsFromShape, type InputRef, type InputShapeFromFields, type InterfaceParam, type MaybePromise, type Normalize, type ObjectFieldsShape, type ObjectParam, type ObjectRef, type ObjectTypeOptions, type OutputRef, type OutputRefShape, type OutputShape, type OutputType, type ParentShape, type Resolver, type SchemaTypes, type ShapeFromListTypeParam, type ShapeFromTypeParam, inputShapeKey } from '@pothos/core'; | ||
export type NodeBaseObjectOptionsForParam<Types extends SchemaTypes, Param extends ObjectParam<Types>, Interfaces extends InterfaceParam<Types>[]> = ObjectTypeOptions<Types, Param, ParentShape<Types, Param>, Interfaces>; | ||
export type NodeObjectOptions<Types extends SchemaTypes, Param extends ObjectParam<Types>, Interfaces extends InterfaceParam<Types>[], IDShape = string> = NodeBaseObjectOptionsForParam<Types, Param, Interfaces> & { | ||
id: Omit<FieldOptionsFromKind<Types, ParentShape<Types, Param>, "ID", false, {}, "Object", OutputShape<Types, "ID">, MaybePromise<OutputShape<Types, "ID">>>, "args" | "nullable" | "type"> & { | ||
export type NodeRefOptions<Types extends SchemaTypes, T, P = T, IDShape = string> = { | ||
id: Omit<FieldOptionsFromKind<Types, P, "ID", false, {}, "Object", OutputShape<Types, "ID">, MaybePromise<OutputShape<Types, "ID">>>, "args" | "nullable" | "type"> & { | ||
parse?: (id: string, ctx: Types["Context"]) => IDShape; | ||
}; | ||
brandLoadedObjects?: boolean; | ||
loadOne?: (id: IDShape, context: Types["Context"]) => MaybePromise<OutputShape<Types, Param> | null | undefined>; | ||
loadMany?: (ids: IDShape[], context: Types["Context"]) => MaybePromise<readonly MaybePromise<OutputShape<Types, Param> | null | undefined>[]>; | ||
loadWithoutCache?: (id: IDShape, context: Types["Context"], info: GraphQLResolveInfo) => MaybePromise<OutputShape<Types, Param> | null | undefined>; | ||
loadManyWithoutCache?: (ids: IDShape[], context: Types["Context"]) => MaybePromise<readonly MaybePromise<OutputShape<Types, Param> | null | undefined>[]>; | ||
loadOne?: (id: IDShape, context: Types["Context"]) => MaybePromise<T | null | undefined>; | ||
loadMany?: (ids: IDShape[], context: Types["Context"]) => MaybePromise<readonly MaybePromise<T | null | undefined>[]>; | ||
loadWithoutCache?: (id: IDShape, context: Types["Context"], info: GraphQLResolveInfo) => MaybePromise<T | null | undefined>; | ||
loadManyWithoutCache?: (ids: IDShape[], context: Types["Context"]) => MaybePromise<readonly MaybePromise<T | null | undefined>[]>; | ||
}; | ||
export type NodeObjectOptions<Types extends SchemaTypes, Param extends ObjectParam<Types>, Interfaces extends InterfaceParam<Types>[], IDShape = string> = NodeBaseObjectOptionsForParam<Types, Param, Interfaces> & NodeRefOptions<Types, OutputShape<Types, Param>, ParentShape<Types, Param>, IDShape>; | ||
export type GlobalIDFieldOptions<Types extends SchemaTypes, ParentShape, Args extends InputFieldMap, Nullable extends boolean, ResolveReturnShape, Kind extends FieldKind = FieldKind> = Omit<FieldOptionsFromKind<Types, ParentShape, "ID", Nullable, Args, Kind, ParentShape, ResolveReturnShape>, "type" | InferredFieldOptionKeys> & { | ||
@@ -156,0 +157,0 @@ resolve: Resolver<ParentShape, InputShapeFromFields<Args>, Types["Context"], ShapeFromTypeParam<Types, OutputRefShape<GlobalIDShape<Types> | string>, true>, ResolveReturnShape>; |
@@ -12,2 +12,5 @@ "use strict"; | ||
_export(exports, { | ||
ImplementableNodeRef: function() { | ||
return ImplementableNodeRef; | ||
}, | ||
NodeRef: function() { | ||
@@ -21,2 +24,3 @@ return NodeRef; | ||
const _core = require("@pothos/core"); | ||
const _addnodeprops = require("./utils/add-node-props"); | ||
function _define_property(obj, key, value) { | ||
@@ -38,10 +42,16 @@ if (key in obj) { | ||
class NodeRef extends _core.ObjectRef { | ||
constructor(name, options){ | ||
super(name); | ||
_define_property(this, _relayIDShapeKey, void 0); | ||
_define_property(this, "parseId", void 0); | ||
this.parseId = options.parseId; | ||
constructor(builder, name, param, options){ | ||
super(name), _define_property(this, _relayIDShapeKey, void 0), _define_property(this, "parseId", void 0); | ||
this.parseId = options.id.parse; | ||
(0, _addnodeprops.addNodeProperties)(name, builder, this, param, options); | ||
} | ||
} | ||
class ImplementableNodeRef extends _core.ImplementableObjectRef { | ||
constructor(builder, name, options){ | ||
super(builder, name), _define_property(this, "parseId", void 0); | ||
this.parseId = options.id.parse; | ||
(0, _addnodeprops.addNodeProperties)(name, builder, this, undefined, options); | ||
} | ||
} | ||
//# sourceMappingURL=node-ref.js.map |
@@ -23,2 +23,3 @@ "use strict"; | ||
const _utils = require("./utils"); | ||
const _addnodeprops = require("./utils/add-node-props"); | ||
function _getRequireWildcardCache(nodeInterop) { | ||
@@ -212,59 +213,28 @@ if (typeof WeakMap !== "function") return null; | ||
}; | ||
schemaBuilderProto.node = function node(param, { interfaces, extensions, id, ...options }, fields) { | ||
schemaBuilderProto.nodeRef = function nodeRef(param, options) { | ||
if (typeof param === 'string') { | ||
return new _noderef.ImplementableNodeRef(this, param, options); | ||
} | ||
(0, _addnodeprops.addNodeProperties)(param.name, this, param, undefined, options); | ||
return param; | ||
}; | ||
schemaBuilderProto.node = function node(param, { id, name, loadMany, loadOne, loadWithoutCache, loadManyWithoutCache, brandLoadedObjects, ...options }, fields) { | ||
(0, _core.verifyRef)(param); | ||
const interfacesWithNode = ()=>[ | ||
this.nodeInterfaceRef(), | ||
...typeof interfaces === 'function' ? interfaces() : interfaces !== null && interfaces !== void 0 ? interfaces : [] | ||
]; | ||
let nodeName; | ||
var _options_isTypeOf; | ||
const ref = this.objectType(param, { | ||
...options, | ||
extensions: { | ||
...extensions, | ||
pothosParseGlobalID: id.parse | ||
}, | ||
isTypeOf: (_options_isTypeOf = options.isTypeOf) !== null && _options_isTypeOf !== void 0 ? _options_isTypeOf : typeof param === 'function' ? (maybeNode, _context, _info)=>{ | ||
if (!maybeNode) { | ||
return false; | ||
} | ||
if (maybeNode instanceof param) { | ||
return true; | ||
} | ||
const proto = Object.getPrototypeOf(maybeNode); | ||
try { | ||
if (proto === null || proto === void 0 ? void 0 : proto.constructor) { | ||
const config = this.configStore.getTypeConfig(proto.constructor); | ||
return config.name === nodeName; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
return false; | ||
} : undefined, | ||
interfaces: interfacesWithNode | ||
const nodeName = typeof param === 'string' ? param : param instanceof _core.BaseTypeRef ? param.name : name; | ||
const ref = new _noderef.NodeRef(this, nodeName, param, { | ||
id, | ||
loadMany, | ||
loadOne, | ||
loadWithoutCache, | ||
loadManyWithoutCache, | ||
brandLoadedObjects | ||
}); | ||
if (typeof param !== 'string') { | ||
this.configStore.associateParamWithRef(param, ref); | ||
} | ||
this.objectType(ref, { | ||
name: nodeName, | ||
...options | ||
}, fields); | ||
this.configStore.onTypeConfig(ref, (nodeConfig)=>{ | ||
var _this_options_relay; | ||
nodeName = nodeConfig.name; | ||
var _this_options_relay_idFieldName; | ||
this.objectField(ref, (_this_options_relay_idFieldName = (_this_options_relay = this.options.relay) === null || _this_options_relay === void 0 ? void 0 : _this_options_relay.idFieldName) !== null && _this_options_relay_idFieldName !== void 0 ? _this_options_relay_idFieldName : 'id', (t)=>{ | ||
var _this_options_relay; | ||
return t.globalID({ | ||
nullable: false, | ||
...(_this_options_relay = this.options.relay) === null || _this_options_relay === void 0 ? void 0 : _this_options_relay.idFieldOptions, | ||
...id, | ||
args: {}, | ||
resolve: (parent, args, context, info)=>(0, _core.completeValue)(id.resolve(parent, args, context, info), (globalId)=>({ | ||
type: nodeConfig.name, | ||
id: globalId | ||
})) | ||
}); | ||
}); | ||
}); | ||
const nodeRef = new _noderef.NodeRef(ref.name, { | ||
parseId: id.parse | ||
}); | ||
this.configStore.associateParamWithRef(nodeRef, ref); | ||
return nodeRef; | ||
return ref; | ||
}; | ||
@@ -271,0 +241,0 @@ schemaBuilderProto.globalConnectionField = function globalConnectionField(name, field) { |
{ | ||
"name": "@pothos/plugin-relay", | ||
"version": "4.2.1", | ||
"version": "4.3.0", | ||
"description": "A Pothos plugin for adding relay style connections, nodes, and cursor based pagination to your GraphQL schema", | ||
@@ -45,6 +45,6 @@ "main": "./lib/index.js", | ||
"devDependencies": { | ||
"graphql": "^16.8.1", | ||
"graphql": ">=16.6.0", | ||
"graphql-subscriptions": "^2.0.0", | ||
"graphql-tag": "^2.12.6", | ||
"@pothos/core": "4.2.0", | ||
"@pothos/core": "4.3.0", | ||
"@pothos/plugin-complexity": "4.1.0", | ||
@@ -51,0 +51,0 @@ "@pothos/test-utils": "2.1.0" |
@@ -26,3 +26,3 @@ import { | ||
} from '@pothos/core'; | ||
import type { NodeRef } from './node-ref'; | ||
import type { ImplementableNodeRef, NodeRef } from './node-ref'; | ||
import type { | ||
@@ -43,2 +43,3 @@ ConnectionResultShape, | ||
NodeObjectOptions, | ||
NodeRefOptions, | ||
PageInfoShape, | ||
@@ -92,2 +93,8 @@ RelayMutationFieldOptions, | ||
nodeRef: <Shape, IDShape = string, Param extends string | ObjectRef<Types, Shape> = string>( | ||
param: Param, | ||
options: NodeRefOptions<Types, Shape, Shape, IDShape>, | ||
fields?: ObjectFieldsShape<Types, Shape>, | ||
) => Param extends string ? ImplementableNodeRef<Types, Shape, Shape, IDShape> : Param; | ||
node: < | ||
@@ -94,0 +101,0 @@ Interfaces extends InterfaceParam<Types>[], |
@@ -1,2 +0,9 @@ | ||
import { ObjectRef, type SchemaTypes } from '@pothos/core'; | ||
import { | ||
ImplementableObjectRef, | ||
type ObjectParam, | ||
ObjectRef, | ||
type SchemaTypes, | ||
} from '@pothos/core'; | ||
import type { NodeRefOptions } from './types'; | ||
import { addNodeProperties } from './utils/add-node-props'; | ||
@@ -15,10 +22,33 @@ export const relayIDShapeKey = Symbol.for('Pothos.relayIDShapeKey'); | ||
constructor( | ||
builder: PothosSchemaTypes.SchemaBuilder<Types>, | ||
name: string, | ||
options: { | ||
parseId?: (id: string, ctx: object) => IDShape; | ||
}, | ||
param: ObjectParam<Types>, | ||
options: NodeRefOptions<Types, T, P, IDShape>, | ||
) { | ||
super(name); | ||
this.parseId = options.parseId; | ||
this.parseId = options.id.parse; | ||
addNodeProperties(name, builder, this, param, options); | ||
} | ||
} | ||
export class ImplementableNodeRef< | ||
Types extends SchemaTypes, | ||
T, | ||
P = T, | ||
IDShape = string, | ||
> extends ImplementableObjectRef<Types, T, P> { | ||
parseId: ((id: string, ctx: object) => IDShape) | undefined; | ||
constructor( | ||
builder: PothosSchemaTypes.SchemaBuilder<Types>, | ||
name: string, | ||
options: NodeRefOptions<Types, T, P, IDShape>, | ||
) { | ||
super(builder, name); | ||
this.parseId = options.id.parse; | ||
addNodeProperties(name, builder, this, undefined, options); | ||
} | ||
} |
import SchemaBuilder, { | ||
BaseTypeRef, | ||
completeValue, | ||
@@ -8,7 +9,4 @@ createContextCache, | ||
InputObjectRef, | ||
type InterfaceParam, | ||
type InterfaceRef, | ||
type MaybePromise, | ||
type ObjectFieldsShape, | ||
type ObjectParam, | ||
ObjectRef, | ||
@@ -20,6 +18,7 @@ type OutputRef, | ||
} from '@pothos/core'; | ||
import { type GraphQLResolveInfo, defaultTypeResolver } from 'graphql'; | ||
import { NodeRef } from './node-ref'; | ||
import type { ConnectionShape, GlobalIDShape, PageInfoShape } from './types'; | ||
import { defaultTypeResolver } from 'graphql'; | ||
import { ImplementableNodeRef, NodeRef } from './node-ref'; | ||
import type { ConnectionShape, PageInfoShape } from './types'; | ||
import { capitalize, resolveNodes } from './utils'; | ||
import { addNodeProperties } from './utils/add-node-props'; | ||
@@ -231,47 +230,49 @@ const schemaBuilderProto = SchemaBuilder.prototype as PothosSchemaTypes.SchemaBuilder<SchemaTypes>; | ||
schemaBuilderProto.node = function node(param, { interfaces, extensions, id, ...options }, fields) { | ||
verifyRef(param); | ||
const interfacesWithNode: () => InterfaceParam<SchemaTypes>[] = () => [ | ||
this.nodeInterfaceRef(), | ||
...(typeof interfaces === 'function' ? interfaces() : interfaces ?? []), | ||
]; | ||
schemaBuilderProto.nodeRef = function nodeRef(param, options) { | ||
if (typeof param === 'string') { | ||
return new ImplementableNodeRef(this, param, options) as never; | ||
} | ||
let nodeName!: string; | ||
addNodeProperties(param.name, this, param, undefined, options); | ||
const ref = this.objectType<[], ObjectParam<SchemaTypes>>( | ||
param, | ||
{ | ||
...(options as {}), | ||
extensions: { | ||
...extensions, | ||
pothosParseGlobalID: id.parse, | ||
}, | ||
isTypeOf: | ||
options.isTypeOf ?? | ||
(typeof param === 'function' | ||
? (maybeNode: unknown, _context: object, _info: GraphQLResolveInfo) => { | ||
if (!maybeNode) { | ||
return false; | ||
} | ||
return param as never; | ||
}; | ||
if (maybeNode instanceof (param as Function)) { | ||
return true; | ||
} | ||
schemaBuilderProto.node = function node( | ||
param, | ||
{ | ||
id, | ||
name, | ||
loadMany, | ||
loadOne, | ||
loadWithoutCache, | ||
loadManyWithoutCache, | ||
brandLoadedObjects, | ||
...options | ||
}, | ||
fields, | ||
) { | ||
verifyRef(param); | ||
const proto = Object.getPrototypeOf(maybeNode) as { constructor: unknown }; | ||
const nodeName = | ||
typeof param === 'string' ? param : param instanceof BaseTypeRef ? param.name : name!; | ||
try { | ||
if (proto?.constructor) { | ||
const config = this.configStore.getTypeConfig(proto.constructor as OutputRef); | ||
const ref = new NodeRef(this, nodeName, param, { | ||
id, | ||
loadMany, | ||
loadOne, | ||
loadWithoutCache, | ||
loadManyWithoutCache, | ||
brandLoadedObjects, | ||
}); | ||
return config.name === nodeName; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
if (typeof param !== 'string') { | ||
this.configStore.associateParamWithRef(param, ref); | ||
} | ||
return false; | ||
} | ||
: undefined), | ||
interfaces: interfacesWithNode as () => [], | ||
this.objectType( | ||
ref, | ||
{ | ||
name: nodeName, | ||
...options, | ||
}, | ||
@@ -281,27 +282,3 @@ fields, | ||
this.configStore.onTypeConfig(ref, (nodeConfig) => { | ||
nodeName = nodeConfig.name; | ||
this.objectField(ref, this.options.relay?.idFieldName ?? 'id', (t) => | ||
t.globalID<{}, false, MaybePromise<GlobalIDShape<SchemaTypes>>>({ | ||
nullable: false, | ||
...this.options.relay?.idFieldOptions, | ||
...id, | ||
args: {}, | ||
resolve: (parent, args, context, info): MaybePromise<GlobalIDShape<SchemaTypes>> => | ||
completeValue(id.resolve(parent, args, context, info), (globalId) => ({ | ||
type: nodeConfig.name, | ||
id: globalId, | ||
})), | ||
}), | ||
); | ||
}); | ||
const nodeRef = new NodeRef(ref.name, { | ||
parseId: id.parse, | ||
}); | ||
this.configStore.associateParamWithRef(nodeRef, ref); | ||
return nodeRef as never; | ||
return ref as never; | ||
}; | ||
@@ -308,0 +285,0 @@ |
@@ -436,12 +436,7 @@ import { | ||
export type NodeObjectOptions< | ||
Types extends SchemaTypes, | ||
Param extends ObjectParam<Types>, | ||
Interfaces extends InterfaceParam<Types>[], | ||
IDShape = string, | ||
> = NodeBaseObjectOptionsForParam<Types, Param, Interfaces> & { | ||
export type NodeRefOptions<Types extends SchemaTypes, T, P = T, IDShape = string> = { | ||
id: Omit< | ||
FieldOptionsFromKind< | ||
Types, | ||
ParentShape<Types, Param>, | ||
P, | ||
'ID', | ||
@@ -459,10 +454,7 @@ false, | ||
brandLoadedObjects?: boolean; | ||
loadOne?: ( | ||
id: IDShape, | ||
context: Types['Context'], | ||
) => MaybePromise<OutputShape<Types, Param> | null | undefined>; | ||
loadOne?: (id: IDShape, context: Types['Context']) => MaybePromise<T | null | undefined>; | ||
loadMany?: ( | ||
ids: IDShape[], | ||
context: Types['Context'], | ||
) => MaybePromise<readonly MaybePromise<OutputShape<Types, Param> | null | undefined>[]>; | ||
) => MaybePromise<readonly MaybePromise<T | null | undefined>[]>; | ||
loadWithoutCache?: ( | ||
@@ -472,9 +464,17 @@ id: IDShape, | ||
info: GraphQLResolveInfo, | ||
) => MaybePromise<OutputShape<Types, Param> | null | undefined>; | ||
) => MaybePromise<T | null | undefined>; | ||
loadManyWithoutCache?: ( | ||
ids: IDShape[], | ||
context: Types['Context'], | ||
) => MaybePromise<readonly MaybePromise<OutputShape<Types, Param> | null | undefined>[]>; | ||
) => MaybePromise<readonly MaybePromise<T | null | undefined>[]>; | ||
}; | ||
export type NodeObjectOptions< | ||
Types extends SchemaTypes, | ||
Param extends ObjectParam<Types>, | ||
Interfaces extends InterfaceParam<Types>[], | ||
IDShape = string, | ||
> = NodeBaseObjectOptionsForParam<Types, Param, Interfaces> & | ||
NodeRefOptions<Types, OutputShape<Types, Param>, ParentShape<Types, Param>, IDShape>; | ||
export type GlobalIDFieldOptions< | ||
@@ -481,0 +481,0 @@ Types extends SchemaTypes, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
470129
122
5988