Thursday, January 4, 2024

Graph QL Simple working example

Here's a simple working tutorial on creating a GraphQL server using Node.js and Express, with a basic query and mutation. We'll be using the Apollo Server Express library to create the GraphQL server.

  1. Install Node.js: If you don't have Node.js installed, download and install it from the official website:
  1. Create a new project folder: Create a new folder for your project and navigate to it in your terminal or command prompt.
  1. Initialize the project: Run the following command to initialize a new Node.js project:

npm init -y

  1. Install required packages: Install Express, Apollo Server Express, and GraphQL using the following command:

npm install express apollo-server-express graphql

  1. Create the GraphQL schema: Create a file named schema.graphql in your project folder and add the following schema definition:

type Query { hello: String } type Mutation { setMessage(message: String!): String }

Here we define a simple query hello that returns a string and a mutation setMessage that takes a string as input and returns a string.

  1. Create the server: Create a file named index.js in your project folder and add the following code:

const express = require("express"); const { ApolloServer, gql } = require("apollo-server-express"); const fs = require("fs"); // Read the schema from the schema.graphql file const typeDefs = gql(fs.readFileSync("schema.graphql", "utf8")); // Set up the resolvers const resolvers = { Query: { hello: () => "Hello, world!", }, Mutation: { setMessage: (_, { message }) => message, }, }; // Create the Apollo Server const server = new ApolloServer({ typeDefs, resolvers }); // Create the Express app const app = express(); // Apply the Apollo middleware to the Express app server.applyMiddleware({ app }); // Start the server const PORT = process.env.PORT || 4000; app.listen(PORT, () => console.log(`GraphQL server running at http://localhost:${PORT}${server.graphqlPath}`) );

In this code, we create a simple Express app and an Apollo Server with the schema and resolvers. The hello query returns a static string, and the setMessage mutation returns the input message.

  1. Start the server: Run the following command in your terminal or command prompt:

node index.js

Your GraphQL server should now be running at http://localhost:4000/graphql.

  1. Test your GraphQL server: Open a web browser and navigate to http://localhost:4000/graphql. You should see the GraphQL Playground interface. Run the following query and mutation in the playground:

query { hello } mutation { setMessage(message: "This is a test message!") }

You should see the appropriate responses for the query and mutation.

That's it! You now have a simple working GraphQL server using Node.js, Express, and Apollo Server Express. You can expand the schema and resolvers to handle more complex data and operations as needed.

No comments:

Post a Comment

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > <...


Search This Blog

Popular Posts