Skip to content

Commit

Permalink
chore: update dependencies - apollo-server3, mongoose6
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Sep 11, 2021
1 parent cb7ba25 commit 715f3d3
Show file tree
Hide file tree
Showing 14 changed files with 1,349 additions and 1,348 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import { MongoMemoryServer } from 'mongodb-memory-server';
import seed from '../data/seed';
import meta from '../index';

let mongoServer;
let mongoServer: MongoMemoryServer;
let con;
let db;
beforeAll(async () => {
mongoServer = new MongoMemoryServer({ instance: { dbName: 'user' } });
const mongoUri = await mongoServer.getConnectionString();
mongoose.set('useCreateIndex', true);
const opts = { useNewUrlParser: true, useUnifiedTopology: true };
mongoose.connect(mongoUri, opts);
con = await MongoClient.connect(mongoUri, opts);
mongoServer = await MongoMemoryServer.create({ instance: { dbName: 'user' } });
const mongoUri = mongoServer.getUri('user');
mongoose.connect(mongoUri);
con = await MongoClient.connect(mongoUri);
db = con.db('user');
await seed(db);
// take time to mongo create indexes if needed
Expand Down
4 changes: 2 additions & 2 deletions examples/mongooseDiscriminators/data/seed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { ObjectID } from 'mongodb';
import { ObjectId } from 'mongodb';

const collectionPrefix = 'md_';

Expand All @@ -14,7 +14,7 @@ export default async function seed(db: any) {
const colName = `${collectionPrefix || ''}${file}`;
const data = JSON.parse(fs.readFileSync(`${__dirname}/${file}.json`, 'utf8'));
data.forEach((_, i) => {
data[i]._id = ObjectID.createFromHexString(data[i]._id);
data[i]._id = ObjectId.createFromHexString(data[i]._id);
});
if (collectionNames.indexOf(colName) > -1) {
console.log(` '${colName}' dropped`);
Expand Down
19 changes: 11 additions & 8 deletions examples/northwind/__tests__/queriesFromIndex-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ import { MongoMemoryServer } from 'mongodb-memory-server';
import seed from '../data/seed';
import meta from '../index';

let mongoServer;
let mongoServer: MongoMemoryServer;
let con;
let db;
beforeAll(async () => {
mongoServer = new MongoMemoryServer({ instance: { dbName: 'northwind' } });
const mongoUri = await mongoServer.getConnectionString();
const opts = { useNewUrlParser: true, useUnifiedTopology: true };
mongoose.connect(mongoUri, opts);
mongoServer = await MongoMemoryServer.create({
instance: { dbName: 'user', storageEngine: 'wiredTiger' },
});
const mongoUri = mongoServer.getUri('northwind');
mongoose.connect(mongoUri, { autoIndex: false });
con = await MongoClient.connect(mongoUri);
mongoose.connection.once('disconnected', () => {
console.log('MongoDB disconnected!');
});
con = await MongoClient.connect(mongoUri, opts);
con = await MongoClient.connect(mongoUri);
db = con.db('northwind');
await seed(db);
// take time to mongo create indexes if needed
await new Promise((resolve) => setTimeout(resolve, 3000));
await Promise.all(
mongoose.modelNames().map((m) => mongoose.models[m].ensureIndexes({ background: false }))
);
});

afterAll(async () => {
Expand Down
6 changes: 3 additions & 3 deletions examples/northwind/queryCostPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export function initQueryComplexityPlugin(opts: {
maxComplexity?: number;
}): ApolloServerPlugin {
return {
requestDidStart: () => {
async requestDidStart() {
let complexity = 0;
const maxComplexity = opts.maxComplexity || 10000;
return {
didResolveOperation({ request, document }) {
async didResolveOperation({ request, document }) {
/**
* This provides GraphQL query analysis to be able to react on complex queries to your GraphQL server.
* This can be used to protect your GraphQL servers against resource exhaustion and DoS attacks.
Expand Down Expand Up @@ -58,7 +58,7 @@ export function initQueryComplexityPlugin(opts: {
);
}
},
willSendResponse({ response }) {
async willSendResponse({ response }) {
response.extensions = response.extensions || {};
response.extensions.complexity = complexity;
response.extensions.maxComplexity = maxComplexity;
Expand Down
2 changes: 1 addition & 1 deletion examples/northwind/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// import { SchemaComposer } from 'graphql-compose';
// const schemaComposer = new SchemaComposer();

import { PubSub } from 'apollo-server-express';
import { PubSub } from 'graphql-subscriptions';
import { schemaComposer } from './schemaComposer';
import { categoryFindManyResolver, categoryFindOneResolver } from './models/category';
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Object {
"age": 24,
"contacts": Object {
"email": "[email protected]",
"phones": CoreMongooseArray [
"phones": Array [
"111-222-333-444",
"444-555-666-777",
],
Expand Down Expand Up @@ -69,7 +69,7 @@ Object {
"email": "[email protected]",
},
"gender": "female",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "en",
"skill": "fluent",
Expand All @@ -92,7 +92,7 @@ Object {
"email": "[email protected]",
},
"gender": "male",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "en",
"skill": "fluent",
Expand All @@ -111,7 +111,7 @@ Object {
"email": "[email protected]",
},
"gender": "male",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "ru",
"skill": "fluent",
Expand All @@ -126,7 +126,7 @@ Object {
"email": "[email protected]",
},
"gender": "male",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "fr",
"skill": "basic",
Expand All @@ -145,7 +145,7 @@ Object {
"email": "[email protected]",
},
"gender": "male",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "es",
"skill": "fluent",
Expand All @@ -168,7 +168,7 @@ Object {
"email": "[email protected]",
},
"gender": "male",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "ru",
"skill": "fluent",
Expand All @@ -193,7 +193,7 @@ Object {
"email": "[email protected]",
},
"gender": "male",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "fr",
"skill": "basic",
Expand All @@ -212,7 +212,7 @@ Object {
"email": "[email protected]",
},
"gender": "male",
"languages": CoreDocumentArray [
"languages": Array [
Object {
"language": "es",
"skill": "fluent",
Expand Down
17 changes: 8 additions & 9 deletions examples/user/__tests__/queriesFromIndex-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@ import { MongoMemoryServer } from 'mongodb-memory-server';
import seed from '../data/seed';
import meta from '../index';

let mongoServer;
let mongoServer: MongoMemoryServer;
let con;
let db;
beforeAll(async () => {
mongoServer = new MongoMemoryServer({ instance: { dbName: 'user' } });
const mongoUri = await mongoServer.getConnectionString();
mongoose.set('useCreateIndex', true);
const opts = { useNewUrlParser: true, useUnifiedTopology: true };
mongoose.connect(mongoUri, opts);
con = await MongoClient.connect(mongoUri, opts);
mongoServer = await MongoMemoryServer.create({ instance: { dbName: 'user' } });
const mongoUri = mongoServer.getUri('user');
mongoose.connect(mongoUri, { autoIndex: false });
con = await MongoClient.connect(mongoUri);
db = con.db('user');
await seed(db);
// take time to mongo create indexes if needed
await new Promise((resolve) => setTimeout(resolve, 1000));
await Promise.all(
mongoose.modelNames().map((m) => mongoose.models[m].ensureIndexes({ background: false }))
);
});

afterAll(async () => {
Expand Down
4 changes: 2 additions & 2 deletions examples/user/data/seed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs';
import { ObjectID, Decimal128 } from 'mongodb';
import { ObjectId, Decimal128 } from 'mongodb';

const collectionPrefix = 'user_';

Expand All @@ -14,7 +14,7 @@ export default async function seed(db: any) {
const colName = `${collectionPrefix || ''}${file}`;
const data = JSON.parse(fs.readFileSync(`${__dirname}/${file}.json`, 'utf8'));
data.forEach((_, i) => {
data[i]._id = ObjectID.createFromHexString(data[i]._id);
data[i]._id = ObjectId.createFromHexString(data[i]._id);
data[i].salaryDecimal = Decimal128.fromString(data[i].salaryDecimal);
});
if (collectionNames.indexOf(colName) > -1) {
Expand Down
4 changes: 4 additions & 0 deletions examples/user/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export class UserDoc /* :: extends Mongoose$Document */ {
};

gender: 'male' | 'female';

address?: any;
someMixed?: any;
salaryDecimal?: any;
}

UserSchema.index({ gender: 1, age: -1 });
Expand Down
15 changes: 10 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import express from 'express';
import cors from 'cors';
import { ApolloServer } from 'apollo-server-express';
import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';
import { altairExpress } from 'altair-express-middleware';
import { express as voyagerMiddleware } from 'graphql-voyager/middleware';
import http from 'http';
Expand Down Expand Up @@ -57,17 +58,21 @@ httpServer.listen(PORT, () => {
);
});

function addExample(example, uri) {
async function addExample(example, uri) {
example.uri = `/${uri}`;

const server = new ApolloServer({
schema: example.schema,
introspection: true,
playground: {
subscriptionEndpoint: process.env.SUBSCRIPTION_ENDPOINT || `ws://localhost:${PORT}/northwind`,
},
plugins: example.plugins,
plugins: [
ApolloServerPluginLandingPageGraphQLPlayground({
subscriptionEndpoint:
process.env.SUBSCRIPTION_ENDPOINT || `ws://localhost:${PORT}/northwind`,
}),
...(example.plugins || []),
],
});
await server.start();
server.applyMiddleware({ app: app as any, path: example.uri });

app.use(
Expand Down
14 changes: 2 additions & 12 deletions mongooseConnection.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import mongoose from 'mongoose';
import { MONGODB_URI } from './config';

mongoose.Promise = Promise;

const opts = {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
};

mongoose.connect(MONGODB_URI, opts);

mongoose.connect(MONGODB_URI);
export const { connection } = mongoose;
connection.on('error', (e) => {
if (e.message.code === 'ETIMEDOUT') {
console.log(e);
mongoose.connect(MONGODB_URI, opts);
mongoose.connect(MONGODB_URI);
}
console.log(e);
});
Expand Down
56 changes: 28 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,48 @@
},
"homepage": "https://github.com/nodkz/graphql-compose-examples",
"dependencies": {
"@apollo/federation": "0.25.2",
"altair-express-middleware": "4.0.6",
"apollo-server-express": "2.25.2",
"aws-sdk": "2.935.0",
"concurrently": "^6.2.0",
"@apollo/federation": "0.32.0",
"altair-express-middleware": "4.0.9",
"apollo-server-express": "3.3.0",
"aws-sdk": "2.987.0",
"concurrently": "6.2.1",
"cors": "2.8.5",
"dedent": "0.7.0",
"elasticsearch": "16.7.2",
"express": "4.17.1",
"graphql": "15.5.1",
"graphql-compose": "9.0.1",
"graphql": "15.5.3",
"graphql-compose": "9.0.3",
"graphql-compose-aws": "5.2.1",
"graphql-compose-connection": "8.2.0",
"graphql-compose-connection": "8.2.1",
"graphql-compose-elasticsearch": "5.0.0",
"graphql-compose-mongoose": "9.4.2",
"graphql-compose-pagination": "8.2.0",
"graphql-query-complexity": "0.8.1",
"graphql-compose-mongoose": "9.6.0",
"graphql-compose-pagination": "8.3.0",
"graphql-query-complexity": "0.9.0",
"graphql-subscriptions": "^1.2.1",
"graphql-voyager": "1.0.0-rc.31",
"mongoose": "5.12.15",
"subscriptions-transport-ws": "0.9.19",
"ts-node": "^10.0.0",
"typescript": "4.3.4"
"mongoose": "6.0.5",
"ts-node": "10.2.1",
"typescript": "4.4.3"
},
"devDependencies": {
"@types/jest": "26.0.23",
"@types/lodash": "^4.14.168",
"@typescript-eslint/eslint-plugin": "4.28.0",
"@typescript-eslint/parser": "4.28.0",
"@types/jest": "27.0.1",
"@types/lodash": "4.14.172",
"@typescript-eslint/eslint-plugin": "4.31.0",
"@typescript-eslint/parser": "4.31.0",
"cz-conventional-changelog": "3.3.0",
"eslint": "7.29.0",
"eslint": "7.32.0",
"eslint-config-airbnb-base": "14.2.1",
"eslint-config-prettier": "8.3.0",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-prettier": "3.4.0",
"jest": "27.0.5",
"eslint-plugin-import": "2.24.2",
"eslint-plugin-prettier": "4.0.0",
"jest": "27.1.1",
"jest-junit": "12.2.0",
"mongodb-memory-server": "6.9.6",
"nodemon": "2.0.7",
"prettier": "2.3.2",
"mongodb-memory-server": "7.4.0",
"nodemon": "2.0.12",
"prettier": "2.4.0",
"rimraf": "3.0.2",
"ts-jest": "27.0.3",
"ts-node-dev": "^1.1.6"
"ts-jest": "27.0.5",
"ts-node-dev": "1.1.8"
},
"scripts": {
"seed": "./node_modules/.bin/ts-node ./scripts/seed.ts",
Expand Down
5 changes: 1 addition & 4 deletions scripts/seedHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ function getDBName(uri: string) {
export async function mongoConnect(): Promise<Db & { con?: MongoClient }> {
let db: Db & { con?: MongoClient };
if (!db) {
const con = await MongoClient.connect(MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const con = await MongoClient.connect(MONGODB_URI);
db = con.db(getDBName(MONGODB_URI));
db.con = con;
}
Expand Down
Loading

0 comments on commit 715f3d3

Please sign in to comment.