热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

GraphQLError:语法错误:意外的名称“未定义”

如何解决《GraphQLError:语法错误:意外的名称“未定义”》经验,为你挑选了1个好方法。

我已将所有查询存储在其页面上:

import gql from "graphql-tag";

const getStories = gql`
  query getStories {
      stories {
        id
        title
        author
        tagline
        summary
        rating
        you
        need
        go
        search
        find
        take
        returned
        changed
    }
  }
`

const createStory = gql`
  mutation($title: String!, $author: String!) {
    create (title: $title, author: $author) {
      id
      title
      author
      tagline
      summary
      rating
      you
      need
      go
      search
      find
      take
      returned
      changed
     }
    }
   `
  const updateStory = gql`
   mutation($id: ID!) {
    update(id: $id) {
     id
     title
     author
     tagline
     summary
     rating
     you
     need
     go
     search
     find
     take
     returned
     changed
   }
  }
 `

  export default { getStories, updateStory, createStory };

在我的表单页面上,我导入了createStory突变,并试图将其与Component绑定,如下所示:

import React, { Component } from "react"
import { withRouter } from 'react-router-dom'
import  graphql  from 'graphql-tag'
import createStory from '../../Queries/Queries'

class Form extends Component {
  constructor(props) {
    super(props);
      this.state = {
         story: {}
      };
   }

  OnChange= e => {
   const storyState = this.state.story;
    storyState[e.target.name] = e.target.value;
    console.log(this.props.createStory)
    console.log(storyState)
    this.setState(storyState);
  };

 OnSubmit= e => {
   e.preventDefault();
   console.log(this.state.story)
   this.props.createStory({
   variables: {
    story: this.state.story
   }
 })
this.props.history.replace('/')
};

render() {
 return (
  

Write A Story

// Cut out my form details for space
); } } const createStoryMutation = graphql(createStory, { name: 'createStory'})(Form) export default withRouter(createStoryMutation)

但是,我一直在运行中收到错误消息。

GraphQLError: Syntax Error: Unexpected Name "undefined"

起初我怀疑这是包装问题,所以我一直试图解决的方法之一是在graphql-tag和之间进行交替react-apollo。但是,当与那些我不断得到的东西搞混时Object(...) is not a function,我知道这与所导入的函数是否包装在{brackets}中有关。

我已经尝试了几乎所有我知道的使此代码正常工作的方法,但无济于事。我知道我的发布突变也很长(即应该将其包装在输入对象中),但是我只是想在清理之前使它们起作用。

话虽这么说,谢谢你。



1> Jclangst..:

请注意,您正在执行import graphql from 'graphql-tag'Form组件文件的顶部。我想您实际上是import { graphql } from 'react-apollo';根据graphql发布的代码中用作HOC 的方式来尝试做的。

您的导入/导出也不正确。您不应该具有默认导出功能,而应将export关键字放在每个查询定义的前面。然后,您需要更改import createStory from '../../Queries/Queries'import {createStory} from '../../Queries/Queries'。目前,您将设置createStory{ getStories, updateStory, createStory }


推荐阅读
author-avatar
humphrey7247
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有