Skip to content

Commit

Permalink
docs: extend intro README.md code example with basic usage demo (ref #…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
sgpinkus and nodkz authored Jun 3, 2021
1 parent e179910 commit 4c7de01
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const LanguagesSchema = new mongoose.Schema({
language: String,
skill: {
type: String,
enum: [ 'basic', 'fluent', 'native' ],
enum: ['basic', 'fluent', 'native'],
},
});

Expand Down Expand Up @@ -128,12 +128,11 @@ const UserSchema = new mongoose.Schema({
});
const User = mongoose.model('User', UserSchema);


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

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

const graphqlSchema = schemaComposer.buildSchema();
export default graphqlSchema;
// STEP 4: BUILD GraphQL SCHEMA OBJECT
const schema = schemaComposer.buildSchema();
export default schema;

// STEP 5: DEMO USE OF GraphQL SCHEMA OBJECT
// Just a demo, normally you'd pass schema object to server such as Apollo server.
import { graphql } from 'graphql';

(async () => {
await mongoose.connect('mongodb://localhost:27017/test');
await mongoose.connection.dropDatabase();

await User.create({ name: 'alice', age: 29, gender: 'female' });
await User.create({ name: 'maria', age: 31, gender: 'female' });
const bob = await User.create({ name: 'bob', age: 30, gender: 'male' });

const response1 = await graphql({
schema,
source: 'query { userMany { _id name } }',
});
console.dir(response1, { depth: 5 });

const response2 = await graphql({
schema,
source: 'query($id: MongoID!) { userById(_id: $id) { _id name } }',
variableValues: { id: bob._id },
});
console.dir(response2, { depth: 5 });

const response3 = await graphql({
schema,
source: 'mutation($id: MongoID!, $name: String) { userUpdateOne(filter: {_id: $id}, record: { name: $name }) { record { _id name } } }',
variableValues: { id: bob._id, name: 'bill' },
});
console.dir(response3, { depth: 5 });

mongoose.disconnect();
})();
```

That's all!
Expand Down

0 comments on commit 4c7de01

Please sign in to comment.