reactjs - '类型错误: parse is not a function' when using gql

标签 reactjs graphql

我从 this 开始学习graphql和reactjs的教程并进行了修改。 现在我在尝试调用 gql 时收到此错误: graphql error

这是执行调用的类:

import React from 'react';
import gql from 'graphql-tag';
import { graphql } from 'react-apollo'

class BookForm extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            title: '',
            author: '',
            src: '',
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleChange(event) {
        this.setState({[event.target.name]: event.target.value });
    }

    handleSubmit = (e) => {
        this.props.addBook({
            variables: {
                author: this.state.author,
                title: this.state.title,
                src: this.state.src
            }
        });
    }

    render() {
        return (
            <form className="book__form" onSubmit={this.handleSubmit}>
                <label>
                    Title:
                    <input type="text" name="title" value={this.srtate.title} onChange={this.handleChange} />
                </label>
                <label>
                    Author:
                    <input type="text" name="author" value={this.state.author} onChange={this.handleChange} />
                </label>
                <label>
                Src:
                <input type="text" name="src" value={this.state.src} onChange={this.handleChange}/>
                </label>
                <label>
                    <input type="submit" value="Submit" />
                </label>
            </form>
        );
    }
}


const addBook =  gql `mutation addBook ($author: String!, $title: String!, $: ID!) {
    addBook(author: $author, title: $title, src: $src) {
      id,
      author,
      title,
      src,
    }
  }`

const addBookWithMutation = graphql(addBook)(BookForm);
export default addBookWithMutation;

查询 graphql 返回所有数据,但现在当我尝试添加新数据时却一无所获。 我在代码中做错了什么还是这是 graphql-tag 的问题?

谢谢

最佳答案

似乎存在兼容性问题,graphql-js 0.13.1 版本破坏了 graphql-tag。您可以通过手动解决版本控制问题来修复 -

yarn add apollo-boost@0.1.1 graphql@0.13.0 react-apollo@2.0.4

作为引用,这里是 issue on github

关于reactjs - '类型错误: parse is not a function' when using gql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48965279/

相关文章:

reactjs - react 钩子(Hook)如何确定它们用于的组件?

javascript - 当我更改 componentDidMount 中的状态时,为什么我的组件没有重新渲染?

json - 如何在 swift 4 中使用 Graphql 获取 json 值数组?

error-handling - 未处理的PromiseRejectionWarning : Error: You must `await server.start()` before calling `server.applyMiddleware()` at ApolloServer

javascript - 如何修复 gatsbyjs 中的 "Unknown argument ' slug' "?

graphql - 如何使用 GraphQL 放置/更新嵌套数据?

reactjs - 网页游戏开发中react/redux和unity的集成

javascript - 在 react 中导出高阶组件

reactjs - 无法创建 react 项目

c# - 如何在 GraphQL HotChocolate 中实现订阅?