Skip to content

Commit

Permalink
exapmle1
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonhsieh committed Jan 9, 2019
0 parents commit b96fbd2
Show file tree
Hide file tree
Showing 8 changed files with 9,686 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"plugins": ["@babel/plugin-proposal-object-rest-spread"],
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": 6
}
}
]
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Start the example

`npm i`

`npm run start:example1`

`npm run start:example2`

...

### this repo is records for using graphql-compose-mongoose

codes and examples from below links

https://github.com/graphql-compose/graphql-compose-examples

https://github.com/graphql-compose/graphql-compose-mongoose/issues/138
27 changes: 27 additions & 0 deletions example1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ApolloServer } from "apollo-server";
import schema from "./userGqlSchema";
import mongoose from "mongoose";
import MongoMemoryServer from "mongodb-memory-server";
const mongod = new MongoMemoryServer();

(async () => {
const uri = await mongod.getConnectionString();
const port = await mongod.getPort();
const dbPath = await mongod.getDbPath();
const dbName = await mongod.getDbName();
console.log(uri, port, dbPath, dbName);

mongoose
.connect(
uri,
{ useNewUrlParser: true }
)
.then(() => console.log("DB connected"));

const server = new ApolloServer(schema);

server.listen().then(({ url }) => {
console.log("example1 is a working simple user schema");
console.log(`🚀 Server ready at ${url}`);
});
})();
33 changes: 33 additions & 0 deletions example1/userGqlSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import mongoose from "mongoose";
import { composeWithMongoose } from "graphql-compose-mongoose";
import { schemaComposer } from "graphql-compose";

const UserSchema = new mongoose.Schema(
{
firstName: String,
lastName: String,
pasword: String
},
{
timestamps: true
}
);

const User = mongoose.model("User", UserSchema);
const customizationOptions = {};
const UserTC = composeWithMongoose(User, customizationOptions);

schemaComposer.Query.addFields({
userById: UserTC.getResolver("findById"),
userByIds: UserTC.getResolver("findByIds"),
userOne: UserTC.getResolver("findOne")
});

schemaComposer.Mutation.addFields({
userCreateOne: UserTC.getResolver("createOne"),
userUpdateById: UserTC.getResolver("updateById"),
userUpdateOne: UserTC.getResolver("updateOne")
});

const schema = schemaComposer.buildSchema();
export default { schema };
Empty file added index.js
Empty file.
Loading

0 comments on commit b96fbd2

Please sign in to comment.