-
Notifications
You must be signed in to change notification settings - Fork 575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connect MongoDB to graphql-yoga #277
Comments
I have an example boilerplate because I was wondering the same. It is only one way to go about it so do any changes as you see fit. P.S. I also have a PostgreSQL branch if you need an example for it too. @rainwater11 |
Thanks for bringing this up, @rainwater11 🙂
|
@rainwater11 You just need to do your classic mongodb connection and give the result to the Quick example: // index.js
const start = async () => {
// Create a mongodb client
const mongoClient = await MongoClient.connect(
config.get("database.url"),
config.get("database.options")
);
const db = mongoClient.db(config.get("database.dbName"));
const graphQLServer = new GraphQLServer({
typeDefs: "./src/schema.graphql",
resolvers,
context: {db}
});
graphQLServer.start();
}
start(); // just to have the async/await available
// in resolver
module.exports = async (context, args , { db }) => {
await anything = db.collection("plop").find()
return anything.foo
}; More information about graphql and mongodb toguether here -> https://medium.com/the-ideal-system/graphql-and-mongodb-a-quick-example-34643e637e49 |
@fabien0102 Thanks for sharing! The funny part is that I am doing the same thing for PostgreSQL 😄 Going through the docs and repo gave me the idea of giving it a shot. |
That's awesome @JorgeCeja! I'd love to hear your opinion on the Prisma Postgres Connector which is available in an alpha preview as of now. It'd be interesting to hear your perspective on the difference in terms of development speed and any suggestions you have for the connector itself 🙂 |
@rainwater11 I'll make an example for it. |
Hey guys! I have an architecture question about using graphql-yoga with mongodb: const db = mongoose.connection;
const server = new GraphQLServer({
typeDefs: resolve(__dirname, './schema/typeDefs.graphql'),
resolvers,
context: { db },
}); and use it for querying like that const resolvers = {
Query: {
todos: async (_, args, ctx) => {
const todos = await ctx.db.model('Todo').find();
return todos;
},
},
} or should I import js model const Todo = require('../db/models/todo');
const resolvers = {
Query: {
todos: async () => {
const todos = await Todo.find();
return todos;
},
},
} What's the right way to work with MongoDB as you think? |
@borisowsky As i said earlier, I'll make an example for it |
@borisowsky it's better to use the context as dependency injection pattern, so it's easier to mock into your unit tests 😉 Indeed you can just pass any function as parameter to simulate your database behaviour |
please give example for graphql Subscription with mongoose |
@Menogus ya sorry, i was busy this week. But the long awaited weekend has come. I'll make it today. |
Just made the pull request, the example will be in the examples/mongodb directory. |
Subscription not in examples :(( |
@Menogus I'll add 👍 |
so....what about example for graphql Subscription with mongoose ?:)) |
|
Hey, @Urigo from The Guild here! You might know us from projects such as graphql-code-generator, envelop or graphql-tools. For a long time we thought that the Javascript ecosystem is still missing a lightweight cross-platform, but still highly customizable GraphQL Server. In the past the awesome Prisma team took on that great challenge and now we are happy to announce that we are continuing them and just released GraphQL Yoga 2.0 - Build fast, extensible, and batteries-included (Subscriptions, Serverless, File uploads support) GraphQL APIs in Node.js 🚀 Regarding this issue, I would also recommend to check out GraphQL Mesh and it's Mongo Handler We have been working a long time on version 2.0 and have been using it in our clients projects for a few months now and shared a couple of alpha cycles here. Please try Yoga out again, give us feedback and help us spread the word on the new release! |
How can I connect my existing MongoDB database to graphql-yoga? Thanks in advance!
The text was updated successfully, but these errors were encountered: