Skip to content

Commit 4c7de01

Browse files
sgpinkusnodkz
andauthored
docs: extend intro README.md code example with basic usage demo (ref #330)
* Extend intro README.md code example with basic usage demo (ref #330). * docs: simplify demo; make graphql call via object which will remain the only way in v16 Co-authored-by: nodkz <[email protected]>
1 parent e179910 commit 4c7de01

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

README.md

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const LanguagesSchema = new mongoose.Schema({
9898
language: String,
9999
skill: {
100100
type: String,
101-
enum: [ 'basic', 'fluent', 'native' ],
101+
enum: ['basic', 'fluent', 'native'],
102102
},
103103
});
104104

@@ -128,12 +128,11 @@ const UserSchema = new mongoose.Schema({
128128
});
129129
const User = mongoose.model('User', UserSchema);
130130

131-
132131
// STEP 2: CONVERT MONGOOSE MODEL TO GraphQL PIECES
133132
const customizationOptions = {}; // left it empty for simplicity, described below
134133
const UserTC = composeMongoose(User, customizationOptions);
135134

136-
// STEP 3: Add needed CRUD User operations to the GraphQL Schema
135+
// STEP 3: ADD NEEDED CRUD USER OPERATIONS TO THE GraphQL SCHEMA
137136
// via graphql-compose it will be much much easier, with less typing
138137
schemaComposer.Query.addFields({
139138
userById: UserTC.mongooseResolvers.findById(),
@@ -164,8 +163,44 @@ schemaComposer.Mutation.addFields({
164163
userRemoveMany: UserTC.mongooseResolvers.removeMany(),
165164
});
166165

167-
const graphqlSchema = schemaComposer.buildSchema();
168-
export default graphqlSchema;
166+
// STEP 4: BUILD GraphQL SCHEMA OBJECT
167+
const schema = schemaComposer.buildSchema();
168+
export default schema;
169+
170+
// STEP 5: DEMO USE OF GraphQL SCHEMA OBJECT
171+
// Just a demo, normally you'd pass schema object to server such as Apollo server.
172+
import { graphql } from 'graphql';
173+
174+
(async () => {
175+
await mongoose.connect('mongodb://localhost:27017/test');
176+
await mongoose.connection.dropDatabase();
177+
178+
await User.create({ name: 'alice', age: 29, gender: 'female' });
179+
await User.create({ name: 'maria', age: 31, gender: 'female' });
180+
const bob = await User.create({ name: 'bob', age: 30, gender: 'male' });
181+
182+
const response1 = await graphql({
183+
schema,
184+
source: 'query { userMany { _id name } }',
185+
});
186+
console.dir(response1, { depth: 5 });
187+
188+
const response2 = await graphql({
189+
schema,
190+
source: 'query($id: MongoID!) { userById(_id: $id) { _id name } }',
191+
variableValues: { id: bob._id },
192+
});
193+
console.dir(response2, { depth: 5 });
194+
195+
const response3 = await graphql({
196+
schema,
197+
source: 'mutation($id: MongoID!, $name: String) { userUpdateOne(filter: {_id: $id}, record: { name: $name }) { record { _id name } } }',
198+
variableValues: { id: bob._id, name: 'bill' },
199+
});
200+
console.dir(response3, { depth: 5 });
201+
202+
mongoose.disconnect();
203+
})();
169204
```
170205

171206
That's all!

0 commit comments

Comments
 (0)