Cover image for Create a simple GraphQL server using typescript

Create a simple GraphQL server using typescript

thetinygoat profile image Sachin Saini ・2 min read

What is GraphQL

From the official website -> GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

But what does it mean ?

Simply put, graphQL is a query language, like SQL, which helps query the data stored in our graph. I won't go into the details about what a graph is but basically it is a data structure, if you need more details about graph there are several books and youtube videos on graphs and graph theory.

Getting Started

We will be working with Node.js and express-graphql and Typescript in this tutorial.

  • let's install the dependencies first npm install express express-graphql graphql
  • Let's install the types npm install -D @types/graphql @types/express @types/node

Build basic a Server

Alt Text

We imported graphqlHTTP from express-graphql and pass it as a middleware to our express app. The graphiql: true option enables the interactive playground for our graphql server. Now if you run the server there will be an error let's fix that.

Add a Schema

Alt Text
we imported buildSchema from graphql to construct our schema. Then we created the hello query. Now let's create the resolvers.

Add resolver

Alt Text
we created the resolvers object and added a hello function, the name of this function must match our query.
Now you can head on to http://localhost:4000/graphql and run your query.

Alt Text

Thanks for reading :)

Posted on by:

thetinygoat profile

Sachin Saini


Currently a student, love to post about all things tech.


markdown guide