Skip to content

Commit 0be4b64

Browse files
committed
add example to Readme
1 parent 5b6f041 commit 0be4b64

File tree

1 file changed

+85
-11
lines changed

1 file changed

+85
-11
lines changed

README.md

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,93 @@
11
graphql-compose-mongoose
22
======================
3-
This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose), which derives a graphql type from mongoose model.
3+
This is a plugin for [graphql-compose](https://github.com/nodkz/graphql-compose), which derives a bunch of GraphQL types and resolvers from mongoose models.
4+
5+
Example
6+
=======
7+
8+
```js
9+
import { Schema } from 'mongoose';
10+
import mongooseToTypeComposer from 'graphql-compose-mongoose';
11+
import { GraphQLSchema, GraphQLObjectType } from 'graphql';
12+
13+
// STEP 1: DEFINE MONGOOSE SCHEMA AND MODEL
14+
const LanguagesSchema = new Schema({
15+
language: String,
16+
skill: {
17+
type: String,
18+
enum: [ 'basic', 'fluent', 'native' ],
19+
},
20+
});
21+
22+
const UserSchema = new Schema({
23+
name: String, // standard types
24+
age: {
25+
type: Number,
26+
index: true,
27+
}
28+
languages: {
29+
type: [LanguagesSchema], // you may include other schemas (here included as array of embedded documents)
30+
default: [],
31+
},
32+
contacts: { // another mongoose way for providing embedded documents
33+
email: String,
34+
phones: [String], // array of strings
35+
},
36+
gender: { // enum field with values
37+
type: String,
38+
enum: ['male', 'female', 'ladyboy'],
39+
},
40+
});
41+
const UserModel = mongoose.model('UserModel', UserSchema);
42+
export default UserModel;
43+
44+
45+
46+
// STEP 2: CONVERT MONGOOSE MODEL TO GraphQL PIECES
47+
const typeComposer = mongooseToTypeComposer(UserModel);
48+
// get list of 12 Resolvers (findById, updateMany and others)
49+
const resolvers = typeComposer.getResolvers();
50+
51+
// typeComposer from (graphql-compose) provide bunch if useful methods
52+
// for modifying GraphQL Types (eg. add/remove fields, relate with other types)
53+
54+
55+
56+
// STEP 3: CREATE CRAZY GraphQL SCHEMA WITH ALL CRUD USER OPERATIONS
57+
const graphqlSchema = new GraphQLSchema({
58+
query: new GraphQLObjectType({
59+
name: 'RootQuery',
60+
fields: {
61+
userById: resolvers.get('findById').getFieldConfig(),
62+
userByIds: resolvers.get('findByIds').getFieldConfig(),
63+
userOne: resolvers.get('findOne').getFieldConfig(),
64+
userMany: resolvers.get('findMany').getFieldConfig(),
65+
userTotal: resolvers.get('count').getFieldConfig(),
66+
},
67+
}),
68+
mutation: new GraphQLObjectType({
69+
name: 'RootMutation',
70+
fields: {
71+
userCreate: resolvers.get('createOne').getFieldConfig(),
72+
userUpdateById: resolvers.get('updateById').getFieldConfig(),
73+
userUpdateOne: resolvers.get('updateOne').getFieldConfig(),
74+
userUpdateMany: resolvers.get('updateMany').getFieldConfig(),
75+
userRemoveById: resolvers.get('removeById').getFieldConfig(),
76+
userRemoveOne: resolvers.get('removeOne').getFieldConfig(),
77+
userRemoveMany: resolvers.get('removeMany').getFieldConfig(),
78+
},
79+
}),
80+
});
81+
```
82+
83+
mongooseToTypeComposer options
84+
==============================
85+
will be described
86+
487

588
TODO
689
====
7-
- [x] convert mongoose models to GraphQLObjectTypes (supports: all primitive types, enums, embedded documents, document arrays, embedded schemas, deep arrays of any type).
8-
- [x] write resolve methods (findById, findByIds, findOne, findMany, updateOne, updateMany, removeOne, removeMany, count)
9-
- [ ] realize helper methods for Type construction from [apiProposal]( https://github.com/nodkz/graphql-compose-mongoose/blob/master/src/metaApiProposal.js)
10-
- [ ] in queries add support for $lt, $gt and other selector's operators
11-
- [ ] add support GraphQL Connection Type
12-
13-
Contribute
14-
==========
15-
I actively welcome pull requests with code and doc fixes.
16-
Also if you made great middleware and want share it within this module, please feel free to open PR.
90+
- [ ] for `filter` arg add support for $lt, $gt and other selector's operators
1791

1892
[CHANGELOG](https://github.com/nodkz/graphql-compose-mongoose/blob/master/CHANGELOG.md)
1993

0 commit comments

Comments
 (0)