Мутации работают с игровой площадки, но не с моего компонента.Это ошибка 400 без других подробностей.Мутация выглядит хорошо.Пожалуйста помоги!
Код мутации сервера в резольвере
Mutation: {
addProduct: async (_, { input }) => {
console.log('in new prod');
const product = await new Product(input);
return product.save();
},
Мутация клиента:
export const addProductMutation = gql`
mutation addProduct($productDetails: productDetails!) {
addProduct(input: $productDetails)
}
`;
Код компонента ниже импорта React, {Component} из 'response';импорт TextField из '@ material-ui / core / styles' import {compose, graphql} из'act-apollo 'import {withStyles} из' @ material-ui / core / styles 'кнопка импорта из' @ material-ui '/ ядро / Button ';импортировать NumberFormat из «ответного числа»импортировать PropTypes из 'prop-types';import {addProductMutation} из '../../../api';
const styles = theme => ({
container: {
display: 'flex',
flexWrap: 'wrap',
},
textField: {
marginLeft: theme.spacing.unit,
marginRight: theme.spacing.unit,
width: 200,
},
dense: {
marginTop: 19,
},
menu: {
width: 200,
},
});
function NumberFormatCustom(props) {
const { inputRef, onChange, ...other } = props;
return (
<NumberFormat
{...other}
getInputRef={inputRef}
onValueChange={values => {
onChange({
target: {
value: values.value,
},
});
}}
// thousandSeparator
// prefix=""
decimalScale={2}
/>
);
}
function NumberFormatNoDecimal(props) {
const { inputRef, onChange, ...other } = props;
return (
<NumberFormat
{...other}
getInputRef={inputRef}
onValueChange={values => {
onChange({
target: {
value: values.value,
},
});
}}
// thousandSeparator
// prefix=""
decimalScale={0}
/>
);
}
NumberFormatCustom.propTypes = {
inputRef: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
};
class ProductDetailsForm extends Component {
constructor(props){
super(props);
this.state = {
name: "",
description: "",
price: "",
sku: "",
mrp: "",
in_stock: "",
status: ""
}
}
displayCategories() {
let data = this.props.getCategoriesQuery;
if (data.loading) {
return (<option>Loading...</option>)
} else {
return data.categories.map(category => {
return (<option key={category.id} value={category.id} >{category.name}</option>)
});
}
}
submitForm(e){
e.preventDefault();
console.log('submitForm');
console.log(this.state);
this.props.addProductMutation({
variables: {
productDetails: {
name: this.state.name,
description: this.state.description,
sku: this.state.sku,
price: this.state.price,
mrp: this.state.mrp,
status: this.state.status,
in_stock: this.state.in_stock,
}
}
}).then((result) => {
console.log(result)
}).catch((err) => {
console.log('ERRRRR')
console.log(err)
});
}
render() {
console.log(this.props)
// console.log(this.state)
const { classes, values, handleChange, numberformat } = this.props;
return (
<React.Fragment >
<br />
<h2>Add Product Details - Step 1 </h2>
<TextField
id="standard-name"
label="Name"
className={classes.textField}
defaultValue={values.name}
onChange={(e) => this.setState({name: e.target.value})}
margin="normal"
/>
<br />
<TextField
id="in_stock"
label="Units In Stock"
className={classes.textField}
defaultValue={values.in_stock}
InputProps={{
inputComponent: NumberFormatNoDecimal,
}}
value={numberformat}
onChange={(e) => this.setState({in_stock: e.target.value})}
margin="normal"
/>
<br />
<TextField
id="description"
label="Description"
multiline
className={classes.textField}
defaultValue={values.description}
onChange={(e) => this.setState({description: e.target.value})}
margin="normal"
/>
<br />
<TextField
id="sku"
label="SKU"
className={classes.textField}
defaultValue={values.sku}
onChange={(e) => this.setState({sku: e.target.value})}
margin="normal"
/>
<br />
<TextField
id="price"
label="Price"
className={classes.textField}
defaultValue={values.price}
onChange={(e) => this.setState({price: e.target.value})}
InputProps={{
inputComponent: NumberFormatCustom,
}}
margin="normal"
/>
<br />
<TextField
id="mrp"
label="MRP"
className={classes.textField}
defaultValue={values.mrp}
onChange={(e) => this.setState({mrp: e.target.value})}
InputProps={{
inputComponent: NumberFormatCustom,
}}
margin="normal"
/>
<br />
<TextField
id="status"
label="Status"
className={classes.textField}
defaultValue={values.status}
onChange={(e) => this.setState({status: e.target.value})}
margin="normal"
/>
<br />
<br />
<Button onClick={this.submitForm.bind(this)} variant="contained" color="primary" className={classes.button}>
Continue
</Button>
</React.Fragment>
)
}
}
export default compose(
withStyles(styles),
// graphql(getCategoriesQuery, {name: "getCategoriesQuery"}),
graphql(addProductMutation, {name: "addProductMutation"})
)(ProductDetailsForm);