Create multiple objectTypes from same prisma model
0xsven opened this issue ยท 5 comments
Perceived Problem
I feel the need to create multiple objectType
s from the same prisma model.
Example:
import { User } from 'nexus-prisma';
export const UserObjectType = objectType({
name: 'User',
definition(t) {
t.field(User.id);
t.field(User.name);
},
});
export const SecretUserObjectType = objectType({
name: 'SecretUser',
definition(t) {
t.field(User.id);
t.field(User.name);
t.field(User.secretValue);
},
});
This would give me the following type error:
Type 'FieldResolver<"User", "id">' is not assignable to type 'FieldResolver<"SecretUser", "id">'.
Ideas / Proposed Solution(s)
It would be great if the types would allow this.
Maybe we need a new helper method.
import { User } from 'nexus-prisma'
export const UserObjectType = objectType({
name: 'User',
definition(t) {
t.field(User.id);
t.field(User.name);
},
});
export const SecretUserObjectType = objectType({
name: 'SecretUser',
definition(t) {
t.field(User.$as('SecretUser').id);
t.field(User.$as('SecretUser').name);
t.field(User.$as('SecretUser').secretValue);
},
})
Or maybe a top level function:
import { User } from 'nexus-prisma'
export const UserObjectType = User.$objectType({
definition(t) {
t.model.id()
t.model.name()
},
});
export const SecretUserObjectType = User.$objectType({
name: 'SecretUser',
definition(t) {
t.model.id()
t.model.name()
t.model.secretValue()
},
})
Or maybe generator config:
import { settings } from 'nexus-prisma/generator'
settings({
modelAliases: {
SecretUser: 'User',
}
})
import { User, SecretUser } from 'nexus-prisma';
export const UserObjectType = objectType({
name: 'User',
definition(t) {
t.field(User.id);
t.field(User.name);
},
});
export const SecretUserObjectType = objectType({
name: 'SecretUser',
definition(t) {
t.field(SecretUser.id);
t.field(SecretUser.name);
t.field(SecretUser.secretValue);
},
});
@0xsven Did you manage to implement this with a workaround?
@alainfonhof we are still using the old plugin (@kenchi/nexus-plugin-prisma
) and the new plugin (nexus-prisma
) at the same time. Once it is possible, we will remove the old one.
I was able to make this work by using as ObjectDefinitionBlock<PrismaModelName>
.
export const ProjectModel = objectType({
name: "Project",
definition(_t) {
const t = _t as unknown as ObjectDefinitionBlock<"Task">
t.field(Task.id)
t.field(Task.title)
Task
is a Prisma model
Project
is an Object Type based on Task
@jasonkuhrt the ideas looks good, does one of them are being developed?