【问题标题】:Apollo-client _mutation input type - data provided is emptyApollo-client _mutation 输入类型 - 提供的数据为空
【发布时间】:2019-01-22 21:47:00
【问题描述】:

我正在使用 Apollo 客户端。我正在尝试对客户端进行突变。我想知道为什么当我进行突变时,服务器上传递的数据是无效的?

这是我的变异类型:

type: recipeType,
        args:{ 
            data: {
                name: 'data',
                type: recipeInputType
            }
        }, 
        resolve(parent, args) {
            const recipe = new recipeModel(args.data); 
            const newRecipe = recipe.save();
            if(!newRecipe){ 
                throw new Error("Error adding recipe")
            }
            console.log("recipe add with success!! ", args)
            return newRecipe
        }

这是我的变异请求:

const addRecipe = gql`
    mutation addRecipe($data: RecipeInput){ 
        addRecipe(data: $data){ 
          id
          title 
        }
    }
`;

我的 react-apollo 活页夹:

export default graphql(addRecipe)(Create); 

我的组件中的突变调用:

onSubmit = (e) =>{ 
    e.preventDefault();
    this.props.mutate({ 
      variables:{  

        title: this.state.title,
        ingredients: this.state.ingredients,          
    }
    })
  }

当控制台记录我在表单中提供的数据时,我的服务器返回:

{}

我不知道为什么。
我试图进行以下突变调用:

this.props.addRecipe() 

但我的控制台返回:

this.props.addRecipe() 不是函数

为什么会有这种行为?
任何提示都会很棒,

谢谢

【问题讨论】:

    标签: javascript reactjs graphql react-apollo apollo-client


    【解决方案1】:

    要访问您的数据,您必须在变量对象中重新输入数据标题。就我而言:

    this.props.mutate({ 
          variables:{
              data: {
                {...}
            }
        }
    

    【讨论】:

    猜你喜欢
    • 2021-06-06
    • 2020-12-06
    • 2019-12-19
    • 2018-07-18
    • 2019-03-15
    • 2020-09-25
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多