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

Is there a way to use this with the new Apollo Federation 2 Supergraph? #344

Closed paulbijancoch closed 1 year ago

paulbijancoch commented 1 year ago

Hey everyone,

we started to implement the great graphql-upload package in our current project. But we wanted to make use of the new Apollo Federation 2 Supergraphs & Subgraphs. Does anyone have any experience with that or any ideas on how to implement it?

Best wishes,

Paul

dominik-myszkowski commented 1 year ago

I did use it with apollo server v3 and federation and it worked well. Unfortunately it seemed to stop working after upgrading the server to v4 (some issues with CSRF, I am still investigating). I consider dumping graphql-upload and switching to REST upload as this package no longer fills my needs and also stopped supporting CommonJS imports (require) from v16 onwards. Anyway if You are interested, there are few things you should do:

  1. On gateway side use that package: `const FileUploadDataSource = require('@profusion/apollo-federation-upload').default;

const gateway = new ApolloGateway({ // supergraphSdl: sdlContent, supergraphSdl: new IntrospectAndCompose({ subgraphs: serviceList, }), buildService({ name, url }) { return new FileUploadDataSource({ url, useChunkedTransfer: true, }); }, }); ......... const server = new ApolloServer({ gateway, includeStacktraceInErrorResponses: true, }); `

  1. On your subgraph backend side use graphql-upload

`const { graphqlUploadExpress } = require('graphql-upload');

app.use(graphqlUploadExpress());`

  1. On client side:

`import { createUploadLink } from 'apollo-upload-client';

const uploadLink = createUploadLink({ uri: ${process.env.REACT_APP_BACKEND_URL}/graphql, // credentials: 'include', });

const client = new ApolloClient({ // credentials: 'include', cache: new InMemoryCache({ addTypename: false, }), link: ApolloLink.from([some_other_links, uploadLink]), }); `

I hope it was what you were asking for.

jaydenseric commented 1 year ago

Closing because this is a question better asked of Apollo, since it concerns their products and services. Anyone knowledgeable about the topic is still welcome to answer the original question here though and share their insights.

paulbijancoch commented 1 year ago

hey @dominik-myszkowski and thank you very much for your solution. Did you use that with typescript? because when I do so, i get an error, that the graphql-upload's package.json does not have any "exports" main defined. I'd be thankful for any thought on this.

best, Paul

jaydenseric commented 1 year ago

@paulbijancoch see the documented requirements for TypeScript:

https://github.com/jaydenseric/graphql-upload/tree/v16.0.2#requirements

Also, make sure you are deep importing as the exports are documented:

https://github.com/jaydenseric/graphql-upload/tree/v16.0.2#exports

paulbijancoch commented 1 year ago

@jaydenseric unfortunately this is meant to be in the @profusion/apollo-federation-upload package. I already mentioned my bug in their repository, see https://github.com/profusion/apollo-federation-file-upload/issues/62. But thank you for your kind advice