• Stars
    star
    292
  • Rank 142,152 (Top 3 %)
  • Language
    JavaScript
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

REST and GraphQL really aren't that different. I'll prove it!

GraphQLess

GraphQLess

βš›οΈ πŸš€πŸ€˜

REST and GraphQL really aren't that different. I'll prove it!
GraphQLess is a thin wrapper around the official apollo-server-express project.
GraphQLess lets you write a GraphQL server in an Express.js style.

Setup

yarn add graphqless

And here is how you write a server... Look familiar?

const { GraphQLess } = require('graphqless');
const app = new GraphQLess();

const db = { users: [{ name: 'Tyler' }] };

app.get('/users', (req, res) => {
  const { users } = db;
  res.send(users);
});

app.get('/user', (req, res) => {
  const user = db.users.find(user => user.name === req.body.name);
  res.send(user);
});

app.post('/createUser', (req, res) => {
  const userCount = db.users.push({ name: req.body.name });
  res.send(userCount);
});

app.listen(3000, () => {
  console.log('Visit: http://localhost:3000/playground');
});

I know it looks like Express.js but the code above is a GraphQL server! There is one caveat though...

GraphQL requires us to write a schema that describes the .get and .post functions' inputs and outputs.

Just know that .get === Query && .post === Mutation. Now let's modify the last few lines of the snippet above to include the required schema:

app
  .useSchema(
    `
    type Query {
      users: [User]
      user(name: String): User
    }
    type Mutation {
      createUser(name: String): Int
    }
    type User {
      name: String
    }
  `
  )
  .listen(3000, () => {
    console.log('Visit: http://localhost:3000/playground');
  });

That's the only catch! You now have a fully functioning and extendable GraphQL server.

You can find more examples in the examples folder.

Examples

Queries for examples/example

npx nodemon examples/example.js
mutation createUser {
  createUser(name: "Buchea")
}

query getUsers {
  users {
    name
  }
  user(name: "Tyler") {
    name
  }
}

Queries for examples/exampleWithAuth

npx nodemon examples/exampleWithAuth.js
# Add this to "HTTP HEADERS" in GraphQL Playground:
# { "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.YWJj.4noRC-c0ay0hOeZ5Cgc80MVS0P4p4FrR2lJFzMNSnE4" }

query getMe {
  getToken
  me {
    id
    name
  }
}

Queries for examples/exampleWithRouter

npx nodemon examples/exampleWithRouter/index.js
mutation createUser {
  createUser(name: "Buchea")
}

query getUsers {
  users {
    name
  }
  user(name: "Tyler") {
    name
  }
}

Queries for examples/exampleWithReactClient

npx nodemon examples/exampleWithReactClient/index.js
mutation createUser {
  createUser(name: "Buchea") {
    name
  }
}

query getUsers {
  users {
    name
  }
}

Queries for examples/exampleWithSubscription

npx nodemon examples/exampleWithSubscription.js
subscription subscribeToCount {
  count
}

query getDummyData {
  dummy
}

Queries for examples/exampleWithRelations

npx nodemon examples/exampleWithRelations.js
query getDeepRelations {
  users {
    name
    favorites {
      name
      user {
        name
      }
    }
  }
}

More Repositories

1

cordova-android-crosswalk

This command line tool is an easy way to migrate your existing Cordova based (including Ionic) projects over to use Crosswalks Chromium webview.
JavaScript
28
star
2

lure.js

A tiny educational MVC framework written in JavaScript
JavaScript
12
star
3

badass-react-snippets

Package for Github's Atom Editor. These snippets are made to work with the latest ES6 standards, adhere to the airbnb eslint config, and are React/React-Native agnostic.
CoffeeScript
12
star
4

docker-swarm-traefik

Shell
9
star
5

use-screen-recording

Easily create screen recordings using React Hooks.
JavaScript
8
star
6

my-simple-app

A dead simple React & Redux implementation!
JavaScript
7
star
7

freqtrade-now

Python
7
star
8

my-simple-async-app

JavaScript
7
star
9

react-native-context-api-example

Objective-C
4
star
10

fkxcode

JavaScript
4
star
11

react-apollo-with-hooks

HTML
3
star
12

mac-setup

3
star
13

graphql-buildschema-example

JavaScript
3
star
14

react-redux-webpack

JavaScript
2
star
15

codename-hopper

TypeScript
2
star
16

samjak

CoffeeScript
2
star
17

solana-nft-viewer

JavaScript
2
star
18

gitribute

Search Github repositories for TODO comments.
JavaScript
2
star
19

graphql-apollo-example

JavaScript
2
star
20

solana-react-starter

TypeScript
1
star
21

simple-graphql-and-react-example

JavaScript
1
star
22

react-native-apollo-example

JavaScript
1
star
23

headersfactory

Opinionated headers factory for easy use with fetch and other AJAX things too I guess.
Shell
1
star
24

ar-expo-cube

JavaScript
1
star
25

fioswificalculator

JavaScript
1
star
26

wedding

JavaScript
1
star
27

javascript-course-repo

1
star
28

gotellthat.com

JavaScript
1
star
29

shroomz-fast

HTML
1
star
30

git-commands

JavaScript
1
star
31

com.tylerbuchea.ontap

HTML
1
star