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

我具有以下数据库模型

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

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

每次创建GlobalUser时,我都想在客户端表中创建一个客户端。 如果我选择使用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从客户端组合并执行多个Mutations?还是在服务器端更好?

wearcoll658 回答:用Prisma和Apollo调用多个GraphQL突变的正确方法是什么

如果像这样将客户端添加为与数据模型的关系:


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

本文链接:https://www.f2er.com/3039931.html

大家都在问