如何将其他请求正文值添加到 Apollo GraphQL 请求



我正在将 Apollo 与 Scaphold.io 服务一起使用,为了将 blob 写入该服务,我需要能够向请求正文添加其他选项。

完整的 Scaphold 示例可以在这里找到:https://scaphold.io/docs/#uploading-files

但它做这样的事情:

form.append("variables", JSON.stringify({
  "input": {
    "name": "Mark Zuck Profile Picture",
    "userId": "VXNlcjoxMA==",
    "blobFieldName": "myBlobField"
  }
}));
// The file's key matches the value of the field `blobFieldName` in the variables
form.append("myBlobField", fs.createReadStream('./mark-zuckerberg.jpg'));
fetch("https://us-west-2.api.scaphold.io/graphql/scaphold-graphql", {
  method: 'POST',
  body: form
}).then(function(res) {
  return res.text();
}).then(function(body) {
  console.log(body);
});

它将 blob 字段添加到请求正文的位置。

基于此,我需要将 blobFieldName 属性传递到变量中,然后将传递给该属性的值与 blob 一起添加到请求正文中。但是,使用 Apollo I 无法添加到请求正文。我尝试了以下操作,但是当我签入网络检查器时,请求正文中没有它:

export const withCreateCoverPhoto = graphql(CreateCoverPhoto, {
  props: ({mutate}) => ({
    createCoverPhoto: (name, file) => mutate({
      variables: {
        input: {
          blobFieldName: name,
        },
      },
      [name]: file
    }),
  }),
});

请指教。

谢谢你的问题。目前,上传文件的最佳方法是[将其附加到FormData并使用fetch将其发送到服务器](https://medium.com/@danielbuechele/file-uploads-with-graphql-and-apollo-5502bbf3941e(。基本上,您将无法在此处缓存文件本身,但这是使用多部分请求在 Scaphold 上处理上传文件的最佳方式。

最新更新