【发布时间】:2019-02-03 22:14:12
【问题描述】:
我只是在学习反应阿波罗。我发现这个错误很难调试。当然,我搜索了多个博客文章、问答网站并尝试了其中的一些。没有人工作。实际上,我想将一个值传递给 GraphQL 方法 Mutation 中的参数(它的值是一个对象)。这是我的源代码
const client = new ApolloClient({
uri: "www.example.com"
});
const ADD_PARTNER = gql`
mutation LE($input: inputPartner!) {
le_addpartner(input: $input) {
result {
code
message
success
}
partnerID
}
}
`;
class AddTodo extends React.Component {
state = {
name: '',
nameSlug: '',
bankLogo: null,
phoneNumber: '',
companyProfile: '',
metaDescription: '',
metaTitle: '',
formSchema: ''
}
handleValueChange = (e) => {
this.setState({
[e.target.id]: e.target.value
})
}
render () {
return (
<Mutation mutation={ADD_PARTNER}>
{(le_addpartner, { data }) => (
<form
onSubmit={e => {
e.preventDefault();
le_addpartner({
variables: {
input: {...this.state}
}
})
}}
>
<div className="form-group">
<label htmlFor="name">Name</label>
<input type="text" className="form-control" id="name" onChange={(e) => this.handleValueChange(e)} />
</div>
<div className="form-group">
<label htmlFor="nameSlug">Name Slug</label>
<input type="text" className="form-control" id="nameSlug" onChange={(e) => this.handleValueChange(e)} />
</div>
<div className="form-group">
<label htmlFor="bankLogo">Bank Logo</label>
<input type="text" className="form-control" id="bankLogo" onChange={(e) => this.handleValueChange(e)} />
</div>
<div className="form-group">
<label htmlFor="phoneNumber">Phone Number</label>
<input type="text" className="form-control" id="phoneNumber" onChange={(e) => this.handleValueChange(e)} />
</div>
<div className="form-group">
<label htmlFor="companyProfile">Company Profile</label>
<textarea className="form-control" id="companyProfile" rows="3" onChange={(e) => this.handleValueChange(e)}></textarea>
</div>
<div className="form-group">
<label htmlFor="metaDescription">Meta Description</label>
<textarea className="form-control" id="metaDescription" rows="3" onChange={(e) => this.handleValueChange(e)}></textarea>
</div>
<div className="form-group">
<label htmlFor="metaTitle">Meta Title</label>
<textarea className="form-control" id="metaTitle" rows="3" onChange={(e) => this.handleValueChange(e)}></textarea>
</div>
<div className="form-group">
<label htmlFor="formSchema">Form Schema</label>
<textarea className="form-control" id="formSchema" rows="4" onChange={(e) => this.handleValueChange(e)}></textarea>
</div>
<button type="submit" className="btn btn-primary">Add Partner</button>
</form>
)}
</Mutation>
)
}
}
const App = () => (
<ApolloProvider client={client}>
<div style={{ marginTop: '50px' }}>
<h2>
GraphQL Mutation Partner{' '}
<span aria-labelledby='jsx-a11y/accessible-emoji' role='img'>????</span>
</h2>
<AddTodo />
</div>
</ApolloProvider>
);
我提交表单时遇到的错误是
[网络错误]: SyntaxError: Unexpected token
任何帮助将不胜感激:D
【问题讨论】:
标签: reactjs graphql react-apollo