1

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. 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.

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

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? Or is it better to do it on the server side?

chris
  • 2,490
  • 4
  • 32
  • 56

1 Answers1

1

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. E.g.:

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

realAlexBarge
  • 1,808
  • 12
  • 19