【发布时间】:2019-06-24 11:57:37
【问题描述】:
当我尝试从我的 UI 向 mi SERVER 发送寄存器时,会向我发送此消息
POST http://localhost:4000/graphql 400(错误请求) index.js:70 [GraphQL 错误]:消息:所需类型的变量“$description” “细绳!”未提供。,位置:[对象对象],路径: 未定义的 index.js:75 [网络错误]:ServerError:响应不 成功:收到状态码
这是我的类组件
import React, { Component, Fragment } from 'react';
import { Query, Mutation } from 'react-apollo';
import BrandItem from '../models/BrandItem';
import * as BrandService from '../services/BrandService';
export class Brand extends Component {
render() {
let input;
return (
<div className="divform">
<Mutation mutation={BrandService.ADD_BRAND}>
{
(addBrand, { data }) => (
<form onSubmit={e => {
e.preventDefault();
addBrand({ variables: { type: input.value } });
input.value = '';
}}>
<label htmlFor="description">Description</label>
<input type="text" id="description" name="description" required ref={node => { input = node; }} />
<input type="submit" value="Send" />
</form>
)
}
</Mutation>
<div>
<Fragment>
<Query query={BrandService.BRAND_QUERY}>
{
({ loading, error, data }) => {
if (loading) return <div><h4>loading....</h4></div>
if (error) console.log(error);
return <Fragment>
<table>
<thead>
<tr>
<th>Id</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{
data.brands.map(brand => (
<BrandItem key={brand.id} brand={brand} />
))
}
</tbody>
</table>
</Fragment>
}
}
</Query>
</Fragment>
</div>
</div>
)
}
}
export default Brand
这是我的服务
import gql from 'graphql-tag';
export const BRAND_QUERY = gql`
query BrandQuery {
brands {
id
description
}
}
`;
export const ADD_BRAND = gql`
mutation AddBrand($description: String!){
addBrand(description: $description){
id
}
}
`;
【问题讨论】:
标签: reactjs graphql apollo mutation