0

I'm implementing a graphql prisma datamodel. Here I have a type called BankAccount . I may need to update and delete them as well. I'm implementing this as immutable object. So, when updating I'm adding updating the existing record as IsDeleted and add a new record. And when updating an existing record I need to keep the id of the previous record to know which record is updated. So, I've came up with a type like this

type BankAccount {
  id: ID! @unique
  parentbankAccount: String!
  bankName: String!
  bankAccountNo: String!
  isDeleted: Boolean! @default(value: "false")
}

Here the parentBankAccount keeps the id of previous BankAccount. I'm thinking when creating a bank account, setting the parentBankAccount as same as the id as it doesn't have a parent. The thing is I'm not sure it's possible. I'm bit new to GraphQL. So, any help would be appreciated. Thanks

4

1 回答 1

1

In GraphQL, generally if one object refers to another, you should directly refer to that object; you wouldn't embed its ID. You can also make fields nullable, to support the case where some relationship just doesn't exist. For this specific field, then, this would look like

type BankAccount {
  parentBankAccount: BankAccount
  ...
}

and that field would be null whenever an account doesn't have a parent.


At an API level, the layout you describe seems a little weird. If I call

query MyBankAccount {
  me { accounts { id } }
}

I'll get back some unique ID. I'd be a little surprised to later call

query MyBalance($id: ID!) {
  node(id: $id) {
    ... on BankAccount {
      name
      isDeleted
      balance
    }
  }
}

and find out that my account has been "deleted" and that the balance is from a week ago.

Using immutable objects in the underlying data store makes some sense, particularly for auditability reasons, but that tends to not be something you can expose out through a GraphQL API directly (or most other API layers: this would be equally surprising in a REST framework where the object URL is supposed to be permanent).

于 2019-03-01T11:59:43.590 回答