Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQLError: Syntax Error: Unexpected Name "undefined"

I have stored all of my queries on their on page:

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 };

On my form page I have imported the createStory mutation and I am trying to bind it with the Component, like so:

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 (
  <div className="card">
    <h1>Write A Story</h1>
    <form onSubmit={this.onSubmit}>
        // Cut out my form details for space
      <input type="submit" value="Write Story" />
    </form>
  </div>
   );
  } 
 }

 const createStoryMutation = graphql(createStory, {
   name: 'createStory'})(Form)

export default withRouter(createStoryMutation)

However, I have continually been running receiving the error.

GraphQLError: Syntax Error: Unexpected Name "undefined"

At first I suspected it was a packaging issue, so one of the ways I've been trying to solve it is by alternating between graphql-tag and react-apollo. I've but when messing with those I am constantly getting Object(...) is not a function, which I know has to do with whether or not the imported functions are wrapped in {brackets} or not.

I have tried just about everything that I am aware of to get this code to work, yet to no avail. I know that my post mutation is to long as well(i.e. it should be wrapped in an input object), but I was just trying to make things functional before I clean up.

That being said, thank you in advance.

like image 387
Lullaby Avatar asked Dec 14 '22 15:12

Lullaby


1 Answers

Notice that you are doing import graphql from 'graphql-tag' at the top of the Form component file. I'd imagine that you are actually trying to do import { graphql } from 'react-apollo'; based on the way that you are using graphql as a HOC in the code you posted.

Your import/export is also incorrect. You shouldn't have a default export but rather put the export keyword in front of each query definition. Then you need to change import createStory from '../../Queries/Queries' to import {createStory} from '../../Queries/Queries'. Currently you are setting createStory equal to { getStories, updateStory, createStory }.

like image 199
Jclangst Avatar answered Dec 18 '22 00:12

Jclangst