【发布时间】:2018-08-04 11:19:43
【问题描述】:
我目前在 Meteor/Apollo/GraphQL 上关注 this tutorial,并且在使用参数/变量进行突变时遇到了巨大的麻烦。这是我的代码和底部的一些注释!
代码
架构
type Resolution {
_id: String!
name: String!
}
type Mutation {
createResolution(name: String!): Resolution
}
解析器
import Resolutions from './resolutions'
export default {
Query: {
resolutions() {
return Resolutions.find({}).fetch()
}
},
Mutation: {
createResolution(obj, args, context) {
console.log('hey i get here')
}
}
}
使用突变的组件
import React, { Component } from 'react'
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'
const createResolutionQuery = gql`
mutation createResolution($name: String!) {
createResolution(name: $name) {
_id
}
}
`
class ResolutionForm extends Component {
submitForm = () => {
this.props
.createResolution({
variables: {
name: this.name.value
}
})
.then(d => console.log('data received'))
.catch(e => console.log(e))
}
render() {
return (
<div>
<input type="text" ref={input => (this.name = input)} />
<button onClick={this.submitForm}>Submit</button>
</div>
)
}
}
export default graphql(createResolutionQuery, {
name: 'createResolution'
})(ResolutionForm)
我知道的
- 当我尝试将查询发送到服务器时,我收到一个 http 400 错误,并且我收到以下 graphql 错误:“Mutation”类型的字段“createResolution”上的“未知参数”名称。”
- createResolution 在我的 graphiQL 中可用,但在文档中不显示任何参数。
- 教程中规定更改 .graphql 架构不会触发流星服务器重新加载,要应用更改,我必须修改我的“register-api”文件,该文件负责制作可执行架构并使用它创建 apollo 服务器.我做了虚假的更改来触发它,但它没有改变任何东西。
- 我在清除浏览器缓存后尝试重新启动服务器,但没有结果。
所以我认为我的问题在于突变参数(我知道的精彩演绎),但我无法弄清楚错字在哪里或我在哪里遗漏了一些东西。欢迎有新面孔的人提供帮助,谢谢:)
编辑
重新安装 npm 包解决了这个问题。
【问题讨论】:
-
用
this.props.createResolution(this.name.value)...试试吧,我认为你不必通过variables配置,因为你没有使用apollo客户端。 -
嘿,谢谢,我试过了,但我的组件确实期待这个带有变量 prop 的对象。我认为问题不是来自客户端,而是来自服务器相关文件,我的参数无法识别。
-
架构看起来不错。你能把你的 repo 放到 github 上吗?
-
是的here it is!
标签: javascript meteor graphql apollo