Skip to content
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

Closed
rainwater11 opened this issue Apr 22, 2018 · 17 comments
Closed

Connect MongoDB to graphql-yoga #277

rainwater11 opened this issue Apr 22, 2018 · 17 comments

Comments

@rainwater11
Copy link

How can I connect my existing MongoDB database to graphql-yoga? Thanks in advance!

@JorgeCeja
Copy link

JorgeCeja commented Apr 23, 2018

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

@marktani
Copy link
Contributor

Thanks for bringing this up, @rainwater11 🙂
The upcoming MongoDB connector for Prisma provides a convenient and powerful way to connect a MongoDB to GraphQL Yoga.

We plan to start a preview for the MongoDB connector later this month. Please reach out to me in Slack (@nilan) if you're interested in participating.

@fabien0102
Copy link
Contributor

@rainwater11 You just need to do your classic mongodb connection and give the result to the context. After you can retrieve your instance to any resolvers 😃

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

@JorgeCeja
Copy link

@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.

@marktani
Copy link
Contributor

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 🙂

@0xDaksh
Copy link
Contributor

0xDaksh commented May 12, 2018

@rainwater11 I'll make an example for it.

@nikitowsky
Copy link

nikitowsky commented May 15, 2018

Hey guys! I have an architecture question about using graphql-yoga with mongodb:
Should I pass db as context

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 Todo and use it like that

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?

@0xDaksh
Copy link
Contributor

0xDaksh commented May 15, 2018

@borisowsky As i said earlier, I'll make an example for it

@fabien0102
Copy link
Contributor

@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

@Menogus
Copy link

Menogus commented May 25, 2018

please give example for graphql Subscription with mongoose

@0xDaksh
Copy link
Contributor

0xDaksh commented May 25, 2018

@Menogus ya sorry, i was busy this week. But the long awaited weekend has come. I'll make it today.

@0xDaksh
Copy link
Contributor

0xDaksh commented May 25, 2018

Just made the pull request, the example will be in the examples/mongodb directory.

@Menogus
Copy link

Menogus commented May 29, 2018

Subscription not in examples :((

@0xDaksh
Copy link
Contributor

0xDaksh commented May 29, 2018

@Menogus I'll add 👍

@Menogus
Copy link

Menogus commented Jun 14, 2018

so....what about example for graphql Subscription with mongoose ?:))

@pratiksyngenta
Copy link

{"message": "Endpoint request timed out"} here while using serverless

@Urigo
Copy link
Collaborator

Urigo commented Mar 29, 2022

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.
Thank you all for your feedback and suggestions, you made this release possible!

Please try Yoga out again, give us feedback and help us spread the word on the new release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants