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

Create multiple objectTypes from same prisma model #160

Open
0xsven opened this issue Nov 30, 2021 · 5 comments
Open

Create multiple objectTypes from same prisma model #160

0xsven opened this issue Nov 30, 2021 · 5 comments
Labels
needs/discussion Something needs deciding, new info changes issue scope/spec, unforeseen challenges, etc. type/feat Add a new capability or enhance an existing one

Comments

@0xsven
Copy link

0xsven commented Nov 30, 2021

Perceived Problem

I feel the need to create multiple objectTypes 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.

@0xsven 0xsven added the type/feat Add a new capability or enhance an existing one label Nov 30, 2021
@jasonkuhrt jasonkuhrt added the needs/discussion Something needs deciding, new info changes issue scope/spec, unforeseen challenges, etc. label Dec 3, 2021
@jasonkuhrt
Copy link
Member

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);
  },
});

@alainfonhof
Copy link

@0xsven Did you manage to implement this with a workaround?

@0xsven
Copy link
Author

0xsven commented Feb 10, 2022

@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.

@larskarbo
Copy link

larskarbo commented Jun 7, 2022

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

@sandrewTx08
Copy link

@jasonkuhrt the ideas looks good, does one of them are being developed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs/discussion Something needs deciding, new info changes issue scope/spec, unforeseen challenges, etc. type/feat Add a new capability or enhance an existing one
Projects
None yet
Development

No branches or pull requests

5 participants