简体   繁体   中英

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

I know it is a very much a beginner question but I am struggling to grasp a few things when it comes to the MERN stack and GraphQL. There is this particular project on github where the web app is developed using MongoDB, Express, React and Nodejs along with GraphQL.

I do understand that MongoDB is used for data storage and React for the front end but I can't wrap my head around as for why Express and Nodejs is used if an API is created with GraphQL which POSTs and GETs data directly to/from the MongoDB database? What is the role and interconnection between nodejs, express and graphql?

This question might not make sense to you because I am missing the knowledge of basic concepts of web app development and understanding of web dev stacks such as MERN.

Node.js is a JavaScript runtime environment -- it's what actually executes all your server-side code. Express is a framework that provides basic features for developing a web application in Node.js. While Node.js is already capable of listening to requests on a port, Express makes it simpler to set up a web server by eliminating boilerplate and offering a simpler API for creating endpoints.

GraphQL is a query language. GraphQL.js is the JavaScript implementation of GraphQL. Neither is capable of creating an endpoint or web server. GraphQL itself doesn't listen to requests being made to a particular port. This is what we use Express for -- combined with another library like apollo-server-express or express-graphql , Express sets up our endpoint, listens for incoming requests to the endpoint, parses them and hands them over to GraphQL to execute. It then sends the execution result back to the client that made the request.

Similarly, GraphQL.js is not capable of directly interfacing with a database. GraphQL simply executes the code you provide in response to a request. The actual interaction with the database is typically done through a driver (like mongodb ) or an ORM (like mongoose ).

So a client (like your React application) makes a request to your Express application, which parses the request and hands it to GraphQL, which executes your query and in the process, calls some code that then gets data from your database. This data is formatted into a proper response and sent back to the client.

For a beginner, the missing project detail you are referencing is as follows:

  1. Used Node.js to create an environment for the API generation or running your code. GraphQL can't do this alone.
  2. Used Express for body parsing middleware, authentication middleware(it will authenticate every GraphQL request) and express-graphql for the integration of GraphQL with express framework(means graphQL API functions will be called after authentication middleware next() function trigger).
  3. GraphQL to create API that needs after auth middleware will call next() function.

So the project is working like the following:

  1. Mongoose is connected first.
  2. Node.js starts a server.
  3. When API calls to send to the server, then
    a) They are parsed with express bodyParser
    b) Headers are set on those requests.
    c) auth middleware call.
    d) Now it is the job of GraphQL to handle the API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM