如何解决无法在 Apollo 中更新我的片段?
我正在制作一个简单的 Reddit 克隆,并在 Apollo-CLient 中使用 React-Native。 我正在尝试为我的帖子点赞或点赞,但我无法更新我的阿波罗客户端,但我可以在服务器上更新我的帖子赞。
最初我使用的是简单的 cache.wrtiteFragment 但后来我意识到由于我的主键不同,我不能使用“id”。所以这是我更新的 InMemoryCache()。
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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。