Back to Prisma1

02 Update Prisma Api C001

docs/1.16/get-started/02-update-prisma-api-c001.mdx

1.34.121.8 KB
Original Source

import QueryChooser from 'components/Markdown/QueryChooser'

export const meta = { title: 'Update Prisma API', position: 2, nextText: 'Fantastic! 🎉 You are now able to make changes to your Prisma API. Learn how to consume the API using Prisma bindings next.' }

Goals

On this page, you will learn how to:

  • Change the data model of your Prisma service
  • Deploy the adjusted data model to update your Prisma API
  • Send nested queries and mutations

Adjust your data model

Update the data model in datamodel.graphql as follows. You are adding a new Post type to the data model as well as a relation between User and Post (via the posts and author fields):

graphql
type User {
  id: ID! @unique
  name: String!
  posts: [Post!]!
}

type Post {
  id: ID! @unique
  title: String!
  published: Boolean! @default(value: "false")
  author: User
}

(Re-)Deploy your Prisma service

To apply the changes you just made to your data model, you need to (re-)deploy your Prisma service:

bash
prisma deploy

Explore the Prisma API in a Playground

Open a GraphQL Playground using the following command:

bash
prisma playground

Because of the relation that was added to the data model, you can now send nested queries and mutations to read and modify connected nodes.

<QueryChooser titles={["Create post & user","Query users & posts"]}>

graphql
mutation {
  createPost(data: {
    title: "GraphQL is great"
    author: {
      create: {
        name: "Bob"
      }
    }
  }) {
    id
    published
    author {
      id
    }
  }
}
graphql
query {
  users {
    id
    name
    posts {
      id
      title
      published
    }
  }
}
</QueryChooser>