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

WIP: File Uploads with Federation #2334

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { gateway, DataSource } from '../gateway/gateway'
import { yoga as service1 } from '../service/yoga'
import { buildService, gateway } from '../gateway/src/gateway'
import { yoga as service1 } from '../service/src/yoga'
import { createServer, Server } from 'http'
import { AddressInfo } from 'net'
import { fetch } from '@whatwg-node/fetch'
import type { GatewayConfig } from '@apollo/gateway'
import { fetch, File, FormData } from '@whatwg-node/fetch'

describe('apollo-federation example integration', () => {
let gatewayServer: Server
Expand All @@ -16,19 +15,12 @@ describe('apollo-federation example integration', () => {
await new Promise<void>((resolve) => serviceServer.listen(0, resolve))
servicePort = (serviceServer.address() as AddressInfo).port

const gatewayConfig: GatewayConfig = {
const gatewayService = await gateway({
serviceList: [
{ name: 'accounts', url: `http://localhost:${servicePort}/graphql` },
],
introspectionHeaders: {
accept: 'application/json',
},
buildService({ url }) {
return new DataSource({ url })
},
}

const gatewayService = await gateway(gatewayConfig)
buildService,
})
gatewayServer = createServer(gatewayService)
await new Promise<void>((resolve) => gatewayServer.listen(0, resolve))
gatewayPort = (gatewayServer.address() as AddressInfo).port
Expand All @@ -51,4 +43,37 @@ describe('apollo-federation example integration', () => {
},
})
})
it('should forward file uploads', async () => {
const formData = new FormData()
formData.append(
'operations',
JSON.stringify({
query: 'query($file: File!){readTextFile(file: $file)}',
variables: {
file: null,
},
}),
)
formData.append(
'map',
JSON.stringify({
0: ['variables.file'],
}),
)
formData.append(
'0',
new File(['test'], 'test.txt', {
type: 'text/plain',
}),
)
const response = await fetch(`http://localhost:${gatewayPort}/graphql`, {
method: 'POST',
body: formData,
})
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data).toEqual({
readTextFile: 'test',
})
})
})
34 changes: 0 additions & 34 deletions examples/apollo-federation/gateway/gateway.js

This file was deleted.

3 changes: 2 additions & 1 deletion examples/apollo-federation/gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"version": "1.3.1",
"private": true,
"scripts": {
"start": "node index.js",
"start": "ts-node src/index.ts",
"check": "exit 0"
},
"dependencies": {
"@apollo/gateway": "^2.0.0",
"@envelop/apollo-federation": "3.0.4",
"@graphql-tools/executor-http": "^0.1.1",
"graphql-yoga": "3.3.1",
"graphql": "^16.5.0"
}
Expand Down
56 changes: 56 additions & 0 deletions examples/apollo-federation/gateway/src/gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
ApolloGateway,
GraphQLDataSource,
ServiceEndpointDefinition,
} from '@apollo/gateway'
import { createYoga, isAsyncIterable } from 'graphql-yoga'
import { useApolloFederation } from '@envelop/apollo-federation'
import { GatewayConfig } from '@apollo/gateway'
import { buildHTTPExecutor } from '@graphql-tools/executor-http'
import { parse } from 'graphql'

export function buildService(
opts: ServiceEndpointDefinition,
): GraphQLDataSource {
const executor = buildHTTPExecutor({
endpoint: opts.url,
})
return {
async process(opts) {
const result = await executor({
document: parse(opts.request.query),
operationName: opts.request.operationName,
variables: opts.request.variables,
context: opts.context,
extensions: {
endpoint: opts.request.http?.url,
method: opts.request.http?.method as 'POST',
headers: opts.request.http?.headers as any,
...opts.request.extensions,
},
})
if (isAsyncIterable(result)) {
throw new Error('Async Iterables are not supported')
}
return result
},
}
}

export async function gateway(config?: GatewayConfig) {
// Initialize the gateway
const gateway = new ApolloGateway(config)

// Make sure all services are loaded
await gateway.load()

const yoga = createYoga({
plugins: [
useApolloFederation({
gateway,
}),
],
})

return yoga
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
/* eslint-disable */
const { createServer } = require('http')
const { gateway, DataSource } = require('./gateway')
import { createServer } from 'http'
import { buildService, gateway } from './gateway'

async function main() {
const yoga = gateway({
const yoga = await gateway({
serviceList: [
{ name: 'accounts', url: 'http://localhost:4001/graphql' },
// ...additional subgraphs...
],
buildService({ url }) {
return new DataSource({ url })
},
buildService,
})

// Start the server and explore http://localhost:4000/graphql
Expand Down
4 changes: 3 additions & 1 deletion examples/apollo-federation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"check": "exit 0"
},
"dependencies": {
"concurrently": "^7.0.0"
"concurrently": "^7.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"workspaces": [
"service",
Expand Down
2 changes: 1 addition & 1 deletion examples/apollo-federation/service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.3.1",
"private": true,
"scripts": {
"start": "node index.js",
"start": "ts-node src/index.ts",
"check": "exit 0"
},
"dependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable */
const { createServer } = require('http')
const { yoga } = require('./yoga')
import { createServer } from 'http'
import { yoga } from './yoga'

const server = createServer(yoga)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-disable */
const { parse } = require('graphql')
const { buildSubgraphSchema } = require('@apollo/subgraph')
const { createYoga } = require('graphql-yoga')
import { parse } from 'graphql'
import { buildSubgraphSchema } from '@apollo/subgraph'
import { createYoga } from 'graphql-yoga'

const typeDefs = parse(/* GraphQL */ `
scalar File

type Query {
me: User
readTextFile(file: File!): String
}

type User @key(fields: "id") {
Expand All @@ -19,6 +21,9 @@ const resolvers = {
me() {
return { id: '1', username: '@ava' }
},
readTextFile(_, { file }) {
return file.text()
},
},
User: {
__resolveReference(user, { fetchUserById }) {
Expand Down
10 changes: 10 additions & 0 deletions examples/apollo-federation/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"skipLibCheck": true,
"target": "esnext",
"moduleResolution": "node",
"module": "commonjs",
"sourceMap": true,
"lib": ["esnext"]
}
}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.