Skip to content

Commit 7d90ea0

Browse files
committed
fix: If field _id present in OutputType then make it NonNull
This fix makes work with Flowtype and Relay Modern more comfortable. It makes _id field NonNull, so no need an additional checks on the client side is _id null or not. Mongoose and mongo almost always have _id for the document and it is not null. In rare cases if you tune mongoose model to have _id optional, you should manually change type for this generated field.
1 parent 5d2d52f commit 7d90ea0

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"watch": "jest --watch",
7979
"coverage": "jest --coverage --maxWorkers 2",
8080
"lint": "eslint --ext .js ./src",
81-
"flow": "./node_modules/.bin/flow stop && ./node_modules/.bin/flow",
81+
"flow": "./node_modules/.bin/flow",
8282
"test": "npm run coverage && npm run lint && npm run flow",
8383
"link": "yarn build && yarn link graphql-compose && yarn link graphql-compose-connection && yarn link mongoose && yarn link",
8484
"unlink": "yarn unlink graphql-compose && yarn unlink graphql-compose-connection && yarn unlink mongoose && yarn add graphql-compose graphql-compose-connection mongoose --dev",

src/__tests__/composeWithMongoose-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
import mongoose from 'mongoose';
55
import { TypeComposer, InputTypeComposer } from 'graphql-compose';
6+
import { GraphQLNonNull } from 'graphql-compose/lib/graphql';
67
import { UserModel } from '../__mocks__/userModel';
78
import { composeWithMongoose } from '../composeWithMongoose';
89
import typeStorage from '../typeStorage';
10+
import GraphQLMongoID from '../types/mongoid';
911

1012
beforeAll(() => UserModel.base.connect());
1113
afterAll(() => UserModel.base.disconnect());
@@ -41,6 +43,13 @@ describe('composeWithMongoose ->', () => {
4143
expect.arrayContaining(['_id', 'name', 'gender', 'age'])
4244
);
4345
});
46+
47+
it('should have NonNull _id field', () => {
48+
const tc = composeWithMongoose(UserModel);
49+
expect(tc.getFieldType('_id')).toBeInstanceOf(GraphQLNonNull);
50+
// $FlowFixMe
51+
expect(tc.getFieldType('_id').ofType).toBe(GraphQLMongoID);
52+
});
4453
});
4554

4655
describe('filterFields()', () => {

src/composeWithMongoose.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* eslint-disable no-use-before-define, no-param-reassign, global-require */
33

44
import { TypeComposer, InputTypeComposer } from 'graphql-compose';
5+
import { GraphQLNonNull } from 'graphql-compose/lib/graphql';
56
import type { MongooseModel } from 'mongoose';
67
import type { ConnectionSortMapOpts } from 'graphql-compose-connection';
78
import { convertModelToGraphQL } from './fieldsConverter';
@@ -135,6 +136,14 @@ export function composeWithMongoose(
135136
createResolvers(model, typeComposer, opts.resolvers || {});
136137
}
137138

139+
if (typeComposer.hasField('_id')) {
140+
const fc = typeComposer.getField('_id');
141+
if (fc.type && !(fc.type instanceof GraphQLNonNull)) {
142+
fc.type = new GraphQLNonNull(fc.type);
143+
typeComposer.setField('_id', fc);
144+
}
145+
}
146+
138147
return typeComposer;
139148
}
140149

0 commit comments

Comments
 (0)