DEV Community

Cover image for GraphQL - Part 1
Shubham Tiwari
Shubham Tiwari

Posted on

GraphQL - Part 1

Hello Everyone, Today i will be showing you what is graphql and how you can get started with it.

GraphQL

GraphQL is an open-source query language and runtime for APIs (Application Programming Interfaces) developed by Facebook. It provides a more efficient and flexible alternative to traditional RESTful APIs for fetching and manipulating data.

With GraphQL, instead of making multiple requests to different endpoints to retrieve different sets of data, you can send a single request to a GraphQL server, specifying exactly what data you need. The server then responds with a JSON object containing the requested data, structured according to the shape of the query.

Here are some key features and concepts of GraphQL:

  • Strongly Typed: GraphQL has a type system that allows you to define the structure of your data and specify what fields can be queried.

  • Efficient and Flexible Queries: Clients can ask for specific data they need, eliminating over-fetching or under-fetching of data. The response from the server matches the structure of the query, providing only the requested data.

  • Single Endpoint: GraphQL has a single endpoint, typically /graphql, where all the queries and mutations are sent. This eliminates the need for multiple endpoints and simplifies the API surface.

  • Hierarchical and Nested Structure: GraphQL queries can have a hierarchical and nested structure, allowing you to traverse related objects and retrieve data in a nested manner.

  • Strong Relationships: GraphQL can handle relationships between objects efficiently. It supports querying related objects, filtering, sorting, and pagination.

  • Mutations: In addition to querying data, GraphQL also supports mutations for creating, updating, and deleting data on the server.

  • Introspection: GraphQL provides introspection capabilities, allowing clients to query the schema itself to understand the available types, fields, and operations.

  • Subscriptions: GraphQL supports real-time updates through subscriptions. Clients can subscribe to specific events or data changes and receive updates in real-time.

To get started with GraphQL, create a folder and run these commands -

npm init
Enter fullscreen mode Exit fullscreen mode
npm i apollo-server graphql lodash
Enter fullscreen mode Exit fullscreen mode
  • This will install the required packages for creating a server, creating graphql instance and lodash for manipulating array data.
npm i --save-dev nodemon
Enter fullscreen mode Exit fullscreen mode
  • Nodemon to run the server continuously on refresh or updates in code.

After installing the required packages, create a file called index.js and paste this code

// index.js
const { ApolloServer }  = require("apollo-server")
const {typeDefs} = require("./schema/type-defs")
const {resolvers} = require("./schema/resolvers")

const server = new ApolloServer({typeDefs,resolvers});

server.listen().then(({url}) => {
    console.log(`server running at ${url}`)
})
Enter fullscreen mode Exit fullscreen mode
  • This will create a new Apollo server instance that will run on localhost:4000 port.
  • This is the basic setup for the GraphQL server.
  • For now, if you do npm run start, it will show an error as we didn't create typeDefs and resolvers, which we will discuss in the next part.

CREDITS - https://www.youtube.com/results?search_query=pedrotech

THANK YOU FOR CHECKING THIS POST
You can contact me on -
Instagram - https://www.instagram.com/supremacism__shubh/
LinkedIn - https://www.linkedin.com/in/shubham-tiwari-b7544b193/
Email - shubhmtiwri00@gmail.com

^^You can help me with some donations at the link below Thank you👇👇 ^^
☕ --> https://www.buymeacoffee.com/waaduheck <--

Also check these posts as well
https://dev.to/shubhamtiwari909/website-components-you-should-know-25nm

https://dev.to/shubhamtiwari909/smooth-scrolling-with-js-n56

https://dev.to/shubhamtiwari909/swiperjs-3802

https://dev.to/shubhamtiwari909/custom-tabs-with-sass-and-javascript-4dej

Top comments (0)