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

Update mongoose 8 #439

Merged
merged 26 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 16 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
57 changes: 57 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
root = true

[*]
charset = utf-8
max_line_length = 120
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2

[{Makefile, makefile}]
indent_style = tab

[*.go]
indent_style = tab

[*.proto]
indent_size = 2

[*.swift]
indent_size = 4

[*.tmpl]
indent_size = 2

[{*.js, *.ts, *.jsx, *.tsx}]
indent_size = 2

[*.html]
indent_size = 2

[*.bat]
end_of_line = crlf

[*.{json, yml, yaml}]
indent_size = 2

[.{babelrc, eslintrc}]
indent_size = 2

[{Fastfile, .buckconfig, BUCK}]
indent_size = 2

[*.diff]
indent_size = 1

[*.m]
indent_size = 1
indent_style = space

[*.java]
indent_size = 4
indent_style = space

[*.md]
trim_trailing_whitespace = false
24 changes: 12 additions & 12 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ jobs:
contents: write
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [16.x, 18.x, 20.x]
steps:
- run: echo "🎉 The job was triggered by a ${{ github.event_name }} event."
- uses: styfle/cancel-workflow-action@0.11.0
with:
workflow_id: nodejs.yml
access_token: ${{ secrets.GITHUB_TOKEN }}
- uses: FranzDiebold/github-env-vars-action@v2
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install node_modules
Expand All @@ -32,16 +32,16 @@ jobs:
run: yarn test
env:
CI: true
- name: Testing with previous mongoose versions 6
run: yarn test-prev-vers-6
- name: Testing with previous mongoose versions 7
run: yarn test-prev-vers-7
env:
CI: true
- name: Testing with previous mongoose versions 5
run: yarn test-prev-vers-5
- name: Testing with previous mongoose versions 6
run: yarn test-prev-vers-6
env:
CI: true
- name: Send codecov.io stats
if: matrix.node-version == '14.x'
if: matrix.node-version == '18.x'
run: bash <(curl -s https://codecov.io/bash) || echo ''

publish:
Expand All @@ -52,11 +52,11 @@ jobs:
packages: write
contents: write
steps:
- uses: actions/checkout@v3
- name: Use Node.js 14
uses: actions/setup-node@v3
- uses: actions/checkout@v4
- name: Use Node.js 18
uses: actions/setup-node@v4
with:
node-version: 14.x
node-version: 18.x
- name: Install node_modules
run: yarn install
- name: Build
Expand Down
17 changes: 9 additions & 8 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.json',
isolatedModules: true,
diagnostics: false,
},
},
moduleFileExtensions: ['ts', 'js'],
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.ts$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.json',
isolatedModules: true,
diagnostics: false,
},
],
'^.+\\.js$': 'babel-jest',
},
roots: ['<rootDir>/src'],
Expand Down
39 changes: 21 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@
},
"peerDependencies": {
"graphql-compose": "^7.21.4 || ^8.0.0 || ^9.0.0",
"mongoose": "^7.0.0 || ^6.0.0 || ^5.0.0 || ^4.4.0"
"mongoose": "^8.0.0 || ^7.0.0 || ^6.0.0"
},
"devDependencies": {
"@types/jest": "28.1.8",
"@typescript-eslint/eslint-plugin": "5.54.0",
"@typescript-eslint/parser": "5.54.0",
"eslint": "8.35.0",
"@types/jest": "29.5.7",
"@typescript-eslint/eslint-plugin": "6.9.1",
"@typescript-eslint/parser": "6.9.1",
"eslint": "8.52.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-config-prettier": "8.6.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-prettier": "4.2.1",
"graphql": "16.6.0",
"eslint-config-prettier": "9.0.0",
"eslint-plugin-import": "2.29.0",
"eslint-plugin-prettier": "5.0.1",
"graphql": "16.8.1",
"graphql-compose": "9.0.10",
"jest": "28.1.3",
"mongodb-memory-server": "8.11.5",
"mongoose": "7.0.0",
"prettier": "2.8.4",
"jest": "29.7.0",
"mongodb-memory-server": "9.0.1",
"mongoose": "8.0.0",
"prettier": "3.0.3",
"request": "2.88.2",
"rimraf": "4.1.3",
"ts-jest": "28.0.8",
"typescript": "4.9.5"
"rimraf": "5.0.5",
"ts-jest": "29.1.1",
"typescript": "5.2.2"
},
"scripts": {
"prepare": "tsc -p ./tsconfig.build.json",
Expand All @@ -64,7 +64,10 @@
"link": "yarn build && yarn link graphql-compose && yarn link graphql-compose-connection && yarn link graphql-compose-pagination && yarn link mongoose && yarn link",
"unlink": "rimraf node_modules && yarn install",
"semantic-release": "semantic-release",
"test-prev-vers-6": "yarn add mongoose@6.1.2 --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock",
"test-prev-vers-5": "yarn add mongoose@5.13.8 --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock"
"test-prev-vers-7": "yarn add mongoose@7.6.4 --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock",
"test-prev-vers-6": "yarn add mongoose@6.1.2 --dev --ignore-scripts && yarn coverage && git checkout HEAD -- package.json yarn.lock"
},
"engines": {
"node": ">=16.0.0"
}
}
10 changes: 1 addition & 9 deletions src/__mocks__/mongooseCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ mongoose.createConnection = (async () => {
const mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();

// originalConnect.bind(mongoose)(mongoUri, { useMongoClient: true }); // mongoose 4
originalConnect.bind(mongoose)(
mongoUri,
{
useNewUrlParser: true,
useUnifiedTopology: true,
} as any /* for tests compatibility with mongoose v5 & v6 */
); // mongoose 5
// originalConnect.bind(mongoose)(mongoUri, {}); // mongoose 6
originalConnect.bind(mongoose)(mongoUri, {});

mongoose.connection.on('error', (e) => {
if (e.message.code === 'ETIMEDOUT') {
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/__snapshots__/integration-test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`integration tests projection should request all fields to rawData field: projection from all fields 1`] = `
Array [
[
"__v",
"_id",
"age",
Expand All @@ -24,9 +24,9 @@ Array [
`;

exports[`integration tests projection should request only fields from query: projection from query fields 1`] = `
Object {
"data": Object {
"user": Object {
{
"data": {
"user": {
"name": "Name",
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/github_issues/268-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('issue #268 - Allow to provide `suffix` option for resolvers configs',
const createOne1 = UserTC.mongooseResolvers.createOne();
expect(createOne1.getTypeName()).toBe('CreateOneUserPayload');
expect(createOne1.getArgITC('record').toSDL()).toMatchInlineSnapshot(`
"\\"\\"\\"\\"\\"\\"
"""""""
input CreateOneUserInput {
name: String!
age: Float
Expand All @@ -53,7 +53,7 @@ describe('issue #268 - Allow to provide `suffix` option for resolvers configs',
});
expect(createOne2.getTypeName()).toBe('CreateOneUserShortPayload');
expect(createOne2.getArgITC('record').toSDL()).toMatchInlineSnapshot(`
"\\"\\"\\"\\"\\"\\"
"""""""
input CreateOneUserShortInput {
_id: Int
name: String!
Expand Down
30 changes: 15 additions & 15 deletions src/__tests__/github_issues/286-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type CreateManyUserPayload {
\\"\\"\\"Documents IDs\\"\\"\\"
"""Documents IDs"""
recordIds: [Int!]!

\\"\\"\\"Created documents\\"\\"\\"
"""Created documents"""
records: [User!]

\\"\\"\\"Number of created documents\\"\\"\\"
"""Number of created documents"""
createdCount: Int!
}"
`);
Expand All @@ -58,10 +58,10 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type CreateOneUserPayload {
\\"\\"\\"Document ID\\"\\"\\"
"""Document ID"""
recordId: Int

\\"\\"\\"Created document\\"\\"\\"
"""Created document"""
record: User
}"
`);
Expand All @@ -73,10 +73,10 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type RemoveByIdUserPayload {
\\"\\"\\"Document ID\\"\\"\\"
"""Document ID"""
recordId: Int

\\"\\"\\"Removed document\\"\\"\\"
"""Removed document"""
record: User
}"
`);
Expand All @@ -88,7 +88,7 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type RemoveManyUserPayload {
\\"\\"\\"Affected documents number\\"\\"\\"
"""Affected documents number"""
numAffected: Int
}"
`);
Expand All @@ -100,10 +100,10 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type RemoveOneUserPayload {
\\"\\"\\"Document ID\\"\\"\\"
"""Document ID"""
recordId: Int

\\"\\"\\"Removed document\\"\\"\\"
"""Removed document"""
record: User
}"
`);
Expand All @@ -115,10 +115,10 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type UpdateByIdUserPayload {
\\"\\"\\"Document ID\\"\\"\\"
"""Document ID"""
recordId: Int

\\"\\"\\"Updated document\\"\\"\\"
"""Updated document"""
record: User
}"
`);
Expand All @@ -130,7 +130,7 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type UpdateManyUserPayload {
\\"\\"\\"Affected documents number\\"\\"\\"
"""Affected documents number"""
numAffected: Int
}"
`);
Expand All @@ -142,10 +142,10 @@ describe('issue #286 - Allow to provide `disableErrorField` option for mutation
});
expect(resolver.getTypeComposer().toSDL()).toMatchInlineSnapshot(`
"type UpdateOneUserPayload {
\\"\\"\\"Document ID\\"\\"\\"
"""Document ID"""
recordId: Int

\\"\\"\\"Updated document\\"\\"\\"
"""Updated document"""
record: User
}"
`);
Expand Down
11 changes: 7 additions & 4 deletions src/composeMongoose.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { SchemaComposer, Resolver, InputTypeComposer } from 'graphql-compose';
import { schemaComposer as globalSchemaComposer, ObjectTypeComposer } from 'graphql-compose';
import type { Model, Document } from 'mongoose';
import type { InputTypeComposer, Resolver, SchemaComposer } from 'graphql-compose';
import { ObjectTypeComposer, schemaComposer as globalSchemaComposer } from 'graphql-compose';
import type { Document, Model } from 'mongoose';
import { convertModelToGraphQL } from './fieldsConverter';
import { resolverFactory } from './resolvers';
import MongoID from './types/MongoID';
import { patchMongooseSchemaIndex } from './utils/patchMongoose';

export type TypeConverterInputTypeOpts = {
/**
Expand Down Expand Up @@ -100,7 +101,7 @@ export type GenerateResolverType<TDoc extends Document, TContext = any> = {

export type ObjectTypeComposerWithMongooseResolvers<
TDoc extends Document,
TContext = any
TContext = any,
> = ObjectTypeComposer<TDoc, TContext> & {
mongooseResolvers: GenerateResolverType<TDoc, TContext>;
};
Expand All @@ -120,6 +121,8 @@ export function composeMongoose<TDoc extends Document, TContext = any>(
`You try to generate GraphQL Type with name ${name} from mongoose model but this type already exists in SchemaComposer. Please choose another type name "composeWithMongoose(model, { name: 'NewTypeName' })", or reuse existed type "schemaComposer.getOTC('TypeName')", or remove type from SchemaComposer before calling composeWithMongoose method "schemaComposer.delete('TypeName')".`
);
}
patchMongooseSchemaIndex();

if (sc.has(m.schema)) {
// looks like you want to generate new TypeComposer from model
// so remove cached model (which is used for cross-reference types)
Expand Down
Loading