DEV Community

loading...
Cover image for Beginners Guide to communication with GraphQL Server in Javascript with Pokemon Schema
GraphQL Editor

Beginners Guide to communication with GraphQL Server in Javascript with Pokemon Schema

aexol profile image Artur Czemiel Updated on ・2 min read

First of all, I want to tell you I created the tool graphql-zeus it is GraphQL client on top of the fetch function. What it means? Basically, you point Zeus to the GraphQL schema and it generates libraries for you.

Sounds fun? It is even more fun because you don't have to know gql the query language of GraphQL because Zeus provides you with its own GraphQL query like autocompleted syntax.

Create a project folder:

mkdir zeus-tutorial
cd zeus-tutorial
Enter fullscreen mode Exit fullscreen mode

Let's start then. First, you will have to init a new npm package:

npm init
Enter fullscreen mode Exit fullscreen mode

click enter enter enter etc.

Install dev dependencies.

npm i -D @babel/core @babel/node @babel/preset-env
Enter fullscreen mode Exit fullscreen mode

Install dependencies.

npm i node-fetch
Enter fullscreen mode Exit fullscreen mode

Then Create .babelrc file

echo '{ "presets": ["@babel/preset-env"] }' >> .babelrc
Enter fullscreen mode Exit fullscreen mode

Then Create the src directory

mkdir src
Enter fullscreen mode Exit fullscreen mode

and create a index.js file

touch src/index.js
Enter fullscreen mode Exit fullscreen mode

Add script to your package.json

{
  "scripts": {
    "start": "babel-node src/index.js"
  },
}
Enter fullscreen mode Exit fullscreen mode

Your whole package.json should look like this:

{
  "name": "zeustutorial",
  "version": "1.0.0",
  "description": "",
  "main": "main/index.js",
  "scripts": {
    "start": "babel-node src/index.js"
  },
  "author": "Aexol <aexol@aexol.com> (http://aexol.com)",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.5.5",
    "@babel/node": "^7.5.5",
    "@babel/preset-env": "^7.5.5"
  },
  "dependencies": {
    "node-fetch": "^2.6.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now it is high time to generate some code from GraphQL. Go ahead install graphql-zeus.

install graphql-zeus

npm i -g graphql-zeus
Enter fullscreen mode Exit fullscreen mode

Generate files from pokemon schema

zeus https://graphql-pokemon.now.sh/ ./src
Enter fullscreen mode Exit fullscreen mode

Hurray! You should have definition files generated in ./src folder.
Now go and open some editor of choice - I prefer VSCode but it is up to you. I can guarantee it works with VSCode though.

Open package directory with editor. Open src/index.js
In the first part of the series, we will write simple query loading first ten pokemon names and images and display it in the terminal.

import { Chain } from "./graphql-zeus";

const chain = Chain("https://graphql-pokemon.now.sh");

const run = async () => {
  const { pokemons } = await chain.Query({
    pokemons: [
      {
        first: 10
      },
      {
        name: true,
        image: true
      }
    ]
  });
  console.log(pokemons);
  return pokemons;
};
run();

Enter fullscreen mode Exit fullscreen mode

In zeus everything is typed so when you write chain. You should see Query and when you open {} parentheses you should see all the possible queries.

In zeus everything is autocompleted so you don't have to learn gql syntax.

And run it with being in the project folder

npm run start
Enter fullscreen mode Exit fullscreen mode

You should see the first ten pokemon in the output! Congratulations you've just done your first GraphQL query.

Support

If you want to support me creating graphql-zeus visit

GitHub logo graphql-editor / graphql-zeus

GraphQL client and GraphQL code generator with GraphQL autocomplete library generation ⚡⚡⚡ for browser,nodejs and react native

npm Commitizen friendly npm downloads

GraphQL Zeus creates autocomplete client library for JavaScript or TypeScript which provides autocompletion for strongly typed queries.

From version 2.0 Zeus support mapped types !!!

Supported Languages:

  • Javascript
    • Browser
    • NodeJS
    • React Native
  • TypeScript
    • Browser
    • NodeJS
    • React Native

How it works

Given the following schema Olympus Cards

Table of contents

License

MIT

How to use

Main usage of graphql zeus should be as a CLI.

As

and leave a star. That's it.

Discussion

pic
Editor guide
Collapse
carlillo profile image
Carlos Caballero

Good topic 🐒