<p>I have found a solution to using GQL to upload a file for an Entry's Asset Field.</p>
<ol>
<li>Create file input field &amp; <a href="https://www.digitalocean.com/community/tutorials/js-file-reader" rel="nofollow noreferrer">process adding file</a>.</li>
<li>When file is added to file input field, save the asset to the assets volume using:</li>
</ol>
<pre><code>export const SET_DOCUMENTS_ASSET = gql`
  mutation($document: FileInput) {
    save_documents_Asset(_file: $document, newFolderId: 5) {
      id
      filename
      title
    }
  }
`;

/* 
  FileInput is expecting an object as { fileName:&quot;&quot;, id:NaN }
  When saving file for first time leave id:NaN when resaving pass asset id
*/
</code></pre>
<ol start="2">
<li>On SET_DOCUMENTS_ASSET promise grab the returned ID of the Asset.</li>
<li>When saving the Form use that asset(s) ID(s) to set the Entry's Asset(s)</li>
</ol>
<pre><code>export const SET_ENTRY = gql`
  mutation (
    $document: [Int]
  ) {
    save_section_entrytype_Entry(
      document: $document
    ) {
      id
      title
      document {
        id
        url
        title
      }
    }
  }
`;
</code></pre>
<p>If the user needs to select a different file, here are steps too removed saved asset.</p>
<ol>
<li>Allow user to remove file from the File Input.</li>
<li>On removal of file delete the asset from the Volume passing the ID of the asset you originally saved:</li>
</ol>
<pre><code>export const DELETE_ASSET = gql`
  mutation($id: Int!){
    deleteAsset(id:$id)
  }
`;
</code></pre>

CC BY-SA 4.0
2022-08-15T16:49:07.683Z
2022-08-15T16:49:07.683Z
a-am
<p>In my mutation I need to upload a file to an Asset Field. Can this all be done in one mutation or do you need to upload the file in one mutation and assign the id to the asset field in a secondary mutation?</p>
<pre><code>mutation($document:FileInput) {
   save_sectionHandle_entryType_Entry(document:$document) {
      document {
          id
          title
          url
      }
   }
}

// Params
{
    document: { fileData: Base64 file data, filename: String }
}
</code></pre>

CC BY-SA 4.0
2022-07-15T10:05:50.940Z
have you ever found a solution to this?
obs
<craft3><graphql>
How do you create a GraphQL mutation to upload a file for an entry's asset field?
# How do you create a GraphQL mutation to upload a file for an entry's asset field?

(Asked by: username_0 on 2021-12-28)

In my mutation I need to upload a file to an Asset Field. Can this all be done in one mutation or do you need to upload the file in one mutation and assign the id to the asset field in a secondary mutation?
mutation($document:FileInput) {
   save_sectionHandle_entryType_Entry(document:$document) {
      document {
          id
          title
          url
      }
   }
}

// Params
{
    document: { fileData: Base64 file data, filename: String }
}

**Tags:** craft3, graphql
> **Comments:**
> **username_1**: have you ever found a solution to this?  

---

## 1 answers

### Answer by username_0 on 2022-08-15. Score: 0 | Accepted answer

I have found a solution to using GQL to upload a file for an Entry's Asset Field.

Create file input field & process adding file.
When file is added to file input field, save the asset to the assets volume using:

export const SET_DOCUMENTS_ASSET = gql`
  mutation($document: FileInput) {
    save_documents_Asset(_file: $document, newFolderId: 5) {
      id
      filename
      title
    }
  }
`;

/* 
  FileInput is expecting an object as { fileName:"", id:NaN }
  When saving file for first time leave id:NaN when resaving pass asset id
*/


On SET_DOCUMENTS_ASSET promise grab the returned ID of the Asset.
When saving the Form use that asset(s) ID(s) to set the Entry's Asset(s)

export const SET_ENTRY = gql`
  mutation (
    $document: [Int]
  ) {
    save_section_entrytype_Entry(
      document: $document
    ) {
      id
      title
      document {
        id
        url
        title
      }
    }
  }
`;

If the user needs to select a different file, here are steps too removed saved asset.

Allow user to remove file from the File Input.
On removal of file delete the asset from the Volume passing the ID of the asset you originally saved:

export const DELETE_ASSET = gql`
  mutation($id: Int!){
    deleteAsset(id:$id)
  }
`;


---

