Solved

Failed when trying to upload a file from Graphql

  • 2 October 2021
  • 9 replies
  • 790 views

Userlevel 1

Hello,

I'm trying to send a file to a card. Executing createPresignedUrl and PUT for S3 works perfectly, I can even access the download of the file that was sent.

My problem is with the updateFieldsValues mutation. When I send it I get an error, can you help me with that?

 

My mutation:


mutation {
updateFieldsValues(
input: {
nodeId: CARD-ID,
values: {
fieldId: "foto_do_cnpj_cpf",
value: ["uploads/7c3b7885-16d5-44c0-9e23-8570f261b20a/1633126573430.png"],
operation: ADD
}
}
)

{
clientMutationId,
updatedNode
}
}

 

The returned error:

 

{
"data": {
"updateFieldsValues": null
},
"errors": [
{
"message": "Invalid input: foto_do_cnpj_cpf ([\"uploads/7c3b7885-16d5-44c0-9e23-8570f261b20a/1633126573430.png\"])",
"locations": [
{
"line": 3,
"column": 7
}
],
"path": [
"updateFieldsValues"
],
"code": 30000,
"type": "PipefyRuntimeError"
}
]
}

 

Do you know what it can be? I've already checked the field id, it's correct

icon

Best answer by Lais Laudari 6 October 2021, 17:00

View original

9 replies

Userlevel 7
Badge +12

@dawid-szemborowski  reports a similar problem in

https://community.pipefy.com/api-76/createpresignedurl-returns-readablestream-1264

 

I do have a script where I got the upload working. The difference in the updateCardField query compared to yours is the includsion of the “orgs/...” part of the URL.

 

Below, I post my (PHP) function so you can compare it to yours, as I do not see the rest of your code.

 

<?php

use GraphQL\Client;
use GraphQL\Exception\ArgumentException;
use GraphQL\Exception\InvalidSelectionException;
use GraphQL\Exception\QueryError;
use GraphQL\Mutation;
use GraphQL\Query;
use GraphQL\RawObject;

// namespace, class, functions omitted

function uploadFile(string $filePath, string $fileName, int $cardId, string $fieldId, string $contentType)
{

$targetFileName = preg_replace("/[^a-zA-Z0-9_\-\.]+/", '_', $fileName);

// Fetch the URL to upload to
$mutation = (new Mutation('createPresignedUrl'))->setArguments(['input' => new RawObject('{
organizationId: "' . self::ORGANIZATION_ID . '",
fileName: "' . addslashes($targetFileName) . '",
contentType: "' . addslashes($contentType) . '"
}')])->setSelectionSet(['clientMutationId', 'url']);

// DO UPLOAD
$presignedUrlResults = $this->runGraphQLQuery($mutation);
$presignedUrl = $presignedUrlResults['createPresignedUrl']['url'];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $presignedUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');

$headers = [];
$headers[] = 'Content-Type: ' . $contentType;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
if (curl_errno($ch)) {
$this->logger->critical('Error on uploading file to Pipefy', [curl_error($ch)]);
// echo 'Error:' . curl_error($ch);
}
curl_close($ch);

$this->logger->info('Uploaded file', [$presignedUrl, $filePath, $response]);
$attachmentValue = ltrim(parse_url($presignedUrl, PHP_URL_PATH), '/');

// Add to card field
$addToCardMutation = (new Mutation('updateCardField'))->setArguments(['input' => new RawObject('{
card_id: "' . $cardId . '",
field_id: "' . $fieldId . '",
new_value: ["' . addslashes($attachmentValue) . '"]
}')])->setSelectionSet(['success', 'clientMutationId']);

$addToCardResults = $this->runGraphQLQuery($addToCardMutation);
$attachmentQueryResults = null;

if ($addToCardResults['updateCardField']['success']) {
$queryAttachmentURL = (new Query('card'))
->setArguments(['id' => $cardId])
->setSelectionSet([
(new Query('attachments'))->setSelectionSet([
'url', 'path', (new Query('field'))->setSelectionSet(['id', 'internal_id']),
]),
]);
$attachmentQueryResults = $this->runGraphQLQuery($queryAttachmentURL);
foreach ($attachmentQueryResults['card']['attachments'] as $key => $attachment) {
if (!$attachment['field']) {
$this->logger->warning('Attachment has no field', [$attachment]);
} else {
if ($attachment['field']['id'] == $fieldId) {
$this->logger->debug('Found attachment field', [$attachment]);

return $attachment['url'];
}
}
}
}
$this->logger->critical('Attachment not found after upload', [$addToCardResults, $attachmentQueryResults]);

return '';
}

 

Userlevel 7
Badge +9

Hi @avalizei, howa re you doing today?:slight_smile:
Could you check if the information in this link helps you, please? 

https://developers.pipefy.com/docs/how-to-upload-attachments-and-attach-to-a-card-record#sample-curl-commands

Let me know if you need anything else!:wink:

Userlevel 4

Hi @avalizei, howa re you doing today?:slight_smile:
Could you check if the information in this link helps you, please? 

https://developers.pipefy.com/docs/how-to-upload-attachments-and-attach-to-a-card-record#sample-curl-commands

Let me know if you need anything else!:wink:

 

Hi Lais! How are you? This is not working!

In the step that tells us to upload the file it gives us the code 200 and the file appears on the other card, but when I download it, it has 1kb and don’t opens.

Userlevel 7
Badge +8

Hi @avalizei, howa re you doing today?:slight_smile:
Could you check if the information in this link helps you, please? 

https://developers.pipefy.com/docs/how-to-upload-attachments-and-attach-to-a-card-record#sample-curl-commands

Let me know if you need anything else!:wink:

 

Hi Lais! How are you? This is not working!

In the step that tells us to upload the file it gives us the code 200 and the file appears on the other card, but when I download it, it has 1kb and don’t opens.

@lais-laudari can you please give us a extra help here? 

Userlevel 4

Hi @avalizei, howa re you doing today?:slight_smile:
Could you check if the information in this link helps you, please? 

https://developers.pipefy.com/docs/how-to-upload-attachments-and-attach-to-a-card-record#sample-curl-commands

Let me know if you need anything else!:wink:

 

Hi Lais! How are you? This is not working!

In the step that tells us to upload the file it gives us the code 200 and the file appears on the other card, but when I download it, it has 1kb and don’t opens.

@lais-laudari can you please give us a extra help here? 

Hi @lais-laudari and @Juliana Spinardi. I’ve managed to get it to work. I wasn’t sending the data to the upload step. The fact is that i’m using Integromat, a integrator web software. Anyway, its working now. Thanks for your reply.

Userlevel 1

Hello @tobiasbaco 

I see you decided the same problem using integromat.
Can you help me? How do you create step 2 from this documentation using Integromat?
I can’t understand how to create it.

Thanks

Userlevel 4

Hello @tobiasbaco 

I see you decided the same problem using integromat.
Can you help me? How do you create step 2 from this documentation using Integromat?
I can’t understand how to create it.

Thanks

Hi Kal! How are you? Here it is the modules. Just follow the doc ahead, of course using your variables. If need any help please contact me.

 

Userlevel 1

Hello @tobiasbaco 

I see you decided the same problem using integromat.
Can you help me? How do you create step 2 from this documentation using Integromat?
I can’t understand how to create it.

Thanks

I found solution.
Sorry, for distrub.

Userlevel 1

Hello @tobiasbaco 

I see you decided the same problem using integromat.
Can you help me? How do you create step 2 from this documentation using Integromat?
I can’t understand how to create it.

Thanks

Hi Kal! How are you? Here it is the modules. Just follow the doc ahead, of course using your variables. If need any help please contact me.

 

@tobiasbaco  
Sorry, I don’t see you answer.
Thanks a lot.
Have a nice day.

Reply