使用TypeScript类初始化Apollo的客户端默认本地状态

时间:2019-12-26 15:55:11

标签: reactjs typescript apollo react-apollo apollo-client

我有我的Checkout

export class Checkout {
  totalValue: number
  subTotalValue: number
  shippingCost: number
  lineItems: [LineItem] | []

  constructor(
    totalValue: number,
    subTotalValue: number,
    shippingCost: number,
    lineItems: [LineItem] | []
  ) {
    this.totalValue = totalValue
    this.subTotalValue = subTotalValue
    this.shippingCost = shippingCost
    this.lineItems = lineItems
  }
}

在我的typeDefs


export const typeDefs = gql`
  extend type Query {
    isLoggedIn: Boolean!
    checkout: Checkout
  }

  extend type Checkout {
    totalValue: Int
    subTotalValue: Int
    shippingCost: Int
    lineItems: [LineItem]
  }

我想要做的是能够构建该TS类并将其设置为阿波罗的本地状态:

cache.writeData({
  data: {
    isLoggedIn: !!token,
    checkout: new Checkout(0, 0, 15, []),
  },
})

这是正确的方法吗?

0 个答案:

没有答案