Prisma ClientFeatures
GraphQL Requests (TypeScript)
Overview
The Prisma client lets you send GraphQL queries and mutations directly to your Prisma service using the $graphql
method:
$graphql: <T = any>(query: string, variables?: {[key: string]: any}) => Promise<T>;
As the GraphQL operation is passed untyped (as a regular string), the type of the returned promise is any
.
Examples
Fetching a single user:
const query = `
query {
user(id: "cjcdi63j80adw0146z7r59bn5") {
id
name
}
}
`
const result: any = prisma.$graphql(query)
// sample result:
// {"data": { "user": { "id": "cjcdi63j80adw0146z7r59bn5", "name": "Sarah" } } }
Fetching a single user using variables:
const query = `
query ($id: ID!){
user(id: $id) {
id
name
}
}
`
const variables: UserWhereUniqueInput = { id: 'cjcdi63j80adw0146z7r59bn5' }
const result: any = prisma.$graphql(query, variables)
// sample result:
// {"data": { "user": { "id": "cjcdi63j80adw0146z7r59bn5", "name": "Sarah" } } }
Creating a new user:
const mutation = `
mutation ($name: String!){
createUser(name: $name) {
id
name
}
}
`
const variables: UserCreateInput = { name: 'Alice' }
const result: any = prisma.$graphql(mutation, variables)
// sample result:
// {"data": { "createUser": { "id": "cjlhqfbfa003t0a23rhzjragl", "name": "Alice" } } }