emexrevolarter's avatar

GraphQL upload file returns error: Array to String

(updated) I have this schema:

scalar Upload @scalar(class: "Nuwave\Lighthouse\Schema\Types\Scalars\Upload")
extend type Mutation {
    createImageList(
        id: ID!
        category_id: String!
        image_path: [Upload!]!
        thumbnail: String!
        label: String
        description: String
        tags: JSON
        favorite_count: Int
    ): GalleryImageList @upsert

The React Client App:

const client = new ApolloClient({
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
  link: createUploadLink({
    uri: process.env.NX_API_ENDPOINT
  }),
  cache: new InMemoryCache(),
  cors: {
    origin: [originPath],
    credentials: true,
    "Access-Control-Allow-Origin" : true,
    methods: ['GET', 'POST'],
    contentType: "application/json",
    allowedHeaders: ['emexrevolarter']
  }
});

Client Mutation schema:

API_MUTATION = gql`
  mutation Entries($id: ID!,
    $category_id: String!,
    $image_path: [Upload!]!,
    $thumbnail: String!,
    $label: String,
    $description: String,
    $tags: JSON,
    $favorite_count: Int) {
    createImageList(
      id: $id,
      category_id: $category_id,
      image_path: $image_path,
      thumbnail: $thumbnail,
      label: $label,
      description: $description,
      tags: $tags,
      favorite_count: $favorite_count
    ){
      id
      category_id
      label
      description
      updated_at
    }
  }
`;

Sending the multi-part form data including multiple files as formData:

SendMutation({variables: {
      id: id,
      category_id: category,
      image_path: formData,
      thumbnail: getThumbnail,
      label: label,
      description: description,
      tags: getTags,
      favorite_count: 0
    }

I get the below error:

"debugMessage": "Array to string conversion",
            "message": "Internal server error"

How do I make uploading files to grapghql work please?

0 likes
5 replies
vincent15000's avatar

Are you sure that formData is a string ?

image_path: formData,

emexrevolarter's avatar

FormData returns an array of files:

[ { name: "FB_IMG_1624231832842.jpg" size: 44799 type: "image/jpeg" }, ... ]

emexrevolarter's avatar

My main headache is, why is it that in the Lighthouse Docs, @upload is listed and explained. However, trying to use it in the schema: @upload(disk: "public", path: "images/gallery", public: true)

returns: "debugMessage": "No directive found for upload"

The upload directive looks straight forward to use. Please, is this directive removed from Lighthouse? How can I use it?

Please or to participate in this conversation.