jaydenseric / graphql-upload

Middleware and a scalar Upload 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 132 forks source link

Unexpected token P in JSON at position 0 #242

Closed shriram10567 closed 3 years ago

shriram10567 commented 3 years ago

i apologize for creating a new issue but i have tried everything for the past few days to no avail.

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

Duplicate of https://github.com/jaydenseric/graphql-upload/issues/241.