简体   繁体   English

使用 Prisma 和 Apollo 调用多个 GraphQL 突变的正确方法是什么

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

I have the following database model我有以下数据库模型

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

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

Every time a GlobalUser is created, I want to create a Client in the client table.每次创建 GlobalUser 时,我都想在 client 表中创建一个 Client。 If I choose to do that from the Angular client-side of the application using Apollo, this could be the approach, where I call one mutation after the other using Promises.如果我选择使用 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);
 });

I did not find a way to do that from a Prisma resolver on the server side我没有找到从服务器端的 Prisma 解析器中做到这一点的方法

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

Is there a way we can combine and execute multiple Mutations from the client side using Apollo in Angular?有没有一种方法可以在 Angular 中使用 Apollo 从客户端组合和执行多个 Mutation? Or is it better to do it on the server side?还是在服务器端做更好?

If you add the client as a relation to the data model like this:如果您将客户端添加为与数据模型的关系,如下所示:


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! 
}

You can create the client using the prisma client within one request from the frontend.您可以在来自前端的一个请求中使用 Prisma 客户端创建客户端。 Eg:例如:

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

For more information check: https://www.prisma.io/docs/datamodel-and-migrations/datamodel-POSTGRES-knum/#the-@relation-directive有关更多信息,请查看: https : //www.prisma.io/docs/datamodel-and-migrations/datamodel-POSTGRES-knum/#the-@relation-directive

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 中 GraphQL 的 Apollo 的 watchQuery().valueChanges 的正确返回类型是什么? - What is the correct return type of Apollo's watchQuery().valueChanges for GraphQL in Angular? 在一个效果中调用多个服务的正确方法是什么? - what is the correct way to call multiple services in one effect? 在 Angular 中动态创建 Apollo GraphQL 突变调用 - Dynamically creating Apollo GraphQL mutation call in Angular Apollo Angular Graphql,如何使用多个端点? - Apollo Angular Graphql, How to use multiple enpoints? 调用 NgRx 加载微调器的正确方法是什么 - What is the correct way to call an NgRx loading spinner 等待多个请求完成的正确方法是什么? - What is the correct way to wait for multiple requests to finish? Apollo - 来自 Angular 应用程序子组件的多个 graphql 订阅 - Apollo - multiple graphql subscriptions from child components in Angular app 从父母那里打电话给孩子 function 的正确、优雅的方式是什么? - What is the correct, elegant way to call a child function from a parent? 如何为 apollo-angular 中的突变配置缓存? - how to configure the cache for mutations in apollo-angular? 在 RxJs 5 中共享 Angular Http.network 调用结果的正确方法是什么? - What is the correct way to share the result of an Angular Http network call in RxJs 5?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM