Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 4, 2023 08:41 am GMT

GraphQL VS API Graph with 2 same examples

GraphQL is all about asking the API for what exactly I need rather than it gives me the whole data, but introduced new concepts like resolvers and mutations and changes the whole way you use to write your APIs.

api-graph is also all about asking the API for what exactly I require rather than it gives me the complete data, but without changing anything that you use to write.

We will go throughout to two examples that do the same thing, the first one uses GraphQL and the second uses api-graph.

It's a simple example that don't use any database, all the data are stored in memory. The two examples have an array of warriors data that includes id and name then we will try to extract one of those two fields but the two libraries GraphQL and api-graph.

GraphQL example

this example is from digitalocean

import express from "express";import cors from "cors";import { graphqlHTTP } from "express-graphql";import { makeExecutableSchema } from "@graphql-tools/schema";const app = express();const port = 4000;// In-memory data storeconst data = {  warriors: [    { id: "001", name: "Ahmed" },    { id: "002", name: "Abdalla" },  ],};// Schemaconst typeDefs = `type Warrior {  id: ID!  name: String!}type Query {  warriors: [Warrior]}`;// Resolver for warriorsconst resolvers = {  Query: {    warriors: (obj, args, context) => context.warriors,  },};const executableSchema = makeExecutableSchema({  typeDefs,  resolvers,});app.use(cors());app.use(express.json());app.use(express.urlencoded({ extended: true }));// Entrypointapp.use(  "/graphql",  graphqlHTTP({    schema: executableSchema,    context: data,    graphiql: true,  }));app.listen(port, () => {  console.log(`Running a server at http://localhost:${port}`);});

api-graph example

import express from "express";import cors from "cors";import apiGraph from "api-graph";const app = express();const port = 4000;// In-memory data storeconst warriors = [  { id: "001", name: "Ahmed" },  { id: "002", name: "Abdalla" },];app.use(cors());app.use(express.json());app.use(express.urlencoded({ extended: true }));app.use(apiGraph({ extract: "query" }));app.post("/api-graph", (req, res) => res.json({ warriors }));app.listen(port, () => {  console.log(`Running a server at http://localhost:${port}`);});

Differences

  • api-graph saves the way you use to write apis and GraphQL uses resolvers, mutations and changes every thing.
  • The memory cost of import in GraphQL is height, while in api it's low.

graphql cost of import

api-graph cost of import

  • GraphQL More writing while api-graph less writing
  • GraphQL { warriors { id } } api-graph { warriors : [ { id } ] } GraphQL use graph query language, but api-graph uses string JSON without values.
  • GraphQL has a playground that you can send request from it in the development process, which means it has documents for API, api-graph don't have.

Original Link: https://dev.to/ahmedsudani/graphql-vs-api-graph-with-2-examples-34f6

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To