无法使用阿波罗客户端上的突变添加数据
阿波罗服务器客户端的突变:
import { gql } from "@apollo/client";
export const UPLOAD_IMAGE = gql`
mutation uploadImage($url: String!, $description: String!, $posterName: String!) {
uploadImage(post: { url: $url, description: $description , posterName: $posterName }) {
url
posterName
description
}
}
`;
尝试添加新帖子:
import { useMutation, useQuery } from "@apollo/client";
import { UPLOAD_IMAGE } from "./mutation";
import { useState } from "react";
function NewPost() {
const [posterName, setPosterName] = useState(null);
const [uu, {err}] = useMutation(UPLOAD_IMAGE);
const [url, setUrl] = useState(null);
const [description, setDescription] = useState(null);
const addPost = () => {
uu({
variables: {
url: url,
description: description,
posterName: posterName,
},
});
};
return (
<div className="App">
Url--- <input onChange={(e) => setUrl(e.target.value)} />
<br />
PosterName--- <input onChange={(e) => setPosterName(e.target.value)} />
<br />
Description ---
<input onChange={(e) => setDescription(e.target.value)} />
<br />
<button onClick={() => addPost()}>Add Post</button>
</div>
);
}
export default NewPost;
在服务器端,阿波罗服务器上的突变:
typedef:
type Mutation {
uploadImage(
url: String
description: String
posterName: String
): ImagePost
}
Resolver:
Mutation: {
uploadImage: async (_,args) => {
//const { data } = await axios.get('https://api.unsplash.com/photos/?client_id=2zceQd7D4SraKoqW_GjPzXboSup3TKRIPk7EXfJBcAs');
const newPost = {
id: uuid.v4(),
url: args.url,
description: args.description,
posterName: args.posterName,
binned: false,
userPosted: true,
}
await client.lpushAsync("postedImagesList",JSON.stringify(newPost));
return newPost;
}
}
创建新帖子时,我将其添加到Redis中以存储它。但是该帖子不会从输入字段和按钮中添加。它确实使用游乐场工作,不确定为什么。我正在使用lrangeasync检索数据,以从REDIS中获取数据。
Mutation on client side of Apollo server:
import { gql } from "@apollo/client";
export const UPLOAD_IMAGE = gql`
mutation uploadImage($url: String!, $description: String!, $posterName: String!) {
uploadImage(post: { url: $url, description: $description , posterName: $posterName }) {
url
posterName
description
}
}
`;
Trying to add new post:
import { useMutation, useQuery } from "@apollo/client";
import { UPLOAD_IMAGE } from "./mutation";
import { useState } from "react";
function NewPost() {
const [posterName, setPosterName] = useState(null);
const [uu, {err}] = useMutation(UPLOAD_IMAGE);
const [url, setUrl] = useState(null);
const [description, setDescription] = useState(null);
const addPost = () => {
uu({
variables: {
url: url,
description: description,
posterName: posterName,
},
});
};
return (
<div className="App">
Url--- <input onChange={(e) => setUrl(e.target.value)} />
<br />
PosterName--- <input onChange={(e) => setPosterName(e.target.value)} />
<br />
Description ---
<input onChange={(e) => setDescription(e.target.value)} />
<br />
<button onClick={() => addPost()}>Add Post</button>
</div>
);
}
export default NewPost;
Mutation on Server side, Apollo server:
Typedef:
type Mutation {
uploadImage(
url: String
description: String
posterName: String
): ImagePost
}
Resolver:
Mutation: {
uploadImage: async (_,args) => {
//const { data } = await axios.get('https://api.unsplash.com/photos/?client_id=2zceQd7D4SraKoqW_GjPzXboSup3TKRIPk7EXfJBcAs');
const newPost = {
id: uuid.v4(),
url: args.url,
description: args.description,
posterName: args.posterName,
binned: false,
userPosted: true,
}
await client.lpushAsync("postedImagesList",JSON.stringify(newPost));
return newPost;
}
}
When a new post is created I am adding it to Redis to store it. But the post does not get added from the input fields and button. It does work using the playground, not sure why. I am retrieving the data using lrangeasync to get back data from Redis.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个!
Try this!