Skip to content

Commit

Permalink
Merge pull request hayes#809 from hayes/tgriesser/feat/loadableNodeId
Browse files Browse the repository at this point in the history
Tgriesser/feat/loadable node
  • Loading branch information
hayes authored Feb 23, 2023
2 parents 286f785 + 68c94e4 commit b31928d
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 37 deletions.
7 changes: 7 additions & 0 deletions .changeset/slimy-brooms-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@pothos/plugin-dataloader': minor
'@pothos/plugin-relay': minor
'@pothos/deno': minor
---

Support parsing globalIDs for loadableNode
4 changes: 2 additions & 2 deletions packages/deno/packages/plugin-dataloader/global-types.ts

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

6 changes: 4 additions & 2 deletions packages/deno/packages/plugin-dataloader/refs/node.ts

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

8 changes: 6 additions & 2 deletions packages/deno/packages/plugin-dataloader/schema-builder.ts

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

8 changes: 5 additions & 3 deletions packages/deno/packages/plugin-dataloader/types.ts

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

8 changes: 6 additions & 2 deletions packages/deno/packages/plugin-relay/global-types.ts

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

5 changes: 2 additions & 3 deletions packages/deno/packages/plugin-relay/input-field-builder.ts

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

30 changes: 24 additions & 6 deletions packages/plugin-dataloader/src/global-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ declare global {
options: DataLoaderOptions<Types, Shape, Key, CacheKey>,
) => ImplementableLoadableInterfaceRef<Types, Key | Shape, Shape, Key, CacheKey>;

loadableNodeRef: <Shape extends object, Key extends bigint | number | string, CacheKey = Key>(
loadableNodeRef: <
Shape extends object,
IDShape extends bigint | number | string = string,
Key extends bigint | number | string = IDShape,
CacheKey = Key,
>(
name: string,
options: DataLoaderOptions<Types, Shape, Key, CacheKey> & LoadableNodeId<Types, Shape>,
) => ImplementableLoadableNodeRef<Types, Key | Shape, Shape, Key, CacheKey>;
options: DataLoaderOptions<Types, Shape, Key, CacheKey> &
LoadableNodeId<Types, Shape, IDShape>,
) => ImplementableLoadableNodeRef<Types, Key | Shape, Shape, IDShape, Key, CacheKey>;

loadableUnion: <
Key extends bigint | number | string,
Expand All @@ -101,14 +107,26 @@ declare global {
Shape extends NameOrRef extends ObjectParam<Types>
? ShapeFromTypeParam<Types, NameOrRef, false>
: object,
Key extends bigint | number | string,
Interfaces extends InterfaceParam<Types>[],
NameOrRef extends ObjectParam<Types> | string,
IDShape extends bigint | number | string = string,
Key extends bigint | number | string = IDShape,
CacheKey = Key,
>(
nameOrRef: NameOrRef,
options: LoadableNodeOptions<Types, Shape, Key, Interfaces, NameOrRef, CacheKey>,
) => Omit<LoadableObjectRef<Types, Key | Shape, Shape, Key, CacheKey>, 'implement'>
options: LoadableNodeOptions<
Types,
Shape,
Interfaces,
NameOrRef,
IDShape,
Key,
CacheKey
>,
) => Omit<
ImplementableLoadableNodeRef<Types, Key | Shape, Shape, IDShape, Key, CacheKey>,
'implement'
>
: '@pothos/plugin-relay is required to use this method';
}

Expand Down
9 changes: 6 additions & 3 deletions packages/plugin-dataloader/src/refs/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ export class ImplementableLoadableNodeRef<
Types extends SchemaTypes,
RefShape,
Shape extends object,
Key extends bigint | number | string,
CacheKey,
IDShape extends bigint | number | string = string,
Key extends bigint | number | string = IDShape,
CacheKey = Key,
> extends ImplementableLoadableObjectRef<Types, RefShape, Shape, Key, CacheKey> {
parseId: ((id: string, ctx: object) => IDShape) | undefined;
private idOptions;

constructor(
Expand All @@ -18,10 +20,11 @@ export class ImplementableLoadableNodeRef<
{
id,
...options
}: DataLoaderOptions<Types, Shape, Key, CacheKey> & LoadableNodeId<Types, Shape>,
}: DataLoaderOptions<Types, Shape, Key, CacheKey> & LoadableNodeId<Types, Shape, IDShape>,
) {
super(builder, name, options);
this.idOptions = id;
this.parseId = id.parse;

this.builder.configStore.onTypeConfig(this, (config) => {
const nodeInterface = (
Expand Down
11 changes: 8 additions & 3 deletions packages/plugin-dataloader/src/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ schemaBuilderProto.loadableNode = function loadableNode<
Shape extends NameOrRef extends ObjectParam<SchemaTypes>
? ShapeFromTypeParam<SchemaTypes, NameOrRef, false>
: object,
Key extends DataloaderKey,
Interfaces extends InterfaceParam<SchemaTypes>[],
NameOrRef extends ObjectParam<SchemaTypes> | string,
IDShape extends bigint | number | string = string,
Key extends bigint | number | string = IDShape,
CacheKey = Key,
>(
this: PothosSchemaTypes.SchemaBuilder<SchemaTypes>,
nameOrRef: NameOrRef,
options: LoadableNodeOptions<SchemaTypes, Shape, Key, Interfaces, NameOrRef, CacheKey>,
options: LoadableNodeOptions<SchemaTypes, Shape, Interfaces, NameOrRef, IDShape, Key, CacheKey>,
) {
if (
typeof (this as PothosSchemaTypes.SchemaBuilder<SchemaTypes> & Record<string, unknown>)
Expand All @@ -155,14 +156,18 @@ schemaBuilderProto.loadableNode = function loadableNode<
? nameOrRef
: (options as { name?: string }).name ?? (nameOrRef as { name: string }).name;

const ref = new ImplementableLoadableNodeRef<SchemaTypes, Shape, Shape, Key, CacheKey>(
const ref = new ImplementableLoadableNodeRef<SchemaTypes, Shape, Shape, IDShape, Key, CacheKey>(
this,
name,
options,
);

ref.implement({
...options,
extensions: {
...options.extensions,
pothosParseGlobalID: options.id.parse,
},
isTypeOf:
options.isTypeOf ??
(typeof nameOrRef === 'function'
Expand Down
13 changes: 8 additions & 5 deletions packages/plugin-dataloader/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export interface LoadableRef<K, V, C> {
getDataloader: (context: C) => DataLoader<K, V>;
}

export interface LoadableNodeId<Types extends SchemaTypes, Shape extends object> {
export interface LoadableNodeId<Types extends SchemaTypes, Shape extends object, IDShape> {
id: Omit<
FieldOptionsFromKind<
Types,
Expand All @@ -175,15 +175,18 @@ export interface LoadableNodeId<Types extends SchemaTypes, Shape extends object>
MaybePromise<OutputShape<Types, 'ID'>>
>,
'args' | 'nullable' | 'type'
>;
> & {
parse?: (id: string, ctx: Types['Context']) => IDShape;
};
}

export type LoadableNodeOptions<
Types extends SchemaTypes,
Shape extends object,
Key extends bigint | number | string,
Interfaces extends InterfaceParam<Types>[],
NameOrRef extends ObjectParam<Types> | string,
CacheKey,
IDShape extends bigint | number | string = string,
Key extends bigint | number | string = IDShape,
CacheKey = Key,
> = DataloaderObjectTypeOptions<Types, Shape, Key, Interfaces, NameOrRef, CacheKey> &
LoadableNodeId<Types, Shape>;
LoadableNodeId<Types, Shape, IDShape>;
32 changes: 32 additions & 0 deletions packages/plugin-dataloader/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ interface Error {
message: String!
}
type LoadableParseTest implements Node {
id: ID!
idNumber: Int!
}
interface Node {
id: ID!
}
Expand Down Expand Up @@ -72,6 +77,8 @@ type Query {
fromContext3: User!
fromContext4: [User!]!
fromContext5: [User!]!
loadableParse: LoadableParseTest!
loadableParseNodes(ids: [ID!]): [LoadableParseTest!]!
node(id: ID!): Node
nodes(ids: [ID!]!): [Node]!
nullableUser(id: String): NullableUser
Expand Down Expand Up @@ -255,6 +262,31 @@ exports[`dataloader > queries > query with errors 1`] = `
}
`;

exports[`dataloader > queries > query with global ids 1`] = `
{
"data": {
"loadableParse": {
"id": "TG9hZGFibGVQYXJzZVRlc3Q6MQ==",
"idNumber": 1,
},
"loadableParseNodes": [
{
"id": "TG9hZGFibGVQYXJzZVRlc3Q6MQ==",
"idNumber": 1,
},
{
"id": "TG9hZGFibGVQYXJzZVRlc3Q6Mg==",
"idNumber": 2,
},
{
"id": "TG9hZGFibGVQYXJzZVRlc3Q6MTA=",
"idNumber": 10,
},
],
},
}
`;

exports[`dataloader > queries > sorts loaded results 1`] = `
{
"counts": [
Expand Down
Loading

0 comments on commit b31928d

Please sign in to comment.