Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/defless/graphql-demo
A GraphQL demo for technological watch.
https://github.com/defless/graphql-demo
api express graphql nodejs school-project
Last synced: 10 days ago
JSON representation
A GraphQL demo for technological watch.
- Host: GitHub
- URL: https://github.com/defless/graphql-demo
- Owner: defless
- Created: 2021-11-05T08:19:30.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-05T13:42:01.000Z (about 3 years ago)
- Last Synced: 2024-10-12T10:26:04.327Z (2 months ago)
- Topics: api, express, graphql, nodejs, school-project
- Language: JavaScript
- Homepage:
- Size: 33.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GraphQL-demo 📊
>The way you write the query is the way you get the data
A [GraphQL](https://graphql.org/) demo for technological watch.
Install dependencies
```bash
yarn install
```Start the server
```bash
yarn start
```then go to http://localhost:4000/graphql
#### GraphQL logic items:
GraphQL schema
```
type Address {
street: String
city: City
}type User {
id: ID
name: String
age: Int
friends: [User]
address: Address
}type Query {
users: [User]
user(id: Int): User
}
```GraphQL resolver
```js
const root = {
user: ({id}) => {
return users[id];
},
users: () => {
return users;
},
}
```**Get all the informations in one request with query**:
You can now try your very first GraphQL query :
```
query{
users {
name
age
}
}
```
it allows to get all the users and their name & age.To get all the users informations and their subsequent fields you can do so:
```
query{
users {
name
age
address {
city {
name
code
inhabitants
}
street
}
}
}
```Let's assume now that you want the data of only one specific user with the the id `0`
```
query{
user(id: 0) {
name
age
address {
city {
name
code
inhabitants
}
street
}
}
}
```here you can see that the user resolver is different from the users one. They do a different job but still on the same API endpoint.
This way, we could also get the cities with the corresponding resolver.**Post informations with mutation**:
```
mutation{
newUser(name: "Name", age: 45) {
id
name
age
}
}
```It calls the resolver 'newUser', add the user to our list and then return this user to the specified format.