https://github.com/tylerthebuildor/graphqless
REST and GraphQL really aren't that different. I'll prove it!
https://github.com/tylerthebuildor/graphqless
Last synced: 9 months ago
JSON representation
REST and GraphQL really aren't that different. I'll prove it!
- Host: GitHub
- URL: https://github.com/tylerthebuildor/graphqless
- Owner: tylerthebuildor
- Created: 2019-01-11T08:04:11.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-04T04:28:35.000Z (over 6 years ago)
- Last Synced: 2025-08-07T09:44:33.087Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 507 KB
- Stars: 287
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
- awesome-github-projects - graphqless - REST and GraphQL really aren't that different. I'll prove it! ⭐287 `JavaScript` (📦 Legacy & Inactive Projects)
README
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
```bash
yarn add graphqless
```
And here is how you write a server... Look familiar?
```jsx
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:
```jsx
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](/examples) folder.
## Examples
### Queries for examples/example
```bash
npx nodemon examples/example.js
```
```graphql
mutation createUser {
createUser(name: "Buchea")
}
query getUsers {
users {
name
}
user(name: "Tyler") {
name
}
}
```
### Queries for examples/exampleWithAuth
```bash
npx nodemon examples/exampleWithAuth.js
```
```graphql
# Add this to "HTTP HEADERS" in GraphQL Playground:
# { "Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.YWJj.4noRC-c0ay0hOeZ5Cgc80MVS0P4p4FrR2lJFzMNSnE4" }
query getMe {
getToken
me {
id
name
}
}
```
### Queries for examples/exampleWithRouter
```bash
npx nodemon examples/exampleWithRouter/index.js
```
```graphql
mutation createUser {
createUser(name: "Buchea")
}
query getUsers {
users {
name
}
user(name: "Tyler") {
name
}
}
```
### Queries for examples/exampleWithReactClient
```bash
npx nodemon examples/exampleWithReactClient/index.js
```
```graphql
mutation createUser {
createUser(name: "Buchea") {
name
}
}
query getUsers {
users {
name
}
}
```
### Queries for examples/exampleWithSubscription
```bash
npx nodemon examples/exampleWithSubscription.js
```
```graphql
subscription subscribeToCount {
count
}
query getDummyData {
dummy
}
```
### Queries for examples/exampleWithRelations
```bash
npx nodemon examples/exampleWithRelations.js
```
```graphql
query getDeepRelations {
users {
name
favorites {
name
user {
name
}
}
}
}
```