This example demonstrates usage of Prisma with graphql-yoga and prisma-binding to build an application server and expose a domain specific API.
Note:
prismais listed as a development dependency and script in this project'spackage.json. This means you can invoke the Prisma CLI without having it globally installed on your machine (by prefixing it withyarn), e.g.yarn prisma deployoryarn prisma playground. If you have the Prisma CLI installed globally (which you can do withnpm install -g prisma), you can omit theyarnprefix.
Clone the Prisma monorepo and navigate to this directory or download only this example with the following command:
curl https://codeload.github.com/graphcool/prisma/tar.gz/master | tar -xz --strip=2 prisma-master/examples/application-serverNext, navigate into the downloaded folder and install the NPM dependencies:
cd application-server
yarn installYou can now deploy the Prisma service (note that this requires you to have Docker installed on your machine - if that's not the case, follow the collapsed instructions below the code block):
yarn prisma deployI don't have Docker installed on my machine
To deploy your service to a demo server (rather than locally with Docker), please follow this link.
This example seeds some data into the database for us to explore some queries and features of the data model. Please take a look at seed.graphql for reference. Feel free to add/remove more data via mutations.
yarn start
The easiest way to explore this deployed service and play with the API generated from the data model is by using the GraphQL Playground.
You can either start the desktop app via
yarn playgroundOr you can open a Playground by navigating to http://localhost:4000 in your browser.
query {
posts(where: { author: { name_in: ["Prisma"] } }) {
id
title
status
author {
id
name
handle
}
}
}mutation {
createPost(
data: {
title: "Second Post"
content: "Second Post Content"
status: DRAFT
author: { connect: { handle: "prisma" } }
}
) {
id
}
}Notice that how we are able to associate a User with this newly created post. The user was created using seed.graphql when the first deploy happened.
We are able to associate the user using their handle field as it is decorated with @unique directive.
Feel free to play around with the API.