Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
df8d4f4
feat(discriminators): add composeWithMongooseDiscriminators
mernxl Jul 4, 2018
16b1b33
test(discriminators): add few test, proof of concept
mernxl Jul 4, 2018
9c6bf26
fix: eslint and prettier errors
nodkz Jul 6, 2018
78f0ea4
test: use `mongodb-memory-server` in tests, instead of locally instal…
nodkz Jul 6, 2018
f443704
test: fix flowtype errors
nodkz Jul 6, 2018
b5981d8
refactor: move `pagination` resolver code to its own file with tests …
nodkz Jul 9, 2018
20d283e
refactor: move `connection` resolver code to /resolvers folder (Requi…
nodkz Jul 9, 2018
87e6cf0
Merge branch 'refactor_resolvers' into mongoose-discriminantors
nodkz Jul 9, 2018
5d5b6c6
refactor(discriminators): rename and move discriminator files to sepa…
mernxl Jul 10, 2018
50fd202
refactor(discriminators): revert ChildDiscriminator class to TypeComp…
mernxl Jul 10, 2018
eb46780
refactor(discriminators): move EMCResolvers mapping to resolvers/index
mernxl Jul 10, 2018
4878844
fix: mongoose 5.2.0 now uses `countDocuments` instead of `count`
nodkz Jul 10, 2018
922a302
feat: already generated TC may be obtained via schemaComposer.getTC(m…
nodkz Jul 10, 2018
d31de65
feat(discriminator): add ability to merge customization options
mernxl Jul 10, 2018
e93a6ab
Merge branch 'master' into mongoose-discriminantors
mernxl Jul 10, 2018
c4eae4d
test(discriminator): add test suits for _disTypes, minor refactor
mernxl Jul 11, 2018
50d1a2b
test: remove redundant cleaning of _gqcTypeComposer property
nodkz Jul 11, 2018
dad2397
style: fix flowtype error and use `getFieldConfig` method
nodkz Jul 11, 2018
62946c3
chore: update dependencies
nodkz Jul 12, 2018
8d460b1
fix: better to give new name to Interface rather than user type
nodkz Jul 12, 2018
1634944
fix: remove YAGNI method getGQC(), rename GQC to schemaComposer
nodkz Jul 12, 2018
5500456
refactor: `_disTypes` replaced via new schemaComposer.addSchemaMustHa…
nodkz Jul 12, 2018
b60d243
refactor(discriminator): move DiscriminatorTypeComposer to own file
mernxl Jul 13, 2018
2ae085b
refactor(discriminator): replace getDBaseName() with getTypeName()
mernxl Jul 13, 2018
e8a6b1c
feat(discriminator): override more TypeComposer field change methods
mernxl Jul 13, 2018
0aa9b99
doc(README.md): add discriminator example and use case
mernxl Jul 13, 2018
156c21f
refactor(DiscriminatorTC): change constructor on static method `creat…
nodkz Jul 16, 2018
17bb1a7
chore: update graphql-compose to 4.7.1 version as minimal required ve…
nodkz Jul 16, 2018
1b39922
fix(Flowtype): improve flowtype type definitions
nodkz Jul 16, 2018
8a244cc
refactor: replace `GraphQLInterfaceType` by `InterfaceTypeComposer`
nodkz Jul 16, 2018
4279a7b
chore: fix flowtype definitions and cove rest tests files by flow
nodkz Jul 16, 2018
90f2723
refactor(discriminators): rename 'Options' to 'DiscriminatorOptions'
mernxl Jul 16, 2018
2bb447e
fix: expose composeWithMongooseDiscriminators, other utils to src/ind…
mernxl Jul 16, 2018
331e410
fix(discriminators): fix own member object iteration on prepare-resol…
mernxl Jul 16, 2018
15de649
fix(discriminators): fix grammatical error on error message
mernxl Jul 16, 2018
73afb58
test(discriminators): rearrange test files and test cases
mernxl Jul 16, 2018
f511059
test(discriminators): add tests for DiscriminatorTypeComposer methods
mernxl Jul 17, 2018
41c2cfe
test: refactor try catch with arrow function
nodkz Jul 17, 2018
0b2d024
chore: update flowtype defs and fix its errors
nodkz Jul 17, 2018
bdee04a
refactor: simplify discriminators directory structure
nodkz Jul 17, 2018
de1dca6
refactor: remove `customizationOptions` property from options
nodkz Jul 17, 2018
1b256d5
ci: remove greenkeeper
nodkz Jul 17, 2018
6a7b2b0
build: fix `es` build
nodkz Jul 17, 2018
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
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"env": {
"cjs": {
"plugins": [
"transform-class-properties",
["transform-runtime", { "polyfill": false }]
],
"presets": [
Expand All @@ -17,6 +18,7 @@
]
},
"mjs": {
"plugins": ["transform-class-properties"],
"presets": [
[
"env",
Expand All @@ -31,9 +33,11 @@
]
},
"es": {
"plugins": ["transform-class-properties"],
},
"node8": {
"plugins": [
"transform-class-properties",
["transform-runtime", { "polyfill": false }]
],
"presets": [
Expand All @@ -47,6 +51,9 @@
]
},
"test": {
"plugins": [
"transform-class-properties",
],
"presets": [
["env", {
"targets": {
Expand Down
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ notifications:
node_js:
- "9"
- "8"
before_install: yarn global add greenkeeper-lockfile@1
before_script: greenkeeper-lockfile-update
after_script: greenkeeper-lockfile-upload
script:
- yarn run test
- yarn run build
Expand Down
131 changes: 125 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ const User = mongoose.model('User', UserSchema);
const customizationOptions = {}; // left it empty for simplicity, described below
const UserTC = composeWithMongoose(User, customizationOptions);

// STEP 3: CREATE CRAZY GraphQL SCHEMA WITH ALL CRUD USER OPERATIONS
// STEP 3: Add needed CRUD User operations to the GraphQL Schema
// via graphql-compose it will be much much easier, with less typing
schemaComposer.rootQuery().addFields({
schemaComposer.Query.addFields({
userById: UserTC.getResolver('findById'),
userByIds: UserTC.getResolver('findByIds'),
userOne: UserTC.getResolver('findOne'),
Expand All @@ -104,7 +104,7 @@ schemaComposer.rootQuery().addFields({
userPagination: UserTC.getResolver('pagination'),
});

schemaComposer.rootMutation().addFields({
schemaComposer.Mutation.addFields({
userCreate: UserTC.getResolver('createOne'),
userUpdateById: UserTC.getResolver('updateById'),
userUpdateOne: UserTC.getResolver('updateOne'),
Expand All @@ -122,6 +122,126 @@ You think that is to much code?
I don't think so, because by default internally was created about 55 graphql types (for input, sorting, filtering). So you will need much much more lines of code to implement all these CRUD operations by hands.


### Working with Mongoose Collection Level Discriminators
Variable Namings
* `...DTC` - Suffix for a `DiscriminatorTypeComposer` instance, which is also an instance of `TypeComposer`. All fields and Relations manipulations on this instance affects all registered discriminators and the Discriminator Interface.

```js
import mongoose from 'mongoose';
import { schemaComposer } from 'graphql-compose';
import { composeWithMongooseDiscriminators } from 'graphql-compose-mongoose';

// pick a discriminatorKey
const DKey = 'type';

const enumCharacterType = {
PERSON: 'Person',
DROID: 'Droid',
};

// DEFINE BASE SCHEMA
const CharacterSchema = new mongoose.Schema({
// _id: field...
type: {
type: String,
require: true,
enum: (Object.keys(enumCharacterType): Array<string>),
description: 'Character type Droid or Person',
},

name: String,
height: Number,
mass: Number,
films: [String],
});

// DEFINE DISCRIMINATOR SCHEMAS
const DroidSchema = new mongoose.Schema({
makeDate: String,
primaryFunction: [String],
});

const PersonSchema = new mongoose.Schema({
gender: String,
hairColor: String,
starships: [String],
});

// set discriminator Key
CharacterSchema.set('discriminatorKey', DKey);

// create base Model
const CharacterModel = mongoose.model('Character', CharacterSchema);

// create mongoose discriminator models
const DroidModel = CharacterModel.discriminator(enumCharacterType.DROID, DroidSchema);
const PersonModel = CharacterModel.discriminator(enumCharacterType.PERSON, PersonSchema);

// create DiscriminatorTypeComposer
const baseOptions = { // regular TypeConverterOptions, passed to composeWithMongoose
fields: {
remove: ['friends'],
}
}
const CharacterDTC = composeWithMongooseDiscriminators(CharacterModel, baseOptions);

// create Discriminator Types
const droidTypeConverterOptions = { // this options will be merged with baseOptions -> customisationsOptions
fields: {
remove: ['makeDate'],
}
};
const DroidTC = CharacterDTC.discriminator(DroidModel, droidTypeConverterOptions);
const PersonTC = CharacterDTC.discriminator(PersonModel); // baseOptions -> customisationsOptions applied

// You may now use CharacterDTC to add fields to all Discriminators
// Use DroidTC, `PersonTC as any other TypeComposer.
schemaComposer.Mutation.addFields({
droidCreate: DroidTC.getResolver('createOne'),
personCreate: PersonTC.getResolver('createOne'),
});

const schema = schemaComposer.buildSchema();

describe('createOne', () => {
it('should create child document without specifying DKey', async () => {
const res = await graphql.graphql(
schema,
`mutation CreateCharacters {
droidCreate(record: {name: "Queue XL", modelNumber: 360 }) {
record {
__typename
type
name
modelNumber
}
}

personCreate(record: {name: "mernxl", dob: 57275272}) {
record {
__typename
type
name
dob
}
}
}`
);

expect(res).toEqual({
data: {
droidCreate: {
record: { __typename: 'Droid', type: 'Droid', name: 'Queue XL', modelNumber: 360 },
},
personCreate: {
record: { __typename: 'Person', type: 'Person', name: 'mernxl', dob: 57275272 },
},
},
});
});
});
```

## FAQ

### Can I get generated vanilla GraphQL types?
Expand Down Expand Up @@ -273,7 +393,7 @@ The typical implementation may be like this:
// extend resolve params with hook
rp.beforeRecordMutate = async function(doc, rp) {
doc.userTouchedAt = new Date();

const canMakeUpdate = await performAsyncTask( ...provide data from doc... )
if (!canMakeUpdate) {
throw new Error('Forbidden!');
Expand Down Expand Up @@ -304,7 +424,7 @@ function adminAccess(resolvers) {

// extend resolve params with hook
rp.beforeRecordMutate = async function(doc, rp) { ... }

return next(rp)
})
})
Expand Down Expand Up @@ -465,4 +585,3 @@ This plugin adds `pagination` resolver.

## License
[MIT](https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/LICENSE.md)

4 changes: 2 additions & 2 deletions flow-typed/npm/babel-cli_vx.x.x.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// flow-typed signature: adfc416d3944bc9e0c30c54aa3ad632b
// flow-typed version: <<STUB>>/babel-cli_v^6.26.0/flow_v0.54.1
// flow-typed signature: 40ac71f9403783e7957989cfd3c8695f
// flow-typed version: <<STUB>>/babel-cli_v^6.26.0/flow_v0.76.0

/**
* This is an autogenerated libdef stub for:
Expand Down
Loading