How to Install Express GraphQL Server on ECS

Solution Background

Introduction of GraphQL

Solution Process

  • sudo apt install nodejs
  • sudo apt install npm
  • cd /home
  • mkdir graphql-server-turtorial
  • cd graphql-server-turtorial
  • npm install express — save
  • cd node_modules: then we can see it install all the dependencies automatically.
  • mkdir server
  • touch server/server.js
  • within Server.js:
  • Const express = require('express'); const app = express(); app.get('/', (req, res) => { res.status(200).send({ message: 'connection to api server' }); }); app.listen(3000, () => { console.log('API server listen on port 3000'); });
  • then start Express Server: node server/server.js
  • at last curl server: curl http://localhost:3000/graph?spm=a2c41.12662902.0.0.49196ca0A4urWE
  • npm install graphql express-graphql — save
  • within server.js:
  • Const graphHttp = require ('express-graphql'); app.use( '/graphql', graphqlHTTP({ schema: GraphQLSchema, graphiql: true }) );
  • mkdir schema
  • touch schema/schema.js
  • Within in schema.js
  • const GraphQL = require('graphql'); const schema = new GraphQL.GraphQLSchema({ query: {}, mutation: {} }); exports.default = schema;
  • Within server.js, we also need to import schema we just created.
  • Const GraphQLSchema = require ('./schema/schema').default;
  • Within schema.js:
  • const schema = new GraphQL.GraphQLSchema( { query: new GraphQL.GraphQLObjectType({ name: 'Query', fields: () => ({ user: { type: User, resolve(arg) { return { name: 'Whien', age: 18 }; } } }) }) }); const User = new GraphQL.GraphQLObjectType({ name: 'User', fields: () => ({ name: { type: GraphQL.GraphQLString }, age: { type: GraphQL.GraphQLInt } }) });
  • curl -X POST http://localhost:3000/graph -d ‘{“query”: “query User{ user { name age } }”}’ -H “content-type: application/json”

Final Result

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Meet the team: Design and Photography

Why Nobody is Reading Your Software Engineering Resume

Code on laptop

The Alt-Ergo SMT Solver’s results in the SMT-COMP 2019

An introduction to hash tables with Python

Updating SSL Certs on Ubuntu!

Effectively Overcome Python’s Shortcomings with Go+

Logstash for ModSecurity audit logs

Scramble String-Coding question asked by Microsoft, Google, Salesforce, Meta

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

SECURE MongoDB UBUNTU SERVER!*

Simplified implementation of access and refresh tokens on a Node Express MongoDB backend server

Hosting Nodejs App using your own Docker Image in Jelastic PaaS/ Yeti Cloud