Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error/syntaxError@highlightSourceAtLocation throwing error #274

Closed
ashtonsix opened this issue Jan 12, 2016 · 5 comments
Closed

error/syntaxError@highlightSourceAtLocation throwing error #274

ashtonsix opened this issue Jan 12, 2016 · 5 comments

Comments

@ashtonsix
Copy link

Hi,

Got started w/ graphql w/ graffiti-mongoose & express-graphql. When using graphiql the server was hanging on every request.

Attempted to use graphql function instead, when called w/ any query for my schema I got this error:

{ errors: [ [TypeError: source.body.split is not a function] ] }

That error is useless and has very bad DX, not sure whether to create a seperate issue for it or whether the conversation in #28 should expand it's scope to concern errors thrown in graphql in addition to the schema.

At graphql/graphql.js:70 I added a log

  })['catch'](function (error) {
    console.log(error.stack)
    return { errors: [error] };
  });

And got this stack:

TypeError: source.body.split is not a function
     at highlightSourceAtLocation (/home/ashton/pluralthinker/main/node_modules/graphql/error/syntaxError.js:43:27)
     at syntaxError (/home/ashton/pluralthinker/main/node_modules/graphql/error/syntaxError.js:29:155)
     at unexpected (/home/ashton/pluralthinker/main/node_modules/graphql/language/parser.js:862:33)
     at parseDefinition (/home/ashton/pluralthinker/main/node_modules/graphql/language/parser.js:122:9)
     at parseDocument (/home/ashton/pluralthinker/main/node_modules/graphql/language/parser.js:79:22)
     at parse (/home/ashton/pluralthinker/main/node_modules/graphql/language/parser.js:40:10)
     at /home/ashton/pluralthinker/main/node_modules/graphql/graphql.js:62:49
     at new Promise (/home/ashton/pluralthinker/main/node_modules/core-js/modules/es6.promise.js:197:7)
     at Promise.exports.(anonymous function).target.(anonymous function).function.target.(anonymous function).F (/home/ashton/pluralthinker/main/node_modules/core-js/library/modules/$.export.js:30:36)

I then logged source:

Source {
  body: { query: 'query {\n  viewer {\n    organization(id: "5687429cf7f966ce5a94370e") {\n      id\n    }\n  }\n}' },
  name: 'GraphQL request' }

typeof source.body === "object"

This is the relevant mongoose model used to generate relevant part of schema:

import mongoose, {Schema} from 'mongoose'

export default mongoose.model(
  'Organization',
  new Schema({
    name: String,
    domain: String,
    channels: [{
      type: Schema.Types.ObjectId,
      ref: 'Channel'}],
    createdAt: {
      type: Date,
      default: Date.now}}))

This is my graphql server in current state of debugging:

import 'babel-polyfill'
import {getSchema} from '@risingstack/graffiti-mongoose'
import express from 'express'
import {graphql} from 'graphql'
import graphqlHTTP from 'express-graphql'
import morgan from 'morgan'
import * as models from '../schema'

const PORT = 8200 + parseInt(process.env.NODE_APP_INSTANCE || 0, 10)
const schema = getSchema(_.values(models))

express().
  use(morgan('dev')).
  use('/api/graphql', (req, res, next) => (
    graphql(schema, req.query).then(::console.log), console.log(req.query), next())).
  use('/api/graphql', graphqlHTTP({schema, pretty: true, graphiql: false})).
  listen(
    PORT, 'localhost',
    err => console.log(err ? err : `Listening at http://localhost:${PORT}`))
@ashtonsix
Copy link
Author

I should note no Organization w/ id 5687429cf7f966ce5a94370e existed when the sample query was made, though I ran into similar errors w/ mutations intended to create documents

@dschafer
Copy link
Contributor

I then logged source:

Source {
body: { query: 'query {\n viewer {\n organization(id: "5687429cf7f966ce5a94370e") {\n id\n }\n }\n}' },
name: 'GraphQL request' }
typeof source.body === "object"

Yeah, this seems like the issue.

https://github.com/graphql/graphql-js/blob/a941554fc56d607fecebe2188a7ca8848835c2a8/src/language/source.js#L17L19

source.body should be a String, but in your example it looks like it is an object with a single query property, where query is a string.

at /home/ashton/pluralthinker/main/node_modules/graphql/graphql.js:62:49

If you console.log requestString there, is it the same object?

@ashtonsix
Copy link
Author

Ah, yes. In my example I was passing an object to graphql, updated to graphql(schema, req.query && req.query.query). The Promise never completes like w/ express-graphql.

I realized this is probably b/c there's no database connection. Added mongoose.connect('mongodb://localhost:27017/pluralThinker'). Now everything works as expected.

This is bad day for me because I was silly. Thanks for help.

@IndependentContractor
Copy link

For others who encounter this error. I resolved it by using the print method in graphql-tag/printer to create the string version of the query I wanted to pass in my custom network interface

@josephsiefers
Copy link

Print is now deprecated in graphql-tag but you can import it from graphql-js as follows:
import { print } from 'graphql/language/printer'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants