Uploading Error to a GraphQL API

I am trying to upload a file to a GraphQL API which is built on top of Phoenix.

Following the example here:

https://hexdocs.pm/absinthe/file-uploads.html#content

Whatever I do, I get something like this back from the API:

{"errors":[{"message":"syntax error before: ","locations":[{"line":1,"column":0}]}]}

So, somehow Absinthe returns this as an error, but I am unsure what is going wrong…

Something in either Phoenix, Absinthe or Absinthe’s Plug.

I do not upload JSON, so I doubt it is trying and failing to parse.

Anybody care to give some suggestions or recommendations?

As always when asking a question, please show what you’re doing to create the error.

3 Likes

Ah yes, I overestimated your psychic powers : )

I must be missing something simple…

Here is how our setup looks.

Thanks!

Schema

import_types Absinthe.Plug.Types

...

@desc "Test Upload a File"
field :test_upload, :string do 
  arg(:file, non_null(:upload))

  resolve(fn args, _ ->
    {:ok, "success"}
  end)
end

Router

pipeline :graphql do
  plug(Api.Web.Context)
end

# non authentication queries go through here
scope "/" do
  pipe_through(:graphql)
  post("/", Absinthe.Plug, schema: Api.Gql.Schemas.Api)
end

Endpoint

plug(
  Plug.Parsers,
  parsers: [:urlencoded, :multipart, :json, Absinthe.Plug.Parser],
  pass: ["*/*"],
  json_decoder: Poison
  )

Curl Request

curl -X POST -H "authorization : Bearer $API_TOKEN" -F query="{mutation testUpload(file: \"metadata_json\")" -F metadata_json=@package-lock.json trunk:4000/

Error Message

{"errors":[{"message":"syntax error before: ","locations":[{"line":1,"column":0}]}]}

Your query isn’t valid GraphQL syntax. Also, you name the mutation field testUpload but call uploadTest. Try mutation { testUpload(file: \"metadata_json\") } for your query.

Edit: It looks like you have copied your code from the Absinthe docs, but the syntax there is wrong.

1 Like

Nailed it!

Many thanks @zimt28. The basic problem is the query in the docs is wrong.

(Perhaps the even more basic option, is the error isn’t very explanatory…)

(Also, I mistyped the mutation ‘testUpload’ vs ‘uploadTest’ also, well spotted)