docs/1.20/maintain/graphcool-to-prisma/data-modeling-and-graphql-api-gcf2.mdx
export const meta = { title: 'Data Modeling & GraphQL API', position: 110, }
Prisma introduces a few changes to the way how your datamodel is written as well as to the generated GraphQL API.
@model directiveThe @model directive that was previously required to denote your model types is removed.
type User @model {
id: ID! @isUnique
name: String!
}
type User {
id: ID! @unique
name: String!
}
@relation directive becomes optional on unambiguous relationsWhen a relation in your datamodel is unambiguous, you can omit the @relation directive.
type User @model {
id: ID! @isUnique
name: String!
posts: [Post!]! @relation(name: "UsersPosts")
}
type Post @model {
id: ID! @isUnique
title: String!
author: User! @relation(name: "UsersPosts")
}
type User @model {
id: ID! @unique
name: String!
posts: [Post!]!
}
type Post @model {
id: ID! @unique
title: String!
author: User!
}
id field is optionalThe id field is now optional on the model types in your datamodel (similar to createdAt and updatedAt), you can remove it if it's not needed on a type.
@isUnique is renamed to @uniqueThe @isUnique directive is renamed to @unique.
type User {
id: ID! @isUnique
email: String! @isUnique
}
type User {
id: ID! @unique
email: String! @unique
}
@defaultValue is renamed to @defaultThe @defaultValue directive is renamed to @default.
type User {
id: ID! @isUnique
name: String! @defaultValue(value: "Unknown")
}
type User {
id: ID! @unique
name: String! @default(value: "Unknown")
}
Most notably of all API changes, Prisma merges the previous Simple and Relay APIs. The resulting API is compatible with all GraphQL clients. Consequently, each Prisma service only provides a single HTTP endpoint.
The Simple API of the Graphcool Framework followed the approach of passing single values to mutations. In the new Prisma API, all input arguments for mutations are wrapped in one data argument.
mutation {
createPost(title: "GraphQL is great" text: "It really is") {
id
}
}
mutation {
createPost(data: {
title: "GraphQL is great"
text: "It really is"
}) {
id
}
}
all-prefix from query root fieldsThe generated queries to return lists of nodes have the all-prefix removed.
query {
allUsers {
id
name
}
}
query {
users {
id
name
}
}
When asking for a single node, the corresponding query is now lowercased.
query {
User(id: "cjd5pqjuzpbuy0171tiuj098t") {
id
name
}
}
query {
user(id: "cjd5pqjuzpbuy0171tiuj098t") {
id
name
}
}
filter renamed to whereThe filter argument has been renamed to where in the Prisma GraphQL API.
query {
allUsers(filter: {
name_contains: "Karl"
}) {
id
name
}
}
query {
users(where: {
name_contains: "Karl"
}) {
id
name
}
}
@unique fieldIn the Graphcool Framework GraphQL API, it was only possible to update and delete nodes by selecting them via their id field. With Prisma, you can use any field that's annotated with the @unique directive for that.
Consider this datamodel:
type User {
id: ID! @unique
email: String! @unique
}
With Prisma, you can now send the following mutation to delete a User node:
mutation {
deleteUser(by: {
email: "[email protected]"
}) {
id
}
}
New API features introduced in Prisma include batch mutations, improved nested mutations and transactional mutations and more.
This means that for these use cases, you can now use these new primitives instead of following a more complex setup as before.