1. Install the necessary packages:
First, you need to install `express`, `express-graphql`, `graphql` packages for your project. To do this, navigate to your project folder and run the following commands: \`\`\` npm install express express-graphql graphql \`\`\`1. Set up the server:
Create a new file called `server.js`. \`\`\`javascript const express = require(‘express’); const graphqlHTTP = require(‘express-graphql’); const { buildSchema } = require(‘graphql’); // Construct a schema const schema = buildSchema(\` type Query { hello: String } \`); // Define root resolver const root = { hello: () => ‘Hello world!‘ }; const app = express(); app.use(‘/graphql’, graphqlHTTP({ schema: schema, rootValue: root, graphiql: true, // enable GraphiQL GUI })); app.listen(4000, () => console.log(‘Now browse to localhost:4000/graphql’)); \`\`\`1. Run the server:
Start your GraphQL server by running the following command in your terminal: \`\`\` node server.js \`\`\` You should see the message “Now browse to localhost:4000/graphql” in your console.1. Use GraphiQL:
Navigate to `localhost:4000/graphql` in your browser, you’ll see GraphiQL, an in-browser tool you can use to test your GraphQL server. Enter the following query in the left pane of the interface: \`\`\`graphql { hello } \`\`\` Hit the “Run” button (represented with a Play symbol), you should see the following response on the right pane: \`\`\`json { “data”: { “hello”: “Hello world!“ } } \`\`\`This is a very basic example. Real-world GraphQL server would have more complex schema with various types and resolvers.
Remember that designing a GraphQL schema and its corresponding resolvers is the most important part of building a GraphQL service. Therefore, you would need to adapt this approach according to your specific needs.