jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 131 forks source link

Error: Unexpected token P in JSON at position 0 #241

Closed shriram10567 closed 3 years ago

shriram10567 commented 3 years ago

this my apollo client code

const httpLink = new createUploadLink({
    uri: 'http://localhost:4001/graphql',
    credentials: 'include', 
    headers: {
      cookie:
        (typeof window === 'undefined'
          ? ctx?.req?.headers.cookie
          : undefined) || '',
    },
  })

on the node server, i have used upload:false as per apollo docs.

const server = new ApolloServer({
    uploads: false
.....
})

this is what my resolver looks like

  @Mutation(() => Boolean)
  async addPicture(
    @Arg('picture', () => GraphQLUpload)
    { createReadStream, filename }: Upload
  ): Promise<boolean> {
    console.log(filename)
    return new Promise((resolve, reject) =>
      createReadStream()
        .pipe(createWriteStream(__dirname + `../images/${filename}`))
        .on('finish', () => resolve(true))
        .on('error', () => reject(false))
    )
  }

i am getting the following error: Error: Unexpected token P in JSON at position 0.

P.S. before marking this as closed, please look at this stackoverflow post: https://stackoverflow.com/questions/64971480/cant-upload-files-with-apollo-client-graphql-in-next-js-app-post-body-missing. this post explain the problem better than i have done here

jaydenseric commented 3 years ago

The unformatted code is too hard to read; please format it properly in an edit and then I'll take a look:

https://docs.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting

jaydenseric commented 3 years ago

@shriram1056 I edited your code to be syntax highlighted. Please do that yourself next time, as asked. Also, please edit the markdown link URL for the Stack Overflow link as it's currently broken.

Regarding the Error: Unexpected token P in JSON at position 0; most likely the response body is a text message from the server. The message starts with P, so what does it say? You can use the Chrome dev tools network inspector to see the GraphQL response body.

It after that it doesn't become obvious what the problem is, try to figure out if the problem is on the client or server by using the network inspector to see if the outgoing GraphQL request body is a correct GraphQL multipart request.

If it is a client issue, the GitHub issues of this repo is not the right place to ask for help. If it is a server issue, please consult with the Apollo Server community for support. They have a lot of ongoing discussion about their broken support for file uploads in their repos, and Apollo has instructions in their docs for how to get it going.

I'm leaving this issue closed for now, since no evidence of a graphql-upload bug has been presented.

ru4ert commented 3 years ago

The issue is related to the output of this error message, thrown by the Server. POST body missing, invalid Content-Type, or JSON object has no keys. For some reason, you have an issue and Apollo is trying to build a JSON object, which don't work for a plain text type response.

I guess your server config is wrong.

jaymarkjairi commented 3 years ago

@shriram1056 I edited your code to be syntax highlighted. Please do that yourself next time, as asked. Also, please edit the markdown link URL for the Stack Overflow link as it's currently broken.

Regarding the Error: Unexpected token P in JSON at position 0; most likely the response body is a text message from the server. The message starts with P, so what does it say? You can use the Chrome dev tools network inspector to see the GraphQL response body.

It after that it doesn't become obvious what the problem is, try to figure out if the problem is on the client or server by using the network inspector to see if the outgoing GraphQL request body is a correct GraphQL multipart request.

If it is a client issue, the GitHub issues of this repo is not the right place to ask for help. If it is a server issue, please consult with the Apollo Server community for support. They have a lot of ongoing discussion about their broken support for file uploads in their repos, and Apollo has instructions in their docs for how to get it going.

I'm leaving this issue closed for now, since no evidence of a graphql-upload bug has been presented.

I sorry to insert it here, since it was related to graphql-upload can you help me about this issue? https://stackoverflow.com/questions/68850296/apollo-federation-file-upload because I always see you're comments in other ticket of problem about graphql-upload thank you so much