Managing Files
You can upload & delete a file and list files via API. For all the mutations and queries here you need to be logged in as warehouse worker.
File Uploading
To upload a file you need to run the cURL command. GraphQL playground doesn't support file uploading for now.
GraphQL Query of upload mutation:
mutation($file: Upload!) {
uploadFile(stockOwnerId: "id_str",file: $file) {
fileName,
presignedUrl
}
}
Uploading a specific file:
curl localhost:8000/graphql \
-H "Authorization: Bearer ..." \
-F operations='{ "query": "mutation($file: Upload!){ uploadFile(stockOwnerId: \"id_str\",file: $file) {fileName, presignedUrl} }", "variables": { "file": null } }' \
-F map='{ "file": ["variables.file"] }' \
-F file=@example.pdf
This command will upload the "example.pdf" file to the remote.
Deleting A file
To delete a file you can call deleteFile mutation with the fileName parameter.
mutation {
deleteFile(stockOwnerId: "id_str", fileName: "aaa.pdf")
}
Listing Files
You can use the following query to get a list of files for the current warehouse.
query {
listFiles(stockOwnerId:"id_str") {
key
}
}
Consult a File
You can use the following query to access a specific file.
query {
consultFile(stockOwnerId:"id_str", fileKey:"file_key) {
presignedUrl
}
}
Frontend Implementation
Example code for frontend implementation:
const [uploadedFileUrl, setUploadedFileUrl] = useState("");
const {mutate: uploadFileMutate, isLoading: uploadFileLoading} = useUploadFileMutation<Error>(
graphqlRequestClient,
{
onSuccess: (
data: UploadFileMutation,
_variables: UploadFileMutationVariables,
_context: any
) => {
if(data.uploadFile)
setUploadedFileUrl(data.uploadFile.presignedUrl);
showSuccess(t('messages:success-created'));
},
onError: () => {
showError(t('messages:error-creating-data'));
}
}
)
const uploadFile = async (options:any) => {
const { onSuccess, onError, file, onProgress } = options;
uploadFileMutate({stockOwnerId:"soid",file:file});
};
<Upload
customRequest={uploadFile}
itemRender={(originNode, file, currFileList) => <></>}
>
<Button icon={<UploadOutlined />}>Click to Upload</Button>
</Upload>
{uploadedFileUrl ? <Link href={uploadedFileUrl}>{uploadedFileUrl}</Link> : <></>}
{uploadFileLoading ? <ContentSpin /> : <></>}
```