Eliya Cohen
Eliya Cohen

Reputation: 11458

How to use Nexus' Prisma plugin crud with specific arguments

I have an Advertiser graphql type:

type Advertiser {
  createdAt: DateTime!
  id: Int!
  isActive: Boolean!
  name: String!
  updatedAt: DateTime
}

Given the following mutation:

mutationType({
  definition(t) {
    t.crud.createOneAdvertiser();
  }
});

Nexus generates this schema (as expected):

input AdvertiserCreateInput {
  createdAt: DateTime!
  isActive: Boolean!
  name: String!
  updatedAt: DateTime
}

type Mutation {
  createOneAdvertiser(data: AdvertiserCreateInput!): Advertiser!
}

The thing is, what if I want to omit some of the args? For example, I don't want the client to pass createdAt or updatedAt. these columns should be resolved in the server.

See (partial) documentation here - https://nexusjs.org/docs/plugins/prisma/api#example-4

Upvotes: 1

Views: 1242

Answers (2)

Alex Ruheni
Alex Ruheni

Reputation: 1169

Great question Eliya.

If you're using Nexus to define your GraphQL types, you can use the objectType to define your schema types. When paired with Prisma and nexus-plugin-prisma, you can use t.model to map your schema types to your db models in your objectType. When defining your objectType, you can choose to omit some columns in your database models.

Here's an example Prisma model to demonstrate the concept:

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  updatedAt DateTime @updatedAt
}

When defining your objectType, you can include the the updatedAt column:

import { objectType } from 'nexus'

const Post = objectType({
  name: 'Post',
  definition(t) {
    t.model.id()
    t.model.title()
    t.model.content()
    t.model.published()
    t.model.author()
    t.model.updatedAt()
  }
})

export default Post

updatedAt, in this example, will be resolved by the GraphQL server/Prisma and your client won't be required to pass the argument despite being included in the inputType generated by Nexus.

I hope this answers your question. If you need any further clarifications let me know.

Upvotes: 1

Eliya Cohen
Eliya Cohen

Reputation: 11458

Yet again, found the answer right after posting this question. Simply pass computedInputs like so:

t.crud.createOneAdvertiser({
  computedInputs: {
    createdAt: () => DateTime.utc().toString(),
    updatedAt: () => null
  }
});

Upvotes: 1

Related Questions