无法在 Apollo 中更新我的片段?

问题描述

我正在制作一个简单的 Reddit 克隆,并在 Apollo-CLient 中使用 React-Native。 我正在尝试为我的帖子点赞或点赞,但我无法更新我的阿波罗客户端,但我可以在服务器上更新我的帖子赞。

最初我使用的是简单的 cache.wrtiteFragment 但后来我意识到由于我的主键不同,我不能使用“id”。所以这是我更新的 InMemoryCache()。

enter image description here

这是我的 updateAfterVote 函数:-

import { ApolloCache } from "@apollo/client";
import { VoteMutation } from "../generated/graphql";
import gql from "graphql-tag";

export const updateAfterVote = (
  value: number,postId: string,cache: ApolloCache<VoteMutation>
) => {
  const data = cache.readFragment<{
    postId: string;
    points: number;
    VoteStatus: number | null;
  }>({
    postId: "Post:" + postId,fragment: gql`
      fragment _ on Post {
        postId
        points
        VoteStatus
      }
    `,});

  if (data) {
    if (data.VoteStatus === value) {
      return;
    }
    const newPoints =
      (data.points as number) + (!data.VoteStatus ? 1 : 2) * value;
    cache.writeFragment({
      postId: "Post:" + postId,fragment: gql`
        fragment __ on Post {
          points
          VoteStatus
        }
      `,data: { points: newPoints,VoteStatus: value },});
  }
};

它给了我这个错误:-

'{ postId: string; 类型的参数片段:文档节点; }' 不能分配给 'ReadFragmentOptions'。 对象字面量只能指定已知的属性,并且在类型 'ReadFragmentOptions'。

我需要一些帮助来更新我的客户端。 对于上下文,这是我的帖子实体:-

import { Field,Int,ObjectType } from 'type-graphql';
import {
  BaseEntity,Column,CreateDateColumn,Entity,ManyToOne,OnetoMany,PrimaryGeneratedColumn,UpdateDateColumn
} from 'typeorm';
import { Spaces } from './Spaces';
import { Updoot } from './Updoot';
import { User } from './User';

@ObjectType()
@Entity()
export class Post extends BaseEntity {
  @Field()
  @PrimaryGeneratedColumn('uuid')
  postId: string;

  @Field()
  @Column('text')
  creatorId: string;

  @Field()
  @Column({ type: 'int',default: 0 })
  points!: number;

  @Field()
  @Column('text')
  title: string;

  @Field()
  @Column('text')
  content: string;

  @Field()
  @Column('text')
  postSpaceId: string;

  @Field(() => Int,{ nullable: true })
  VoteStatus: number | null;

  @Field()
  @Column('text')
  spaceName: string;

  @Field()
  @Column('text',{ nullable: true })
  imageUrl: string;

  @Field()
  @ManyToOne(() => User,(user) => user.posts)
  creator: User;

  @OnetoMany(() => Updoot,(updoot) => updoot.post)
  updoots: Updoot[];

  @Field()
  @ManyToOne(() => Spaces,(spaces) => spaces.posts,{
    onDelete: 'CASCADE'
  })
  space: Spaces;

  @Field()
  @CreateDateColumn()
  createdAt: Date;

  @Field()
  @UpdateDateColumn()
  updatedAt: Date;
}

这就是我的 Apollo Cache 的样子:-

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)