簡體   English   中英

使用 Prisma 和 Apollo 調用多個 GraphQL 突變的正確方法是什么

[英]What is the correct way to call multiple GraphQL mutations with Prisma and Apollo

我有以下數據庫模型

type GlobalUser {
  email: String 
  createdAt: DateTime! 
  updatedAt: DateTime! 
}

type Client {
  global_user: GlobalUser! 
  createdAt: DateTime! 
  updatedAt: DateTime! 
}

每次創建 GlobalUser 時,我都想在 client 表中創建一個 Client。 如果我選擇使用 Apollo 從應用程序的 Angular 客戶端執行此操作,這可能是我使用 Promise 一個接一個調用一個更改的方法。

document = gql`
  mutation createGlobalUser(
    $email: String!, $password: String!
  ) {
    createGlobalUser(
      email: $email, password: $password,
    ) {
      email
    }
  }
`;

createGlobalUserService.mutate({

      email: email

}).toPromise().then((res) => {

    createClientService.mutate({

        global_user: res.data.id

 }).catch(err => {
     console.log(err);
 });

我沒有找到從服務器端的 Prisma 解析器中做到這一點的方法

const Mutation = {
 async createGlobalUser(root, args, context) {
   const user = await context.prisma.createGlobalUser({
       ...args
   });
   return {
     id
     email
   }
 }

有沒有一種方法可以在 Angular 中使用 Apollo 從客戶端組合和執行多個 Mutation? 還是在服務器端做更好?

如果您將客戶端添加為與數據模型的關系,如下所示:


type GlobalUser {
  email: String 
  createdAt: DateTime! 
  updatedAt: DateTime! 
  client: Client! @relation(name: "UserClient")
}

type Client {
  global_user: GlobalUser! @relation(name: "UserClient")
  createdAt: DateTime! 
  updatedAt: DateTime! 
}

您可以在來自前端的一個請求中使用 Prisma 客戶端創建客戶端。 例如:

document = gql`
  mutation createGlobalUser(
    $email: String!, $password: String!
  ) {
    createGlobalUser(
      data: {
        email: $email
        password: $password
        client: {
           create: { ... }
        }
    ) {
      email
    }
  }
`;

有關更多信息,請查看: https : //www.prisma.io/docs/datamodel-and-migrations/datamodel-POSTGRES-knum/#the-@relation-directive

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM