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

How do I fix uploads on non-express servers #356

Open
cleveng opened this issue Dec 6, 2024 · 0 comments
Open

How do I fix uploads on non-express servers #356

cleveng opened this issue Dec 6, 2024 · 0 comments

Comments

@cleveng
Copy link

cleveng commented Dec 6, 2024

i use async-graphql and async-graphql-actix-web as a backend, and use nextjs as a frontend.
when i upload a file, it doesn't work. but when i use curl to upload a file, it works.

apollo.ts config

'use client'

import { ApolloLink, from, HttpLink } from '@apollo/client'
import { loadDevMessages, loadErrorMessages } from '@apollo/client/dev'
import { setContext } from '@apollo/client/link/context'
import { onError } from '@apollo/client/link/error'
import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename'
import { ApolloClient, InMemoryCache } from '@apollo/experimental-nextjs-app-support'
import createUploadLink from 'apollo-upload-client/createUploadLink.mjs'

import { useStore } from '@/store'

import { appid, isDev } from '@config/index'

if (isDev) {
  loadDevMessages()
  loadErrorMessages()
}

const BASE_URL = `${process.env.NEXT_PUBLIC_BASE_URL}/graphql`

const httpLink = new HttpLink({
  uri: BASE_URL
})

// https://www.apollographql.com/docs/react/networking/authentication
const authLink = setContext((_, { headers }) => {
  const token = useStore.getState().token
  const app = useStore.getState().app ?? ''

  return {
    // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
    headers: {
      ...headers,
      Authorization: token ? `Bearer ${token}` : '',
      Appid: appid,
      App: app
    }
  }
})

// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const uploadLink = createUploadLink({
  uri: BASE_URL,
  headers: { 'Apollo-Require-Preflight': 'true' }
}) as ApolloLink

const removeTypenameLink = removeTypenameFromVariables()

const errorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors)
    graphQLErrors.forEach(({ message, locations, path }) =>
      // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
      console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
    )

  if (networkError) console.log(`[Network error]: ${networkError}`)
})

// have a function to create a client for you
// https://www.apollographql.com/docs/react/data/file-uploads
export const makeClient = () => {
  return new ApolloClient({
    cache: new InMemoryCache({
      addTypename: false
    }),
    link: from([authLink, errorLink, httpLink, removeTypenameLink, uploadLink]),
    defaultOptions: {
      query: {
        context: {
          fetchOptions: {
            next: {
              revalidate: 60 * 60 * 3 // 3 hours
            }
          }
        }
      }
    }
  })
}

mutation.graphql file

mutation UploadFile($input: Upload!) {
    uploadFile(file: $input) {
      file_url
    }
}
curl localhost:8090/graphql \
  -F operations='{ "query": "mutation ($file: Upload!) { uploadFile(file: $file) { file_url } }", "variables": { "file": null } }' \
  -F map='{ "0": ["variables.file"] }' \
  -F [email protected]

{"data":{"uploadFile":{"file_url":"./storage/mYXl9GY7\\bb43abc6-6a48-4e20-86e1-5997c5b15bff.txt"}}}

when i use curl to upload a file, it works. and i use nextjs to upload a file, it doesn't work, i don't know why, how to fix it

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

1 participant