Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding fields to @pothos/plugin-relay's Node interface #1252

Closed
ewen-lbh opened this issue Jul 22, 2024 · 2 comments
Closed

Adding fields to @pothos/plugin-relay's Node interface #1252

ewen-lbh opened this issue Jul 22, 2024 · 2 comments

Comments

@ewen-lbh
Copy link
Contributor

is adding additional fields on the Node interface supported? i didn't see anything mentionned in the docs.

it worked nicely before v4 using builder.interfaceField(...) (for example: https://github.com/inp-net/churros/blob/22cf7d1eff32d904d34de336fe9bb844c3a5ea6e/packages/api/src/modules/global/resolvers/node.local-id.ts)

but i'm now getting a GraphQLError:

PothosSchemaError [GraphQLError]: Node has not been implemented

...and a kinda ugly, really deep typescript type mismatch error

i discovered that the relay plugin exports NodeRef, so I tried

import { builder, localID } from '#lib';
import type { SchemaTypes } from '@pothos/core';
import { NodeRef } from '@pothos/plugin-relay';
import { LocalID } from '../types/local-id.js';

builder.interfaceField(NodeRef<SchemaTypes, { id: string }>, 'localID', (t) =>
  t.field({
    type: LocalID,
    resolve: ({ id }) => localID(id),
    description: "L'identifiant local de la ressource (sans préfixe)",
  }),
);

(localID is a simple (id: string) => string function, builder is a Pothos builder)

but I get another error, with a seemingly similar root cause:

[@churros/api]: PothosSchemaError [GraphQLError]: Missing implementations for some references (class NodeRef extends ObjectRef {
[@churros/api]:     constructor(name, options) {
[@churros/api]:         super(name);
[@churros/api]:         _define_property(this, _relayIDShapeKey, void 0);
[@churros/api]:         _define_property(this, "parseId", void 0);
[@churros/api]:         this.parseId = options.parseId;
[@churros/api]:     }
[@churros/api]: }).
[@churros/api]:     at constructor (/home/uwun/projects.local/churros/node_modules/@pothos/core/src/errors.ts:7:5)
[@churros/api]:     at constructor (/home/uwun/projects.local/churros/node_modules/@pothos/core/src/errors.ts:14:5)
[@churros/api]:     at ConfigStore.prepareForBuild (/home/uwun/projects.local/churros/node_modules/@pothos/core/src/config-store.ts:339:5)
[@churros/api]:     at BuildCache.buildAll (/home/uwun/projects.local/churros/node_modules/@pothos/core/src/build-cache.ts:166:22)
[@churros/api]:     at SchemaBuilder.toSchema (/home/uwun/projects.local/churros/node_modules/@pothos/core/src/builder.ts:668:11)
[@churros/api]:     at <anonymous> (/home/uwun/projects.local/churros/packages/api/src/schema.ts:35:61) {
[@churros/api]:   path: undefined,
[@churros/api]:   locations: undefined,
[@churros/api]:   extensions: [Object: null prototype] {}
[@churros/api]: }

Note that the API works really well without that .interfaceField call, so Node seems to be implemented correctly, it just seems like it's implemented after that builder.interfaceField call?

I'm certain the file making that call is imported after the builder is called, so I'd expect Node to be implemented at that point.

Is there a way to add fields on the Node interface?

@ewen-lbh ewen-lbh changed the title Adding fields to @pothos/prisma-relay's Node interface Adding fields to @pothos/plugin-relay's Node interface Jul 22, 2024
@hayes
Copy link
Owner

hayes commented Jul 22, 2024

I think this was somewhat coincidental that this worked before, but the correct way to handle this is pretty straight forward. You can use builder.nodeInterfaceRef() to get a ref that can be passed to builder.interfaceField: https://stackblitz.com/edit/typescript-v6gz5m?file=index.ts

@ewen-lbh
Copy link
Contributor Author

Thanks! I created a PR to mention this in the docs (if it's not already there... i'm always scared of asking something because i missed it in the docs, even after looking through it a thousand times lol)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants