-
Still a little new, but starting to get the gist of things except for this one thing... Issue Faced
Now However, I'd expect the type for Why it's an issueThe issue this causes is that now when you define a query, you have to resolve the whole model even if that's not necessarily required.
So if you want to say write a more constrained Possible Workarounds
This seems bad for many reasons, including the fact that it seems like a goal of Pothos is to take the underlying data model itself and use So option 1) would still be functional in theory but redundant, idk how simple it'd actually be but I'd be surprised if something doesn't already exists that I'm just missing here. Could someone please point me in the right direction, hopefully I'm just missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The relationship between typescript types and GraphQL types is important to understand, and isn't intuitive when you are only looking at basic examples. A good way to think about it is that the typescript types represent your data, and your GraphQL types represent your desired output, and Pothos helps ensure that the way this data is mapped is type-safe. The typescript types do not in any way need to reflect the shape of the GraphQL API. When you define an const User = builder.objectRef<{ id: string, firstName: string, lastName: string }>('User') you are declaring the data that will be used throughout your schema to represent a User. Any field that returns a User will need to return an object that matches the type you defined here for the ref. The fields you will define later for the When you define fields for the User.implement({
fullName: t.string({
resolve: (user) => `${user.firstName} ${user.lastName}`
}),
isAdmin: (user) => isAdmin(user.id),
}) the User.implement({
firstName: t.exposeString('firstName'),
// is short hand for
firstName: t.field({
type: 'String',
resolve: (user) => user.firstName
}),
}) The original type passed when creating the ref also affects other APIs: // the user type provided for auth checks for the scope-auth plugin
User.implement({
address: t.field({
type: 'String',
authScopes: (user, ctx) => user.id === ctx.user.id // only allow reading your own address
resolve: (user) => getUserAddress(user.id)
}),
}) If you just want to define objects where the data is the same as what would be returned from the API, you can use the simple-objects plugin, which doesn't require types or resolvers: const UserType = builder.simpleObject(
'User',
{
fields: (t) => ({
firstName: t.string(),
lastName: t.string(),
}),
},
);
builder.queryType({
fields: (t) => ({
user: t.field({
type: UserType,
args: {
id: t.arg.id({ required: true }),
},
resolve: (parent, args, { User }) => {
return {
id: '1003',
firstName: 'Leia',
lastName: 'Organa',
};
},
}),
}),
}); |
Beta Was this translation helpful? Give feedback.
The relationship between typescript types and GraphQL types is important to understand, and isn't intuitive when you are only looking at basic examples.
A good way to think about it is that the typescript types represent your data, and your GraphQL types represent your desired output, and Pothos helps ensure that the way this data is mapped is type-safe. The typescript types do not in any way need to reflect the shape of the GraphQL API.
When you define an
objectRef
you are declaring the data that will be used throughout your schema to represent a User. Any field that returns a User will need to re…