Comments (4)
The default isTypeOf only works if you use a class as the type param, or the shape associated with the type ref has a _type property that can be used to match again.
Something like:
builder.node(SomeClass, {... options})
or builder.node(builder.objectRef<{_type: 'SomeType'}>('SomeType'), {... options})
On a phone, so not sure how clear that is. If you are using one of these patterns, and still seeing type errors, some more context on how you are calling the node function would be helpful
from pothos.
I'm at work right now, i can give a proper example in a few days. But i just tried your suggestion
builder.node(builder.objectRef<{ _type: 'SomeType'; id: string }>('SomeType'), {
id: {
resolve: (node) => node.id
},
fields: (t) => ({ id: t.exposeID('id') })
});
It's still yelling about isTypeOf
is required.
If i create a class it goes away, but since my data usually is not represented in classes rather coming from prisma
i don't know what the best way is to create nodes.
Also with the objectRef approach would i have to add _type
to every object? Or am i misunderstanding something.
from pothos.
Sorry, small typo there, it should be __type, and yes, you would need to add the type to each instance of the node you resolve.
The issue isTypeOf solves is that when there is a field that resolves to the Node interface, graphql needs be able to figure out which type is actually being resolved. If the node is being loaded through the default node or nodes queries we could determine what type the node is, but for custom fields that return nodes all we know is we got an object back and need to figure out what type it's supposed to be. If it's an instance of a class, or the object has a property that tells us the type, we can create a default isTypeOf method that can use an instanceof check, or read the type property. Lacking those, we don't have another way to figure out what the type should be, which is why it becomes required.
I've got a few ideas on how to make this better in the future, but currently you need to manually provide a way to resolve the type of a node.
In the mean time, I don't have a great solution for Prisma. I know some people actually just add the graphql type into their db. This is obviously not ideal. There is a feature request out in Prisma to include the tabel/model name in query results. This would make it trivial to write the isTypeOf functions. Adding on the __type property manually is not perfect, but it might be the
from pothos.
@hayes That makes much more sense now, thank you! I will go ahead and close this issue for now.
from pothos.
Related Issues (20)
- Getting a weird Typescript error with Prisma plugin HOT 3
- Idea: a guide for incremental adoption of pothos in a nexus/typegraphql project HOT 1
- loadableObject doesn't infer type properly HOT 3
- Support Client Directives in Prisma plugin HOT 3
- Nextjs Error webpack : TypeError: _builder__WEBPACK_IMPORTED_MODULE_1__.builder.prismaObject HOT 2
- Can we use `graphql-scalars` scalars? HOT 10
- how to use edge as output field in relay mutation HOT 6
- [Question] Is there any way to add the prisma types with non prisma types to builder.ObjectType HOT 1
- Prisma Plugin - How to expose "decimal" or other DB types? HOT 4
- Can't use objectType with a generic class HOT 3
- Hardcoded path does not exist HOT 2
- [bug] Pothos Subscriptions with Apollo v4 HOT 2
- `relationCount` errors. on null returned for non-nullable field HOT 3
- Feat: [GraphQL Yoga] Request for access to edge environmental values (Cloudflare KV Namespaces, Vercel Edge Config, etc) HOT 2
- Change the Next.js example to use Yoga? HOT 1
- Trying to pass Options to errorOptions on the Error plugin for Pothos but getting error ``` TypeError: Ref ({ parentTypeName , fieldName })=>`${parentTypeName}${fieldName}` has not been implemented``` when trying to generate schema. HOT 3
- `resolveArrayConnection` returns nullable `ConnectionShape` HOT 2
- Type errors on nullable in Typescript 4.9 HOT 6
- Integrating Apollo cacheControl feature into schema not easily possible HOT 2
- Outdated docs for Sentry envelop usage HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pothos.