GraphQLError: Syntax Error: Cannot parse the unexpected character "\u00A0"
Asked Answered
H

3

5

I get an error when I make a query/mutation/subscription with gql (from 'graphql-tag').

Has anyone had this error and know how to fix it ?
Here is my code :

import React from 'react';
import { Mutation } from 'react-apollo';
import MUTATION from './query.js';
import gql from 'graphql-tag';

const ADD_POST_MUTATION = gql`
  mutation addPost($content: String!, $author: String!) {
    addPost(content: $content, author: $author) {
      content
      author
    }
  }
`;

export default class Tool extends React.Component {
  state = {
    content: '',
    author: localStorage.getItem('user') || ''
  };

  handleSubmit = (e, mutation) => {
    console.log(mutation);
    e.preventDefault();
    mutation({ variables: { content: this.state.content, author: this.state.author }})
      .then(res => console.log(res))
      .catch(e => console.log(e));
  }

  render() {
    return (
      <Mutation mutation={MUTATION}>
        {(addPost, { data }) => (
          <div>
            <div>Poster un commentaire</div>
            <form onSubmit={e => this.handleSubmit(e, addPost)}>
              <textarea onChange={e => this.setState({ content: e.target.value })}></textarea>
              <button type='submit'>Poster</button>
            </form>
          </div>
        )}
      </Mutation>
    );
  }
};

The error occurs again for this query:

const FETCH_POST_QUERY = gql`
  query {
    getPost {
      id
      content
      author
    }
  }
`;

And I got this error :

Error image (click to see)

Hydrogeology answered 29/7, 2018 at 10:42 Comment(1)
As per SO guidelines, all text, code, error messages & data must be typed in as text, not posted in image form. Please edit your question replacing the images with text. Text allows visitors to efficiently copy-paste exact error messages, code, & data into their editors & search engines to efficiently & without introducing more typos. Also, text in images can be difficult to read, especially on mobile devices, and images are not accessibility friendly. Thanks, all the best.Cryptograph
H
4

Solved!

There is a problem in graphql-tag, I had to rewrite to be sure there is no non-breaking space!

Hydrogeology answered 29/7, 2018 at 14:38 Comment(0)
J
4

copy your GQL code and use another editor like TextEdit to and paste as plain Text then copy the plain text and paste back in VSCode.

Jacobjacoba answered 24/2, 2019 at 23:41 Comment(1)
Pasting from Notepad and notepad2 didn't work. Typing GQL again workedMonogamous
B
1

VSCode users can download a plugin called "Non-breaking space highlighter" which will highlight all NBSP in red making them easy to find. Delete them from your gql query and the error will go away. I'm sure other editors have a similar plugin.

Barytone answered 18/7, 2021 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.