【问题标题】:What is the correct way to call multiple GraphQL mutations with Prisma and Apollo使用 Prisma 和 Apollo 调用多个 GraphQL 突变的正确方法是什么
【发布时间】:2020-03-20 05:11:56
【问题描述】:

我有以下数据库模型

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

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

每次创建 GlobalUser 时,我都想在 client 表中创建一个 Client。 如果我选择使用 Apollo 从应用程序的 Angular 客户端执行此操作,这可能是我使用 Promises 调用一个又一个突变的方法。

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?还是在服务器端做更好?

【问题讨论】:

    标签: angular graphql apollo-client prisma prisma-graphql


    【解决方案1】:

    如果您将客户端添加为与数据模型的关系,如下所示:

    
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-22
      • 2019-11-27
      • 2020-02-14
      • 2020-12-12
      • 2019-06-18
      • 2019-09-26
      • 2020-09-11
      • 2021-07-04
      相关资源
      最近更新 更多