[英]Getting Error Variable “$name” of required type “String!” was not provided. on firing Graphql request from React-Apollo
Here's the 1st error on component rendering:这是组件渲染的第一个错误:
POST http://localhost:4000/graphql 500 (Internal Server Error) POST http://localhost:4000/graphql 500(内部服务器错误)
[GraphQL error]: Message: Variable "$name" of required type "String." [GraphQL 错误]:消息:所需类型“字符串”的变量“$name”。 was not provided.
没有提供。
The 2nd error on firing query(submitting form):触发查询的第二个错误(提交表单):
this.props.movieByName is not a function...
Here's the code这是代码
import React, { Component } from 'react';
import {HashLink as Link} from 'react-router-hash-link'
import { graphql } from 'react-apollo';
import {flowRight as compose} from 'lodash'
import { gql } from "apollo-boost";
const movieByName = gql`
query SomeName($name: String!){
movieByName(name: $name){
name
genre
year
}
}
`
class Header extends Component {
state = {
name: '',
genre: '',
year: ''
}
searchSubmit = (event) => {
event.preventDefault()
this.props.movieByName({
variables: {
name: event.target.value
}
})
console.log(this.props)}
render(){
return (
<div className="topnav">
<a className="logo" href="/">Movie Maker</a>
<div className="search-container">
<form onSubmit={this.searchSubmit}>
<Link smooth to="#form">Add Movies</Link>
<input type="text" placeholder="Search.." name="search"
onChange={(e) => this.setState({name: e.target.value})}/>
<button type="submit"><i className="fa fa-search"></i></button>
</form>
</div>
</div>
);}}
export default graphql(movieByName)(Header)
$name
.$name
的变量。String
and it cannot be undefined or null (it's required )String
类型,并且不能是未定义的或 null(它是必需的)event.target.value
when your searchSubmit
executes?searchSubmit
时event.target.value
的值是多少? I suspect that it's actually undefinedCall your query with the name value defined in the state.使用 state 中定义的名称值调用您的查询。 Because you update the state within the inputs
onChange
handler, the most up to date value will be stored there因为您在输入
onChange
处理程序中更新了 state,所以最新的值将存储在那里
this.props.movieByName({ variables: { name: this.state.name } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.