Skip to content

Latest commit

 

History

History
 
 

hooks

Hooks

This example demonstrates hooks property available to us in prisma.yml. The hooks property is used to define terminal commands which will be executed by the Prisma CLI before or after certain commands.

Currently, only post-deploy hook is available.

Using hooks, we can perform various workflows like:

  1. Generation of schema file for the autogenerated Prisma GraphQL API.
  2. Syncing the generated schema file with a service or client to implement persisted queries.

Get started

Note: prisma is listed as a development dependency and script in this project's package.json. This means you can invoke the Prisma CLI without having it globally installed on your machine (by prefixing it with yarn), e.g. yarn prisma deploy or yarn prisma playground. If you have the Prisma CLI installed globally (which you can do with npm install -g prisma), you can omit the yarn prefix.

1. Download the example & install dependencies

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/yml-structure

Next, navigate into the downloaded folder and install the NPM dependencies:

cd yml-structure
yarn install

2. Deploy the Prisma database service

You 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 deploy
I 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.

3. hooks property of prisma.yml

The hooks property is used to define terminal commands which will be executed by the Prisma CLI before or after certain commands.

Currently, only post-deploy hook is available. In this example, we are using the post-deploy hook to perform the following tasks

hooks:
  post-deploy:
    - echo "Deployment finished"
    - graphql get-schema --project db
    - graphql codegen
  • Printing "Deployment finished"

  • Getting the latest schema

  • Running graphql codegen to generate TS bindings

Note that to run graphql codegen we need to have prisma-binding with version 2.0 or greater.

Note that these commands work closely in conjunction with .graphqlconfig.yml file that looks like this and directs the output of generate schema and typescript bindings.

projects:
  db:
    schemaPath: generated-schema.graphql
    extensions:
      endpoints:
        default: 'http://localhost:4466/hooks'
      prisma: prisma.yml
      codegen:
        input:
          schema: generated-schema.graphql
        output:
          typings: generated-prisma.ts
        generator: typegen
        language: typescript