Back to Prisma1

Graphql Requests JAVASCRIPT Pyl3

docs/1.26/prisma-client/features/graphql-requests-JAVASCRIPT-pyl3.mdx

1.34.121.1 KB
Original Source

import Collapse from 'components/Markdown/Collapse'

export const meta = { title: 'GraphQL Requests (JavaScript)', position: 100, technology: "node", technologyOrder: 1, articleGroup: "GraphQL Requests", }

Overview

The Prisma client lets you send GraphQL queries and mutations directly to your Prisma service using the $graphql method:

ts
$graphql: <T = any>(query: string, variables?: {[key: string]: any}) => Promise<T>;

Examples

Fetching a single user:

js
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:

js
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" } } }