rmosolgo / graphiql-rails

Mount the GraphiQL query editor in a Rails app
MIT License
447 stars 135 forks source link

SyntaxError: Unexpected token < in JSON at position 0 #29

Closed NicholasLYang closed 7 months ago

NicholasLYang commented 6 years ago

I'm getting this error when running graphiql on my rails app. I'm following this tutorial. From what I've seen from other posts it probably means my Rails app is sending HTML and not JSON. But I'm not really clear why that's happening. Could someone clear up why this is happening?

rmosolgo commented 6 years ago

Yeah, mine does this when the server responds with 500. Is there a clue in your Rails log as to what the error was (/is) ?

Also, can you check the Chrome Devtools "network" tab, do you see any error responses there?

NicholasLYang commented 6 years ago

That's a good call. I'm getting an ArgumentError (wrong number of arguments (given 1, expected 0)): in my landbnb_schema.rb. My schema is the following:

LandbnbSchema = GraphQL::Schema.define do
#  mutation(Types::MutationType)
  query(Types::QueryType)
end

The mutation is commented out because I haven't implemented it yet

mikeLspohn commented 6 years ago

@NicholasLYang Any luck solving this?

I'm getting same thing. This is from my devtools console

Warning: Failed prop type: VariableEditor: prop type readOnly is invalid; it must be a function, usually from React.PropTypes. in VariableEditor (created by GraphiQL) in GraphiQL

Dbz commented 6 years ago

I'm getting a similar error when receiving a 500 internal server error (404 Record Not Found). I posted a Stack Overflow question about this containing my code:

https://stackoverflow.com/questions/47095362/graphql-exception-handling-not-catching-exceptions

I think this may be a bug. I even have exception handling, and it ignores it. However, in other cases it does not ignore the exception handling.

shoan commented 6 years ago

Do you have the JWT_TOKEN in the .env?

sampahcoding commented 6 years ago

I got the same problem, this error appear ArgumentError (wrong number of arguments (given 1, expected 0)): when the code hit the following(line 2):

Schema = GraphQL::Schema.define do
  query Query
end

Turns out I just need to import all the the Query schema (which is Query on above code) and all related data types like below:

#app/graphql/queries.rb
Query = GraphQL::ObjectType.define do
  name 'Query'

  field :allProfiles, !types[ProfileType] do
    description 'Get all xxxx'
    resolve ->(_obj, args, _ctx) {
     # calling datas
    }
  end
end
require_relative 'queries'

Schema = GraphQL::Schema.define do
  query Query
end
ConorSheehan1 commented 5 years ago

Finally got to the bottom of this.

In the tutorial they use DateTimeType in link_type.rb but it's undefined.

Seems the DateTimeType has been a source of confusion for a while. https://stackoverflow.com/questions/47960194/graphql-ruby-date-or-datetime-type https://github.com/rmosolgo/graphql-ruby-demo/issues/27

You could define DateTimeType in app/graphql/types/date_time_type.rb or comment out the created_at field.

# app/graphql/types/date_time_type.rb
module Types
  class DateTimeType < GraphQL::Schema::Scalar
    def self.coerce_input(value, _context)
      Time.zone.parse(value)
    end

    def self.coerce_result(value, _context)
      value.utc.iso8601
    end
  end
end

Edit: DateTimeType inherits from BaseScalar which is also a custom type. You can either add BaseScalar to app/graphql/types/base_scalar.rb or just replace it with GraphQL::Schema::Scalar in date_time_type.rb since BaseScalar is empty and just inherits from GraphQL::Schema::Scalar anyway.

Also, it seems graphiql will raise that error in almost all cases. I managed to figure this out by looking at the rails output and logs.

ghost commented 3 years ago

I was getting this error as well. It turns out the JSON error was the result of a trailing comma in my User model:

Issue:

app/models/user.rb (or relevant model)

class User < ApplicationRecord
  has_secure_password

  validates :name, presence: true
  validates :email, presence: true, uniqueness: true,
end

Solution:

Remove the trailing comma, so email validation should be:

  validates :email, presence: true, uniqueness: true

I realize it's an old issue but hopefully it helps someone following my search trail :)

Honeyzed commented 3 years ago

JsoN Parson error: Unexpected token:<
Plz sove this error

rmosolgo commented 7 months ago

It sounds like this can happen where there's an unrelated server error -- check your server log! If someone runs into other trouble with this, please open a new issue.