1.1 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			1.1 KiB
		
	
	
	
	
	
	
	
| title | categories | tags | |||
|---|---|---|---|---|---|
| Using GraphQL with Node.js | 
  | 
  | 
Using GraphQL with Node.js
Create a basic Express server
First we create a basic server in Node using Express:
import express from "express";
app.get("/", (req, res) => {
  res.send("Graph QL test project");
});
app.listen(8080, () =>
  console.log("Running server on port localhost:8080/graphql")
);
Add GraphQL as middlewear
Next we introduce GraphQL as a piece of Node.js
middlewear, with the
app.use() method.
import { graphqlHTTP } from "express-graphql";
app.use(
  "/graphql",
  graphqlHTTP({
    schema: schema,
    rootValue: resolvers,
    graphiql: true,
  })
);
schemais a reference to our GraphQL schema - the structure of the fields that define our server.rootValueis a reference to our resolvers.graphiqlis the GUI tool that will be served from the GraphQL endpoint atlocalhost:8080/graphql. This tool enables us to interrogate our data using the defined schema and see what data we would get back from frontend queries.