Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maarcingebala/veggies-graphql
Demo GraphQL app in Apollo Server and Typescript.
https://github.com/maarcingebala/veggies-graphql
apollo-server graphql typescript
Last synced: about 1 month ago
JSON representation
Demo GraphQL app in Apollo Server and Typescript.
- Host: GitHub
- URL: https://github.com/maarcingebala/veggies-graphql
- Owner: maarcingebala
- Created: 2020-10-04T14:41:55.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-05T07:28:16.000Z (over 4 years ago)
- Last Synced: 2024-10-05T17:41:23.239Z (3 months ago)
- Topics: apollo-server, graphql, typescript
- Language: TypeScript
- Homepage:
- Size: 160 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Demo GraphQL API in Apollo Server and Typescript.
# Installation
The project was built and tested with Node.js 14.13.0 and Typescript 4.0.3.
1. Install dependencies:
```shell
$ npm i
```2. Build the project:
```shell
$ npm run build
```3. Start the server:
```shell
$ npm start
```4. Application should be running on `http://localhost:4000/`.
# Usage
## Authentication
There are three users predefined in the application:
- [email protected] - can view and manage fruits and vegetables.
- [email protected] - can view and manage fruits; can view and create vegetables; cannot update and delete vegetables.
- [email protected] - can view and manage vegetables; can view and create fruits; cannot update and delete fruits.To authenticate as one of these users run the `tokenCreate` mutation (all users have password `secret`):
```graphql
mutation {
tokenCreate(email: "[email protected]", password: "secret") {
user {
permissions
}
token
errors {
message
}
}
}
```Response:
```json
{
"data": {
"tokenCreate": {
"user": {
"email": "[email protected]",
"permissions": ["MANAGE_FRUITS", "MANAGE_VEGETABLES"]
},
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImFkbWluQGV4YW1wbGUuY29tIiwiaWF0IjoxNjAxODMyMjkxfQ.FXUMtI4TrCpwpPr1ERvjPNaiBUW2ewTiD3mUG8LgVcU",
"errors": []
}
}
}
```Use the returned `token` to authenticate subsequent requests as this user by providing the Authorization header in this format:
```json
{
"Authorization": "JWT token"
}
```You can test if you're authenticated by running the query to return user data:
```graphql
{
me {
permissions
}
}
```## Queries
Get list of fruits:
```graphql
{
fruits {
id
name
}
}
```Get a single fruit by ID:
```graphql
{
fruit(id: 5) {
id
name
}
}
```Use similar queries `vegetables` and `vegetable` to get vegetables data.
# Mutations
Create a new fruit:
```graphql
mutation {
createFruit(input: { name: "Banana" }) {
fruit {
id
name
}
}
}
```Update a fruit:
```graphql
mutation {
updateFruit(id: "5", input: { name: "Orange" }) {
fruit {
id
name
}
}
}
```Delete a fruit:
```graphql
mutation {
deleteFruit(id: "5") {
success
}
}
```Use similar mutations `createVegetable`, `updateVegetable` and `deleteVegetable` to manage vegetables.