import Collapse from 'components/Markdown/Collapse'
export const meta = { title: 'GraphQL Requests (JavaScript)', position: 100, technology: "node", technologyOrder: 1, articleGroup: "GraphQL Requests", }
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>;
Fetching a single user:
const query = `
query {
user(id: "cjcdi63j80adw0146z7r59bn5") {
id
name
}
}
`
prisma.$graphql(query)
.then(result => console.log(result))
// sample result:
// {"data": { "user": { "id": "cjcdi63j80adw0146z7r59bn5", "name": "Sarah" } } }
Fetching a single user using variables:
const query = `
query ($userId: ID!){
user(id: $userId) {
id
name
}
}
`
const variables = { userId: 'cjcdi63j80adw0146z7r59bn5' }
prisma.$graphql(query, variables)
.then(result => console.log(result))
// sample result:
// {"data": { "user": { "id": "cjcdi63j80adw0146z7r59bn5", "name": "Sarah" } } }