GraphQL: Prisma, how to use typescript to seed data.

When one is using Prisma and needs to seed a database with data. One should always use a Typescript file instead of a seed.graphql file.

Using a Typescript file allows for much more control to ensure data that is being used in multiple locations is uniform. It reduces the amount of repetitive code you have to write (DRY) and and allows for typescript error checking. So why are people seeding with a GraphQL file? And why is it the default? I have no idea.

To start with we will need to edit the prisma.yml file to tell it what to do when we seed. I’m also generating a client which will be used in the seed file.

# Specifies the HTTP endpoint of your Prisma API.
endpoint: ${env:EXAMPLE_ENDPOINT}

# Defines your models, each model is mapped to the database as a table.
datamodel: datamodel.graphql
# Specifies the language and directory for the generated Prisma client.
  - generator: typescript-client
    output: ../../src/generated/example-client
# Seed your service with initial data based on `seed.ts`.
  run: ts-node ./seed.ts

Now wee need to create the seed.ts file.

import { $enum } from "ts-enum-util";
import { Example} from "../src/generated/";
import { roles } from "../src/schema/types";

const roleNames = $enum(roles).getValues();

admins =["joe", "bill"]

const setup = async (roles: string[], rolesEnum) => {
  await roles.forEach(async role => {
    Example.createRole({ name: role }).catch(err => console.log(err));
  await admins.forEach(async i => {
      name: i,
      roles: { connect: { name: rolesEnum.admin } }
    }).catch(err => console.log(err));

setup(roleNames, roles);

I recommend creating a Prisma client that can be passed around to the server config and other locations. This makes it easy to include multiple Prisma endpoints as well as other clients as well. Here’s how I did it:

import { prisma as Example} from "./example-client";

export {

Please do let me know if you run into problems or have any questions.

Posted in

Andrew Obrigewitsch

Andrew is a Senior Software Engineer based out of Sacramento California, he works at are large software company that handles massive data sets. He builds both backend and frontend solutions to handle said large data sets. He is passionate about new technologies which make developing faster, easier and achieve more uniform and predication results.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *