nuxt-modules / apollo

Nuxt.js module to use Vue-Apollo. The Apollo integration for GraphQL.
https://apollo.nuxtjs.org
MIT License
954 stars 198 forks source link

JSON.parse: unexpected character at line 1 column 1 #293

Open brendan1212 opened 4 years ago

brendan1212 commented 4 years ago

Version

v4.0.0-rc.19

Reproduction link

https://github.com/pachiraDIG/hub

Steps to reproduce

Repo

After several hours, I can't seem to pinpoint the cause of this error when running a query.

Config for @nuxt/apollo module:

apollo: {
    includeNodeModules: true,
    clientConfigs: {
      default: {
        httpEndpoint: '~/.netlify/functions/fauna-graphql'
      }
    }
  }

The error is coming through the apollo module:

 var vueApolloOptions = Object.assign(providerOptions, {
    errorHandler: function errorHandler(error) {
      console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message);
    }
  });

The Netlify Function, taken directly from a Netlify team member's example repo:

const { ApolloServer } = require('apollo-server-lambda')
const { createHttpLink } = require('apollo-link-http')
const fetch = require('node-fetch')
const { introspectSchema, makeRemoteExecutableSchema } = require('graphql-tools')

exports.handler = async function(event, context) {
  /** required for Fauna GraphQL auth */
  if (!process.env.FAUNADB_SERVER_SECRET) {
    const msg = `
    FAUNADB_SERVER_SECRET missing. 
    Did you forget to install the fauna addon or forgot to run inside Netlify Dev?
    `
    console.error(msg)
    return {
      statusCode: 500,
      body: JSON.stringify({ msg })
    }
  }

  const b64encodedSecret = Buffer.from(
    process.env.FAUNADB_SERVER_SECRET + ':' // weird but they
  ).toString('base64')

  const headers = { Authorization: `Basic ${b64encodedSecret}` }

  /** standard creation of apollo-server executable schema */
  const link = createHttpLink({
    uri: 'https://graphql.fauna.com/graphql', // modify as you see fit
    fetch,
    headers
  })

  const schema = await introspectSchema(link);

  const executableSchema = makeRemoteExecutableSchema({
    schema,
    link
  })

  const server = new ApolloServer({
    schema: executableSchema
  })

  return new Promise((yay, nay) => {
    const cb = (err, args) => (err ? nay(err) : yay(args))
    server.createHandler()(event, context, cb)
  })
}

Inside the component: (The faunadb schema is set up exactly from their example tutorial)

export default {
  data() {
    return {
      test: {},
    }
  },
  apollo: {
    test: gql`
      query {
        allWarehouses {
          data {
            _id
            name
            address {
              street
              city
              state
              zipCode
            }
          }
        }
      }
    `
  }
}

From reading similar questions regarding the same error (but not the same stack), it could be related to something trying parse HTML as JSON. I've also seen that it could be solved with JSON.stringify, but I don't know where the hayhay I should apply it.

Any suggestions?

What is expected ?

A clean return

What is actually happening?

Browser console: Error Network error: JSON.parse: unexpected character at line 1 column 1 of the JSON data

This bug report is available on Nuxt community (#c273)
Gomah commented 4 years ago

I had the same issue using the compression module on my server.

I'm not sure about how fauna handles that on their end but you could potentially attach x-no-compression to your request header to make it work