Curl File Upload

File Upload Using GraphQL and curl

Overview

This guide explains how to upload a file using a GraphQL mutation via curl. You will need to structure your curl request to include the GraphQL query, the file mapping, and the file itself in the form-data.

Steps to Perform a File Upload

  1. Set Up the GraphQL Mutation
    Your GraphQL mutation should look like this:

    mutation singleUpload($file: Upload!) {
         singleUpload(
           file: $file,
           entity_id: "your_entity_id", // or representative_id
           file_enum: front_drivers_license
         ) {
           filename
         }
    }
    
  2. Prepare the operations Parameter
    The operations parameter includes the GraphQL query and the variables. It should be a JSON string. Here’s an example:

    {
         "query": "
           mutation singleUpload($file: Upload!) {
             singleUpload(
               file: $file,
               entity_id: \"your_entity_id\", // or representative_id
               file_enum: front_drivers_license
             ) {
               filename
             }
           }
         ",
         "variables": {
           "file": null
         }
    }
    
  3. Prepare the map Parameter
    The map parameter maps the file in the form data to the GraphQL variable. It should be a JSON string. Here’s an example:

    {
         "0": ["variables.file"]
    }
    
  4. Include the File in the Form-Data
    The file should be included in the form-data with a key that matches the mapping in the map parameter. Here’s how you can reference the file in the form-data:

    0=@/path/to/your/file.txt
    
  5. Construct the curl Command
    Combine all the above elements into a single curl command. Replace placeholders with actual values:

    curl -X POST https://your-graphql-endpoint.com/graphql \
         -H "apollo-require-preflight: true" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -F 'operations={
           "query": "
             mutation singleUpload($file: Upload!) {
               singleUpload(
                 file: $file,
                 entity_id: \"your_entity_id\", // or representative_id
                 file_enum: front_drivers_license
               ) {
                 filename
               }
             }
           ",
           "variables": {
             "file": null
           }
         }' \
         -F 'map={
           "0": ["variables.file"]
         }' \
         -F "0=@/path/to/your/file.txt"
    

Example Explanation

Example Command

Here's an example curl command with placeholders replaced:

curl -X POST https://api.example.com/graphql \
  -H "apollo-require-preflight: true" \
  -H "Authorization: Bearer your-access-token" \
  -F 'operations={
    "query": "
      mutation singleUpload($file: Upload!) {
        singleUpload(
          file: $file,
          entity_id: \"12345\", // or representative_id
          file_enum: front_drivers_license
        ) {
          filename
        }
      }
    ",
    "variables": {
      "file": null
    }
  }' \
  -F 'map={
    "0": ["variables.file"]
  }' \
  -F "0=@/path/to/your/file.txt"

Notes

By following these steps, you can successfully upload a file using a GraphQL mutation with curl. If you encounter any issues, check the structure of your operations and map parameters, and ensure your file path is correct.