Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 

README.md

GraphQL Server Example with TypeGraphQL & Prisma Postgres

Caution

This example was removed on 2025-03-10. If you would like an updated example, please open an issue requesting such.

Previous content

This example shows how to implement a GraphQL server with TypeScript based on Prisma Client, apollo-server. It is based on a SQLite database - you can find the database file with some dummy data at ./prisma/dev.db.

Getting started

1. Download example and navigate into the project directory

Download this example:

npx try-prisma@latest --template orm/graphql-typegraphql --install npm --name graphql-typegraphql

Then, navigate into the project directory:

cd graphql-typegraphql
Alternative: Clone the entire repo

Clone this repository:

git clone git@github.com:prisma/prisma-examples.git --depth=1

Install npm dependencies:

cd prisma-examples/orm/graphql-typegraphql
npm install

2. Create and seed the database

Create a new Prisma Postgres database by executing:

npx prisma init --db

If you don't have a Prisma Data Platform account yet, or if you are not logged in, the command will prompt you to log in using one of the available authentication providers. A browser window will open so you can log in or create an account. Return to the CLI after you have completed this step.

Once logged in (or if you were already logged in), the CLI will prompt you to:

  1. Select a region (e.g. us-east-1)
  2. Enter a project name

After successful creation, you will see output similar to the following:

CLI output
Let's set up your Prisma Postgres database!
? Select your region: ap-northeast-1 - Asia Pacific (Tokyo)
? Enter a project name: testing-migration
βœ” Success! Your Prisma Postgres database is ready βœ…

We found an existing schema.prisma file in your current project directory.

--- Database URL ---

Connect Prisma ORM to your Prisma Postgres database with this URL:

prisma+postgres://accelerate.prisma-data.net/?api_key=...

--- Next steps ---

Go to https://pris.ly/ppg-init for detailed instructions.

1. Install and use the Prisma Accelerate extension
Prisma Postgres requires the Prisma Accelerate extension for querying. If you haven't already installed it, install it in your project:
npm install @prisma/extension-accelerate

...and add it to your Prisma Client instance:
import { withAccelerate } from "@prisma/extension-accelerate"

const prisma = new PrismaClient().$extends(withAccelerate())

2. Apply migrations
Run the following command to create and apply a migration:
npx prisma migrate dev

3. Manage your data
View and edit your data locally by running this command:
npx prisma studio

...or online in Console:
https://console.prisma.io/{workspaceId}/{projectId}/studio

4. Send queries from your app
If you already have an existing app with Prisma ORM, you can now run it and it will send queries against your newly created Prisma Postgres instance.

5. Learn more
For more info, visit the Prisma Postgres docs: https://pris.ly/ppg-docs

Locate and copy the database URL provided in the CLI output. Then, create a .env file in the project root:

touch .env

Now, paste the URL into it as a value for the DATABASE_URL environment variable. For example:

# .env
DATABASE_URL=prisma+postgres://accelerate.prisma-data.net/?api_key=ey...

Run the following command to create tables in your database. This creates the User and Post tables that are defined in prisma/schema.prisma:

npx prisma migrate dev --name init

Execute the seed file in prisma/seed.ts to populate your database with some sample data, by running:

npx prisma db seed

3. Start the GraphQL server

Launch your GraphQL server with this command:

npm run dev

Navigate to http://localhost:4000 in your browser to explore the API of your GraphQL server in a GraphQL Playground.

Using the GraphQL API

The schema specifies the API operations of your GraphQL server. TypeGraphQL allows you to define a schema using TypeScript classes and decorators. The schema is generated at runtime, and is defined by the following classes:

Below are a number of operations that you can send to the API using the GraphQL Playground.

Feel free to adjust any operation by adding or removing fields. The GraphQL Playground helps you with its auto-completion and query validation features.

Retrieve all published posts and their authors

query {
  feed {
    id
    title
    content
    published
    author {
      id
      name
      email
    }
  }
}
See more API operations

Create a new user

mutation {
  signupUser(data: {
    name: "Sarah",
    email: "sarah@prisma.io"
    }
  ) {
    id
  }
}

Create a new draft

mutation {
  createDraft(
    data: {
      title: "Join the Prisma Discord",
      content: "https://pris.ly/discord"
      email: "alice@prisma.io"
    }
  ) {
    id
    published
  }
}

Publish an existing draft

mutation {
  publish(id: __POST_ID__) {
    id
    published
  }
}

Note: You need to replace the __POST_ID__-placeholder with an actual id from a Post item. You can find one e.g. using the filterPosts-query.

Search for posts with a specific title or content

{
  filterPosts(searchString: "graphql") {
    id
    title
    content
    published
    author {
      id
      name
      email
    }
  }
}

Retrieve a single post

{
  post(id: __POST_ID__) {
    id
    title
    content
    published
    author {
      id
      name
      email
    }
  }
}

Note: You need to replace the __POST_ID__-placeholder with an actual id from a Post item. You can find one e.g. using the filterPosts-query.

Delete a post

mutation {
  deleteOnePost(id: __POST_ID__) {
    id
  }
}

Note: You need to replace the __POST_ID__-placeholder with an actual id from a Post item. You can find one e.g. using the filterPosts-query.

Evolving the app

Evolving the application typically requires two steps:

  1. Migrate your database using Prisma Migrate
  2. Update your application code

For the following example scenario, assume you want to add a "profile" feature to the app where users can create a profile and write a short bio about themselves.

1. Migrate your database using Prisma Migrate

The first step is to add a new table, e.g. called Profile, to the database. You can do this by adding a new model to your Prisma schema file file and then running a migration afterwards:

// schema.prisma

model Post {
  id        Int     @default(autoincrement()) @id
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  Int
}

model User {
  id      Int      @default(autoincrement()) @id
  name    String?
  email   String   @unique
  posts   Post[]
+ profile Profile?
}

+model Profile {
+  id     Int     @default(autoincrement()) @id
+  bio    String?
+  userId Int     @unique
+  user   User    @relation(fields: [userId], references: [id])
+}

Once you've updated your data model, you can execute the changes against your database with the following command:

npx prisma migrate dev

2. Update your application code

You can now use your PrismaClient instance to perform operations against the new Profile table. Those operations can be used to implement new queries and mutations in the GraphQL API.

2.1. Use the updated Prisma Client in your application code

2.1. Create GraphQL type for Profile model using TypeGraphQL

You can use TypeGraphQL to expose the new Profile model. Create a new file named src\Profile.ts and add the following code:

import "reflect-metadata";
import { ObjectType, Field, ID } from "type-graphql";
import { User } from "./User";

@ObjectType()
export class Profile {
  @Field((type) => ID)
  id: number;

  @Field((type) => User, { nullable: true })
  user?: User | null;

  @Field((type) => String, { nullable: true })
  bio?: string | null;
}

Create a new file named src\ProfileCreateInput.ts with the following code:

import "reflect-metadata";
import { ObjectType, Field, ID, InputType } from "type-graphql";
import { User } from "./User";

@InputType()
export class ProfileCreateInput {
  @Field((type) => String, { nullable: true })
  bio?: string | null;
}

Add the profile field to src\User.ts and import the Profile class.

@Field(type => Profile, { nullable: true })
profile?: Profile | null;

Add the profile field to src\UserCreateInput.ts and import the ProfileCreateInput class:

@Field(type => ProfileCreateInput, { nullable: true })
profile?: ProfileCreateInput | null;

Extend the src\UserResolver.ts class with an additional field resolver:

@FieldResolver()
async profile(@Root() user: User, @Ctx() ctx: Context): Promise<Profile> {
  return (await ctx.prisma.user.findUnique({
    where: {
      id: user.id
    }
  }).profile())!
}

Update the signupUser mutation to include the option to create a profile when you sign up a new user:

@Mutation(returns => User)
async signupUser(
  @Arg("data") data: UserCreateInput,
  @Ctx() ctx: Context): Promise<User> {
  try {
    return await ctx.prisma.user.create({
      data: {
        email: data.email,
        name: data.name,
        profile: {
          create: {
            bio: data.bio?.bio
          }
        }
      }
    });
  }
  catch (error) {
    throw error;
  }
}

Run the following mutation to create a user with a profile:

mutation {
  signupUser(data: {
    email:"katla@prisma.io",
    profile: { bio: "Sometimes I'm an Icelandic volcano, sometimes I'm a dragon from a book."}
  })
  {
    id
    email
    posts {
      title
    }
    profile {
      id
      bio
    }
  }
}

Run the following query to return a user and their profile:

query {
  user(id: 1) {
    email
    profile {
      id
      bio
    }
    posts {
      title
      content
    }
  }
}

2.2. Update usage of Prisma Client

As the Prisma Client API was updated, you can now also invoke "raw" operations via prisma.profile directly.

Create a new profile for an existing user
const profile = await prisma.profile.create({
  data: {
    bio: "Hello World",
    user: {
      connect: { email: "alice@prisma.io" },
    },
  },
});
Create a new user with a new profile
const user = await prisma.user.create({
  data: {
    email: "john@prisma.io",
    name: "John",
    profile: {
      create: {
        bio: "Hello World",
      },
    },
  },
});
Update the profile of an existing user
const userWithUpdatedProfile = await prisma.user.update({
  where: { email: "alice@prisma.io" },
  data: {
    profile: {
      update: {
        bio: "Hello Friends",
      },
    },
  },
});

Switch to another database (e.g. SQLite, MySQL, SQL Server, MongoDB)

If you want to try this example with another database than Postgres, you can adjust the the database connection in prisma/schema.prisma by reconfiguring the datasource block.

Learn more about the different connection configurations in the docs.

Expand for an overview of example configurations with different databases

Remove the Prisma Client extension

Before you proceed to use your own database, you should remove the Prisma client extension required for Prisma Postgres:

npm uninstall @prisma/extension-accelerate

Remove the client extension from your PrismaClient instance:

- const prisma = new PrismaClient().$extends(withAccelerate())
+ const prisma = new PrismaClient()

Your own PostgreSQL database

To use your own PostgreSQL database remove the @prisma/extension-accelerate package and remove the Prisma client extension.

SQLite

Modify the provider value in the datasource block in the prisma.schema file:

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

Create an .env file and add the SQLite database connection string in it. For example:

DATABASE_URL="file:./dev.db""

MySQL

Modify the provider value in the datasource block in the prisma.schema file:

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

Create an .env file and add a MySQL database connection string in it. For example:

## This is a placeholder url
DATABASE_URL="mysql://janedoe:mypassword@localhost:3306/notesapi"

Microsoft SQL Server

Modify the provider value in the datasource block in the prisma.schema file:

datasource db {
  provider = "sqlserver"
  url      = env("DATABASE_URL")
}

Create an .env file and add a Microsoft SQL Server database connection string in it. For example:

## This is a placeholder url
DATABASE_URL="sqlserver://localhost:1433;initial catalog=sample;user=sa;password=mypassword;"

MongoDB

Modify the provider value in the datasource block in the prisma.schema file:

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

Create an .env file and add a local MongoDB database connection string in it. For example:

## This is a placeholder url
DATABASE_URL="mongodb://USERNAME:PASSWORD@HOST/DATABASE?authSource=admin&retryWrites=true&w=majority"

Next steps