Skip to content

Commit

Permalink
Update graphql-yoga to latest v3 and follow three tag (#4560)
Browse files Browse the repository at this point in the history
* Update graphql-yoga to latest v3 and follow `three` tag

* Update yarn.lock

* Use Yoga v3 also in the examples

* Go

* Go

* Go

* ..

* chore(dependencies): updated changesets for modified dependencies

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] committed Sep 21, 2022
1 parent d44bf60 commit d08ed0e
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 225 deletions.
7 changes: 7 additions & 0 deletions .changeset/@graphql-mesh_http-4560-dependencies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@graphql-mesh/http": patch
---

dependencies updates:

- Updated dependency [`graphql-yoga@3.0.0-next.0` ↗︎](https://www.npmjs.com/package/graphql-yoga/v/3.0.0-next.0) (from `3.0.0-alpha-20220917000718-4285245d`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/olive-pets-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@omnigraph/json-schema': patch
---

Support non-native File or Blob instances
2 changes: 1 addition & 1 deletion examples/graphql-file-upload-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"@graphql-mesh/cli": "0.78.22",
"@graphql-mesh/graphql": "0.31.15",
"concurrently": "5.3.0",
"@graphql-yoga/node": "2.13.13",
"graphql-yoga": "3.0.0-next.0",
"sharp": "0.31.0",
"graphql": "16.6.0"
}
Expand Down
10 changes: 5 additions & 5 deletions examples/graphql-file-upload-example/resize-image/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('./server')
.start()
.then(() => {
console.info(`ResizeImage GraphQL API listening on 3002`);
});
const startServer = require('./server');

startServer().then(() => {
console.info(`ResizeImage GraphQL API listening on 3002`);
});
56 changes: 34 additions & 22 deletions examples/graphql-file-upload-example/resize-image/server.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
const { createServer } = require('@graphql-yoga/node');
const { createYoga, createSchema } = require('graphql-yoga');
const { createServer } = require('http');
const sharp = require('sharp');

module.exports = createServer({
schema: {
typeDefs: /* GraphQL */ `
type Query {
resizeImage(image: String, width: Int, height: Int): String
}
`,
resolvers: {
Query: {
resizeImage: async (_, { image, width, height }) => {
const inputBuffer = Buffer.from(image, 'base64');
const buffer = await sharp(inputBuffer).resize(width, height).toBuffer();
const base64 = buffer.toString('base64');
return base64;
module.exports = function startServer() {
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
resizeImage(image: String, width: Int, height: Int): String
}
`,
resolvers: {
Query: {
resizeImage: async (_, { image, width, height }) => {
const inputBuffer = Buffer.from(image, 'base64');
const buffer = await sharp(inputBuffer).resize(width, height).toBuffer();
const base64 = buffer.toString('base64');
return base64;
},
},
},
},
},
maskedErrors: false,
logging: false,
hostname: 'localhost',
port: 3002,
});
}),
maskedErrors: false,
logging: false,
});
const server = createServer(yoga);
return new Promise(resolve => {
server.listen(3002, () => {
resolve(
() =>
new Promise(resolve => {
server.close(resolve);
})
);
});
});
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const uploadFilesServer = require('../upload-files/server');
const resizeImageServer = require('../resize-image/server');
const startUploadFilesServer = require('../upload-files/server');
const startResizeImageServer = require('../resize-image/server');
const { File } = require('@whatwg-node/fetch');
const { findAndParseConfig } = require('@graphql-mesh/cli');
const { join } = require('path');
Expand All @@ -10,8 +10,19 @@ const mesh$ = findAndParseConfig({
}).then(config => getMesh(config));

describe('Upload Example', () => {
beforeAll(() => Promise.all([uploadFilesServer.start(), resizeImageServer.start()]));
afterAll(() => Promise.all([uploadFilesServer.stop(), resizeImageServer.stop(), mesh$.then(mesh => mesh.destroy())]));
let stopUploadFilesServer;
let stopResizeImageServer;
beforeAll(() => {
return Promise.all([
startUploadFilesServer().then(stop => {
stopUploadFilesServer = stop;
}),
startResizeImageServer().then(stop => {
stopResizeImageServer = stop;
}),
]);
});
afterAll(() => Promise.all([stopUploadFilesServer(), stopResizeImageServer(), mesh$.then(mesh => mesh.destroy())]));
it('should give correct response', async () => {
const { execute } = await mesh$;
const file = new File(['CONTENT'], 'test.txt');
Expand Down
10 changes: 5 additions & 5 deletions examples/graphql-file-upload-example/upload-files/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require('./server')
.start()
.then(() => {
console.info(`UploadFiles GraphQL API listening on 3001`);
});
const startServer = require('./server');

startServer().then(() => {
console.info(`UploadFiles GraphQL API listening on 3001`);
});
103 changes: 58 additions & 45 deletions examples/graphql-file-upload-example/upload-files/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { createServer } = require('@graphql-yoga/node');
const { createYoga, createSchema } = require('graphql-yoga');
const { createServer } = require('http');
const { existsSync, mkdirSync } = require('fs');
const {
promises: { readdir, readFile, writeFile, unlink },
Expand All @@ -10,50 +11,62 @@ if (!existsSync(FILES_DIR)) {
mkdirSync(FILES_DIR);
}

module.exports = createServer({
schema: {
typeDefs: /* GraphQL */ `
scalar File
type Query {
files: [FileResult]
}
type Mutation {
uploadFile(upload: File!): FileResult!
deleteFile(filename: String): Boolean
}
type FileResult {
filename: String
base64: String
}
`,
resolvers: {
Query: {
files: () => readdir(FILES_DIR).then(files => files.map(filename => ({ filename }))),
},
Mutation: {
uploadFile: async (_, { upload }) => {
const filename = upload.name;
const arrayBuffer = await upload.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
try {
await writeFile(join(FILES_DIR, filename), buffer);
} catch (e) {
console.error(`Error writing ${filename}`, e);
}
return { filename };
module.exports = function startServer() {
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
scalar File
type Query {
files: [FileResult]
}
type Mutation {
uploadFile(upload: File!): FileResult!
deleteFile(filename: String): Boolean
}
type FileResult {
filename: String
base64: String
}
`,
resolvers: {
Query: {
files: () => readdir(FILES_DIR).then(files => files.map(filename => ({ filename }))),
},
deleteFile: async (_, { filename }) => {
await unlink(join(FILES_DIR, filename));
return true;
Mutation: {
uploadFile: async (_, { upload }) => {
const filename = upload.name;
const arrayBuffer = await upload.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
try {
await writeFile(join(FILES_DIR, filename), buffer);
} catch (e) {
console.error(`Error writing ${filename}`, e);
}
return { filename };
},
deleteFile: async (_, { filename }) => {
await unlink(join(FILES_DIR, filename));
return true;
},
},
File: {
base64: ({ filename }) => readFile(join(FILES_DIR, filename), 'base64'),
},
},
File: {
base64: ({ filename }) => readFile(join(FILES_DIR, filename), 'base64'),
},
},
},
hostname: 'localhost',
port: 3001,
maskedErrors: false,
logging: false,
});
}),
maskedErrors: false,
logging: false,
});

const server = createServer(yoga);
return new Promise(resolve => {
server.listen(3001, () => {
resolve(
() =>
new Promise(resolve => {
server.close(resolve);
})
);
});
});
};
2 changes: 1 addition & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@graphql-mesh/runtime": "0.44.11",
"@graphql-mesh/utils": "0.41.13",
"@graphql-mesh/types": "0.84.2",
"graphql-yoga": "3.0.0-alpha-20220917000718-4285245d",
"graphql-yoga": "3.0.0-next.0",
"itty-router": "2.6.3",
"itty-router-extras": "0.4.2",
"tslib": "^2.4.0"
Expand Down
29 changes: 20 additions & 9 deletions packages/loaders/json-schema/src/addExecutionLogicToComposer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import lodashSet from 'lodash.set';
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
import { process } from '@graphql-mesh/cross-helpers';
import { getHeadersObj } from '@graphql-mesh/utils';
import { FormData } from '@whatwg-node/fetch';
import { Blob, File, FormData } from '@whatwg-node/fetch';

export interface AddExecutionLogicToComposerOptions {
schemaComposer: SchemaComposer;
Expand Down Expand Up @@ -225,18 +225,29 @@ ${operationConfig.description || ''}
delete headers['Content-Type'];
const formData = new FormData();
for (const key in input) {
let formDataValue: Blob | string;
const inputValue = input[key];
if (typeof inputValue === 'object') {
if (inputValue.toString() === '[object Blob]' || inputValue.toString() === '[object File]') {
formDataValue = inputValue;
if (inputValue != null) {
let formDataValue: Blob | string;
if (typeof inputValue === 'object') {
if (inputValue instanceof File) {
formDataValue = inputValue;
} else if (inputValue.name && inputValue instanceof Blob) {
formDataValue = new File([inputValue], (inputValue as File).name, { type: inputValue.type });
} else if (inputValue.arrayBuffer) {
const arrayBuffer = await inputValue.arrayBuffer();
if (inputValue.name) {
formDataValue = new File([arrayBuffer], inputValue.name, { type: inputValue.type });
} else {
formDataValue = new Blob([arrayBuffer], { type: inputValue.type });
}
} else {
formDataValue = JSON.stringify(inputValue);
}
} else {
formDataValue = JSON.stringify(inputValue);
formDataValue = inputValue.toString();
}
} else {
formDataValue = inputValue.toString();
formData.append(key, formDataValue);
}
formData.append(key, formDataValue);
}
requestInit.body = formData;
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/openapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
},
"devDependencies": {
"@graphql-tools/utils": "8.12.0",
"@graphql-yoga/node": "2.13.13",
"graphql-yoga": "3.0.0-next.0",
"@types/cookie-parser": "1.4.3",
"@types/multer": "1.4.7",
"@whatwg-node/fetch": "0.4.4",
Expand Down
33 changes: 18 additions & 15 deletions packages/loaders/openapi/tests/example_api7.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,51 @@
/* eslint-disable import/no-nodejs-modules */
/* eslint-disable no-unreachable-loop */
import { createServer, YogaNodeServerInstance } from '@graphql-yoga/node';
import { createYoga } from 'graphql-yoga';
import { createServer, Server } from 'http';
import { AbortController, fetch } from '@whatwg-node/fetch';
import { GraphQLSchema } from 'graphql';

import { loadGraphQLSchemaFromOpenAPI } from '../src/loadGraphQLSchemaFromOpenAPI';
import { startServer, stopServer, pubsub } from './example_api7_server';
import getPort from 'get-port';
import { AddressInfo } from 'net';

let createdSchema: GraphQLSchema;

let GRAPHQL_PORT: number;
let API_PORT: number;
let graphqlPort: number;
let apiPort: number;

let yogaServer: YogaNodeServerInstance<any, any, any>;
let yogaServer: Server;

describe('OpenAPI Loader: example_api7', () => {
// Set up the schema first and run example API servers
beforeAll(async () => {
GRAPHQL_PORT = await getPort();
API_PORT = await getPort();
const apiServer = await startServer();
apiPort = (apiServer.address() as AddressInfo).port;

createdSchema = await loadGraphQLSchemaFromOpenAPI('example_api7', {
fetch,
baseUrl: `http://127.0.0.1:${API_PORT}/api`,
baseUrl: `http://127.0.0.1:${apiPort}/api`,
source: './fixtures/example_oas7.json',
cwd: __dirname,
pubsub,
});

yogaServer = createServer({
const yoga = createYoga({
schema: createdSchema,
port: GRAPHQL_PORT,
context: { pubsub },
maskedErrors: false,
logging: false,
});

await Promise.all([yogaServer.start(), startServer(API_PORT)]);
});
yogaServer = createServer(yoga);

await new Promise<void>(resolve => yogaServer.listen(0, resolve));

afterAll(async () => {
await Promise.all([yogaServer.stop(), stopServer()]);
graphqlPort = (yogaServer.address() as AddressInfo).port;
});

afterAll(() => Promise.all([new Promise(resolve => yogaServer.close(resolve)), stopServer()]));

it('Receive data from the subscription after creating a new instance', async () => {
const userName = 'Carlos';
const deviceName = 'Bot';
Expand All @@ -67,7 +70,7 @@ describe('OpenAPI Loader: example_api7', () => {
}
}
`;
const baseUrl = `http://127.0.0.1:${GRAPHQL_PORT}/graphql`;
const baseUrl = `http://127.0.0.1:${graphqlPort}/graphql`;
const url = new URL(baseUrl);

url.searchParams.append('query', subscriptionOperation);
Expand Down

0 comments on commit d08ed0e

Please sign in to comment.