Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-15T16:49:07.683Z
Answers.LastActivityDate: 2022-08-15T16:49:07.683Z
Answers.UserDisplayName: a-am
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-07-15T10:05:50.940Z
Comments.Text: have you ever found a solution to this?
Comments.UserDisplayName: obs
Tags: <craft3><graphql>
Title: How do you create a GraphQL mutation to upload a file for an entry's asset field?
ThreadText: # 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.
  
  1. Create file input field & [process adding file](https://www.digitalocean.com/community/tutorials/js-file-reader).
  2. 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
  */
  
  ```
  
  2. On SET\_DOCUMENTS\_ASSET promise grab the returned ID of the Asset.
  3. 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.
  
  1. Allow user to remove file from the File Input.
  2. 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)
    }
  `;
  
  ```
  
  
  ---
  
  