简体   繁体   English

获取所需类型“String”的错误变量“$name”。 没有提供。 在从 React-Apollo 发出 Graphql 请求时

[英]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)

What does this error mean?这个错误是什么意思?

  • Your graphql schema is expecting a variable called $name .您的 graphql 架构需要一个名为$name的变量。
  • That variable needs to be of type String and it cannot be undefined or null (it's required )该变量必须是String类型,并且不能是未定义的或 null(它是必需的)

Debugging information调试信息

  • What's the value of event.target.value when your searchSubmit executes?执行searchSubmitevent.target.value的值是多少? I suspect that it's actually undefined我怀疑它实际上是未定义的

Solution解决方案

  • Call 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.

相关问题 Graphql react-apollo内省片段匹配器 - Graphql react-apollo IntrospectionFragmentMatcher Graphql / React-Apollo:React Map函数错误 - Graphql/React-Apollo: React map function error 错误 GraphQL 查询中出现错误:所需类型“String!”的变量“$slug” 没有提供 - error There was an error in your GraphQL query: Variable “$slug” of required type “String!” was not provided Graphql react-apollo IntrospectionFragmentMatcher问题 - Graphql react-apollo IntrospectionFragmentMatcher issues 如何使用React-Apollo处理GraphQL错误? - How to handle GraphQL errors with React-Apollo? Gridsome:错误:所需类型的变量“字符串!” 没有提供 - Gridsome: Error: Variable of required type "String!" was not provided ReactJS/Javascript/Graphql/React-apollo:无法读取未定义的属性“名称” - ReactJS/Javascript/Graphql/React-apollo: Cannot read property 'name' of undefined 如何使用react和react-apollo从graphql服务器获取数据? - how to get data from graphql server using react and react-apollo? Graphql,react-apollo如何在加载组件状态时将变量传递给查询 - Graphql, react-apollo how to transfer variable to query at loading component state 将输入值传递给我的graphql突变-react / react-apollo - Passing input value to my graphql mutation - react/react-apollo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM