Skip to content

Commit

Permalink
Merge pull request #74 from hayes/mh-with-input
Browse files Browse the repository at this point in the history
feature: add with-input plugin
  • Loading branch information
hayes committed Apr 4, 2022
2 parents e57093c + 80c88a9 commit 14bd6fe
Show file tree
Hide file tree
Showing 74 changed files with 2,082 additions and 342 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-rats-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-dataloader': minor
---

Add loadableObjectRef loadableInterfaceRef and loadableNodeRef
5 changes: 5 additions & 0 deletions .changeset/healthy-cobras-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/deno': minor
---

update deno packages
7 changes: 7 additions & 0 deletions .changeset/hip-baboons-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@pothos/core': minor
'@pothos/deno': minor
'@pothos/plugin-relay': minor
---

Fix type for InputFieldRef.kind
5 changes: 5 additions & 0 deletions .changeset/little-singers-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-with-input': major
---

Initial release of with-input plugin
2 changes: 1 addition & 1 deletion packages/core/src/refs/input-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class InputFieldRef<
T = unknown,
Kind extends 'Arg' | 'InputObject' = 'Arg' | 'InputObject',
> {
kind: 'Arg' | 'InputObject';
kind: Kind;

parentTypename: string;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/refs/input-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class ImplementableInputObjectRef<
Types extends SchemaTypes,
T extends object,
> extends InputObjectRef<RecursivelyNormalizeNullableFields<T>> {
private builder: PothosSchemaTypes.SchemaBuilder<Types>;
protected builder: PothosSchemaTypes.SchemaBuilder<Types>;

constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, name: string) {
super(name);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/refs/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ImplementableInterfaceRef<
Shape,
Parent = Shape,
> extends InterfaceRef<Shape, Parent> {
private builder: PothosSchemaTypes.SchemaBuilder<Types>;
protected builder: PothosSchemaTypes.SchemaBuilder<Types>;

constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, name: string) {
super(name);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/refs/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ImplementableObjectRef<
Shape,
Parent = Shape,
> extends ObjectRef<Shape, Parent> {
private builder: PothosSchemaTypes.SchemaBuilder<Types>;
protected builder: PothosSchemaTypes.SchemaBuilder<Types>;

constructor(builder: PothosSchemaTypes.SchemaBuilder<Types>, name: string) {
super(name);
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/types/builder-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export type SubscriptionFieldThunk<Types extends SchemaTypes> = (

export type FieldMap = Record<string, FieldRef>;

export type InputFieldMap = Record<string, InputFieldRef>;
export type InputFieldMap<Kind extends 'Arg' | 'InputObject' = 'Arg' | 'InputObject'> = Record<
string,
InputFieldRef<unknown, Kind>
>;

export type FieldOptionsFromKind<
Types extends SchemaTypes,
Expand Down
2 changes: 1 addition & 1 deletion packages/deno/packages/core/refs/input-field.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/refs/input-object.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/refs/interface.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/refs/object.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/builder-options.ts

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

74 changes: 74 additions & 0 deletions packages/deno/packages/plugin-dataloader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,26 @@ builder.objectField(User, 'posts', (t) =>
);
```

### loadableList fields for one-to-many relations

`loadable` fields can return lists, but do not work for loading a list of records from a single id.

The `loadableList` method can be used to define loadable fields that represent this kind of
relationship.

```typescript
// Loading multiple Posts
builder.objectField(User, 'posts', (t) =>
t.loadableList({
// type is singular, but will create a list field
type: Post,
// will be called with ids of all the users, and should return `Post[][]`
load: (ids: number[], context) => context.postsByUserIds(ids),
resolve: (user, args) => user.id,
}),
);
```

### dataloader options

You can provide additional options for your dataloaders using `loaderOptions`.
Expand Down Expand Up @@ -293,6 +313,60 @@ const UserNode = builder.loadableNode('UserNode', {
});
```

### Loadable Refs and Circular references

You may run into type errors if you define 2 loadable objects that circularly reference each other
in their definitions.

There are a some general strategies to avoid this outlined in the
[circular-references guide](../guide/circular-references).

This plug also has methods for creating refs (similar to `builder.objectRef`) that can be used to
split the definition and implementation of your types to avoid any issues with circular references.

```typescript
const User = builder.loadableObjectRef('User', {
load: (ids: string[], context: ContextType) => context.loadUsersById(ids),
});

User.implement({
fields: (t) => ({
id: t.exposeID('id', {}),
}),
});

// Or with relay
const UserNode = builder.loadableNodeRef('UserNode', {
load: (ids: string[], context: ContextType) => context.loadUsersById(ids),
id: {
resolve: (user) => user.id,
},
});

UserNode.implement({
isTypeOf: (obj) => obj instanceof User,
fields: (t) => ({}),
});
```

All the plugin specific options should be passed when defining the ref. This allows the ref to be
used by any method that accepts a ref to implement an object:

```typescript
const User = builder.loadableObjectRef('User', {
load: (ids: string[], context: ContextType) => context.loadUsersById(ids),
});

builder.objectType(User, {
fields: (t) => ({
id: t.exposeID('id', {}),
}),
});
```

The above example is not useful on its own, but this pattern will allow these refs to be used with
other that also allow you to define object types with additional behaviors.

### Caching resources loaded manually in a resolver

When manually loading a resource in a resolver it is not automatically added to the dataloader
Expand Down
10 changes: 7 additions & 3 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.

1 change: 1 addition & 0 deletions packages/deno/packages/plugin-dataloader/refs/index.ts

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

25 changes: 24 additions & 1 deletion packages/deno/packages/plugin-dataloader/refs/interface.ts

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

45 changes: 45 additions & 0 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.

25 changes: 24 additions & 1 deletion packages/deno/packages/plugin-dataloader/refs/object.ts

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

1 comment on commit 14bd6fe

@vercel
Copy link

@vercel vercel bot commented on 14bd6fe Apr 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.