GraphQL VS API Graph with 2 same examples
Tags: graphql, javascript, api, apigraph
Published: 2023-04-04 08:41:18

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](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-graphql-api-server-in-node-js)

```
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 store
const data = {
  warriors: [
    { id: "001", name: "Ahmed" },
    { id: "002", name: "Abdalla" },
  ],
};

// Schema
const typeDefs = `
type Warrior {
  id: ID!
  name: String!
}

type Query {
  warriors: [Warrior]
}
`;

// Resolver for warriors
const 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 }));

// Entrypoint
app.use(
  "/graphql",
  graphqlHTTP({
    schema: executableSchema,
    context: data,
    graphiql: true,
  })
);

app.listen(port, () => {
  console.log(`Running a server at http://localhost:${port}`);
});
```
![GraphQL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cuqv6vl5llfitkuo3oez.png)

## 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 store
const data = {
  warriors: [
    { id: "001", name: "Jaime" },
    { id: "002", name: "Jorah" },
  ],
};

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(data));
app.listen(port, () => {
  console.log(`Running a server at http://localhost:${port}`);
});

```
![API Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g5ixitm3j4fwousrjbex.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/crf7ani34hsa5xssjrbu.png)

![api-graph cost of import](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4u08evw592i7s8s6476z.png)
- 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.