亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

變量“$file”的值無效 {};上傳值無效

變量“$file”的值無效 {};上傳值無效

jeck貓 2023-07-06 16:37:14
我正在使用 GraphQLClient 將graphql-request請求發送到我的服務器。我正在嘗試通過執行以下操作來上傳文件:const graphQLClient = new GraphQLClient('http://localhost:4000/graphql', {    credentials: 'include',    mode: 'cors',});const source = gql`    mutation uploadImage($file: Upload!) {        uploadImage(file: $file)    }`;const file: RcFile = SOME_FILE; // RcFile (from antd) extends Fileawait graphQLClient.request<{uploadImage: boolean}>(source, { file });但是,當我以這種方式向服務器發送請求時,出現以下錯誤:GraphQLError: Variable \"$file\" got invalid value {}; Upload value invalid這就是我的請求在控制臺中的樣子:operations: {    "query":"\n mutation uploadProfileImage($file: Upload!){\n uploadProfileImage(file: $file)\n }\n",      "variables":{"file":null}}map: {"1":["variables.file"]}1: (binary)其他人遇到過這個問題嗎?我似乎無法將文件上傳到我的后端。
查看完整描述

4 回答

?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

我通過在配置中將上傳選項設置為 false 解決了該問題ApolloServer。

new ApolloServer({ schema, context, uploads: false })

然后使用graphqlUploadExpress()來自 的中間件graphql-upload

app.use(graphqlUploadExpress({ maxFileSize: 10000, maxFiles: 10 }));

希望這對遇到與我相同問題的人有所幫助??


查看完整回答
反對 回復 2023-07-06
?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

請確保您還使用以下客戶端實現apollo-upload-client:


import { ApolloClient, InMemoryCache } from "@apollo/client";

import { createUploadLink } from 'apollo-upload-client';


const client = new ApolloClient({

? cache: new InMemoryCache(),

? link: createUploadLink({

? ? ? uri: 'http://localhost:4000/graphql'

? }),

});


查看完整回答
反對 回復 2023-07-06
?
紅顏莎娜

TA貢獻1842條經驗 獲得超13個贊

這取決于您使用的 ApolloClient。


1-如果使用 import { ApolloClient } from 'apollo-client' 必須使用“ createUploadLink ”而不是“ createHttpLink ”意味著,


import { createUploadLink } from 'apollo-upload-client'

const httpLink = createUploadLink({

  uri: httpEndpoint,

})

2-如果使用createApolloClient,則精確這個包:


import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client'

const { apolloClient, wsClient } = createApolloClient({

    ...defaultOptions,

    ...options,

  })

``

You do not need to set anything and Upload work complete.


查看完整回答
反對 回復 2023-07-06
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

此外,請嘗試確保csrfPrevention您的 ApolloServer 配置中未將其設置為 true


  const server = new ApolloServer({

    typeDefs,

    resolvers,

    csrfPrevention: false, // if this is set to true, uploads will fail

    uploads: false,

    cache: "bounded",

    plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })],

  });


查看完整回答
反對 回復 2023-07-06
  • 4 回答
  • 0 關注
  • 225 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號