我正在使用 Next.js 、 typescript 和 Graphql 。我正在尝试通过 apollo 服务器将数据添加到本地 json 文件。有 2 个问题
我收到警告“来自服务器的额外属性:输入处的 fdprocessedid” .
目前,控制台显示已添加数据,但是本地的 JSON 文件尚未更新。 这是解析器.ts
让lastUserId = 0;
Mutation: {
createUser: (_parent:null, { input }: { input: AddUserInput }): User => {
const newUserId = ++lastUserId;
return {
id: newUserId.toString(), // Example: generate a random ID
name: input.name,
email: input.email,
};
}
架构
input AddUserInput {
name: String!
email: String!
}
type Mutation {
createUser(input: AddUserInput!): User!
}
组件
"use client"
import { FC, FormEvent, useState } from 'react';
import { Action, Contact } from './reducer/userReducer';
import { InputLabel , FormGroup ,TextField,FormControl,Button } from '@mui/material';
import { useMutation, gql } from '@apollo/client';
const ADD_USER = gql`
mutation AddUser($input: AddUserInput!) {
createUser(input: $input) {
id
}
}
`;
const UserForm: React.FC = () => {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [addUserMutation] = useMutation(ADD_USER);
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
try {
await addUserMutation({
variables: {
input: {
name,
email,
},
},
});
// Optionally, clear the form fields
setName('');
setEmail('');
console.log('User added successfully!');
} catch (error) {
console.error('Error adding user:', error);
}
};
return (
<form className='userform' onSubmit={handleSubmit} >
<FormControl fullWidth margin="normal">
<InputLabel htmlFor="firstName">First Name</InputLabel >
<TextField
className='name'
name='name'
value={name}
type='text'
onChange={(e) => setName(e.target.value)}
/>
</FormControl >
<FormControl fullWidth margin="normal">
<InputLabel htmlFor="email">Email</InputLabel>
<TextField
className='email'
name='email'
value={email}
type='text'
onChange={(e) => setEmail(e.target.value)}
/>
</FormControl>
<FormGroup >
<Button variant="contained" type="submit">Add
</Button>
</FormGroup>
</form>
);
};
export default UserForm;
JSON
[
{ "id": 1, "name": "Alice", "email": "[email protected]" },
{ "id": 2, "name": "Bob", "email": "[email protected]" },
{ "id": 3, "name": "Bobbb", "email": "[email protected]" }
]
我通过在浏览器中禁用一个名为 McAfee® WebAdvisor 的扩展程序来解决该警告,
我在输入和按钮中收到了上述警告,但是我通过禁用 McAfee® WebAdvisor 扩展解决了这两个警告。
希望对你有帮助