jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.42k stars 131 forks source link

Error when testing v13 using apollo-server-express #349

Closed garrettg123 closed 1 year ago

garrettg123 commented 1 year ago

I have uploading working, however I can't get a test working. When using apollo-server, the error is:

Variable "$file" got invalid value {}; Upload value invalid.

But when using apollo-server-express, the query must be a string, which isn't available to me after using graphql-codegen, and I can't use the DocumentNode which is generated.

Here's the test code:

    const file = Promise.resolve({
      createReadStream: () => stream,
      stream,
      filename,
      mimetype: 'text/plain',
      encoding: 'utf-8',
    })
await server.executeOperation({
        query: UploadDocumentFileDocument,
        variables: {
          file,
          documentId,
          fileId,
        },
      })
export const UploadDocumentFileDocument = ({
  kind: 'Document',
  definitions: [
    {
      kind: 'OperationDefinition',
      operation: 'mutation',
      name: { kind: 'Name', value: 'UploadDocumentFile' },
      variableDefinitions: [
        {
          kind: 'VariableDefinition',
          variable: { kind: 'Variable', name: { kind: 'Name', value: 'file' } },
          type: {
            kind: 'NonNullType',
            type: {
              kind: 'NamedType',
              name: { kind: 'Name', value: 'Upload' },
            },
          },
        },
        {
          kind: 'VariableDefinition',
          variable: {
            kind: 'Variable',
            name: { kind: 'Name', value: 'documentId' },
          },
          type: {
            kind: 'NonNullType',
            type: {
              kind: 'NamedType',
              name: { kind: 'Name', value: 'String' },
            },
          },
        },
        {
          kind: 'VariableDefinition',
          variable: {
            kind: 'Variable',
            name: { kind: 'Name', value: 'fileId' },
          },
          type: {
            kind: 'NonNullType',
            type: {
              kind: 'NamedType',
              name: { kind: 'Name', value: 'String' },
            },
          },
        },
      ],
      selectionSet: {
        kind: 'SelectionSet',
        selections: [
          {
            kind: 'Field',
            name: { kind: 'Name', value: 'uploadDocumentFile' },
            arguments: [
              {
                kind: 'Argument',
                name: { kind: 'Name', value: 'file' },
                value: {
                  kind: 'Variable',
                  name: { kind: 'Name', value: 'file' },
                },
              },
              {
                kind: 'Argument',
                name: { kind: 'Name', value: 'documentId' },
                value: {
                  kind: 'Variable',
                  name: { kind: 'Name', value: 'documentId' },
                },
              },
              {
                kind: 'Argument',
                name: { kind: 'Name', value: 'fileId' },
                value: {
                  kind: 'Variable',
                  name: { kind: 'Name', value: 'fileId' },
                },
              },
            ],
            selectionSet: {
              kind: 'SelectionSet',
              selections: [
                {
                  kind: 'FragmentSpread',
                  name: { kind: 'Name', value: 'DocumentFileFragment' },
                },
              ],
            },
          },
        ],
      },
    },
    {
      kind: 'FragmentDefinition',
      name: { kind: 'Name', value: 'DocumentFileFragment' },
      typeCondition: {
        kind: 'NamedType',
        name: { kind: 'Name', value: 'DocumentFile' },
      },
      selectionSet: {
        kind: 'SelectionSet',
        selections: [
          { kind: 'Field', name: { kind: 'Name', value: 'id' } },
          { kind: 'Field', name: { kind: 'Name', value: 'createdAt' } },
          { kind: 'Field', name: { kind: 'Name', value: 'updatedAt' } },
          { kind: 'Field', name: { kind: 'Name', value: 'documentId' } },
          { kind: 'Field', name: { kind: 'Name', value: 'fileId' } },
          { kind: 'Field', name: { kind: 'Name', value: 'filename' } },
          { kind: 'Field', name: { kind: 'Name', value: 'mimetype' } },
          { kind: 'Field', name: { kind: 'Name', value: 'encoding' } },
          { kind: 'Field', name: { kind: 'Name', value: 'url' } },
        ],
      },
    },
  ],
} as unknown) as DocumentNode
jaydenseric commented 1 year ago

Closing because this isn't an issue with graphql-upload and I don't have availability right now to assist with usage questions or debugging users project code. If anyone would like to help out by answering this question feel free!

Over the next few weeks and months I'm going to be setting up tests for parts of a GraphQL schema at work that utilise file uploads so if I notice anything about that that could be smoothed over by additions to documentation (such as code examples) or utility functions that help with testing, I will revisit questions like this.