export const meta = { title: 'Data Modeling & GraphQL API', position: 110, }
Prisma introduces a few changes to the way how your data model is written as well as to the generated GraphQL API.
The @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!
}
When a relation in your data model 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!
}
The id
field is now optional on the model types in your data model (similar to createdAt
and updatedAt
), you can remove it if it's not needed on a type.
The @isUnique
directive is renamed to @unique
.
type User {
id: ID! @isUnique
email: String! @isUnique
}
type User {
id: ID! @unique
email: String! @unique
}
The @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
}
}
The 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
}
}
The 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
}
}
In 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 data model:
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: "alice@graph.cool"
}) {
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.