Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Router only supports uploading a file if the file field name is "file" #1487

Open
l-petrov opened this issue Jan 5, 2025 · 2 comments
Open
Labels
bug Something isn't working internally-reviewed The issue has been reviewed internally.

Comments

@l-petrov
Copy link
Contributor

l-petrov commented Jan 5, 2025

Component(s)

router

Component version

0.158.0

wgc version

0.71.3

controlplane version

None

router version

0.158.0

What happened?

If possible, please create a PR with a failing test to illustrate the issue clearly.
Otherwise, please attach a minimum reproduction through a GitHub repository that includes
essential information such as the relevant subgraph SDLs.
Please also make sure that the instructions for the reproduction are clear, tested, and fully accurate.

Description

When attempting to upload a file through the Router, the name of the field in the schema must be file, as illustrated in the example provided:
https://cosmo-docs.wundergraph.com/router/file-upload
Any other name will result in the router passing null as the file data to the final federated GraphQL server.

This is because the httpclient package, which is part of the graphql-go-tools repository, has a hardcoded name for the file variables, which is file, as evident in the nethttpclient.go file.

func DoMultipartForm(
	client *http.Client, ctx context.Context, requestInput []byte, files []File, out *bytes.Buffer,
) (err error) {
	if len(files) == 0 {
		return errors.New("no files provided")
	}

	url, method, body, headers, queryParams, enableTrace := requestInputParams(requestInput)

	h := pool.Hash64.Get()
	defer pool.Hash64.Put(h)
	_, _ = h.Write(body)

	formValues := map[string]io.Reader{
		"operations": bytes.NewReader(body),
	}

	var fileMap string
	var tempFiles []*os.File
	for i, file := range files {
		if len(fileMap) == 0 {
			if len(files) == 1 {
				fileMap = fmt.Sprintf(`"%d" : ["variables.file"]`, i)
			} else {
				fileMap = fmt.Sprintf(`"%d" : ["variables.files.%d"]`, i, i)
			}
		} else {
			fileMap = fmt.Sprintf(`%s, "%d" : ["variables.files.%d"]`, fileMap, i, i)
		}
		key := fmt.Sprintf("%d", i)
		_, _ = h.WriteString(file.Path())
		temporaryFile, err := os.Open(file.Path())
		tempFiles = append(tempFiles, temporaryFile)
		if err != nil {
			return err
		}
		formValues[key] = bufio.NewReader(temporaryFile)
	}
....

Steps to Reproduce

  1. Create a simple WunderGraph router that is federating a GraphQL server with a schema that has a query with a file upload query. Make sure the field of the file data is not named file
  2. Try to upload a file to print the contents of the file at the GraphQL server as it was passed by WunderGraph router.

Expected Result

If this is by design it should be stated explicitly in the docs that you can upload files only using fields that are named file.
Other wise the expected result is that the file upload should work with any field name.

Actual Result

The destination GraphQL server receives a null file

Environment information

Environment

OS: MacOS 15.2
Package Manager: npm
Compiler(if manually compiled): (e.g., "go 14.2")

Router configuration

No response

Router execution config

No response

Log output

No response

Additional context

No response

@l-petrov l-petrov added the bug Something isn't working label Jan 5, 2025
Copy link

github-actions bot commented Jan 5, 2025

WunderGraph commits fully to Open Source and we want to make sure that we can help you as fast as possible.
The roadmap is driven by our customers and we have to prioritize issues that are important to them.
You can influence the priority by becoming a customer. Please contact us here.

@jensneuse jensneuse added the internally-reviewed The issue has been reviewed internally. label Jan 7, 2025
@jensneuse
Copy link
Member

Thanks for bringing this up. We'll take a look at improving the docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working internally-reviewed The issue has been reviewed internally.
Projects
None yet
Development

No branches or pull requests

2 participants