1

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

Eliya Cohen
  • 10,716
  • 13
  • 59
  • 116

2 Answers2

1

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
  }
});
Eliya Cohen
  • 10,716
  • 13
  • 59
  • 116
1

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.

Alex Ruheni
  • 989
  • 5
  • 6
  • Yet again, thanks for the help. But what if we're talking about a column that is not `updatedAt`? let's say I have a type of Account. An account has a "credits" column. I want to read it, but not write on it. How would you approach this situation? – Eliya Cohen Dec 30 '20 at 11:50
  • You can make a column nullable by using the `?` operator in your model. Nexus `inputTypes` generated will make field nullable too meaning you won't have to include the field when creating/updating a record.. The field will be readable and writable(if you wish to write to it). Would you like to make a column readonly? – Alex Ruheni Dec 30 '20 at 12:50
  • It should be readonly. Meaning, only the server would be able to write on it (e.g auth token, credits, etc) – Eliya Cohen Dec 30 '20 at 14:23