Client
Serveur
PSQL
Express
GraphQL
Apollo Client
React Native / Redux
Request (String)
Response
(JSON)
GET /users/1 and GET /users/1/friends
GET /users/1?include=friends.username
Le front n'a pas forcément besoin de toutes les informations renvoyées
Or
// Réponse
{
"user": {
"id": 1,
"email": "son-goku@capsule-corp.com",
"username": "SonGoku",
"powerLevel": 1500000000,
"friends": [
{
"id": 2,
"username": "Piccolo",
"email": "piccolo@deamon.com",
"powerLevel": 18000,
},
{
"id": 5,
"username": "SonGoten",
"email": "son-goten@edu-univ.com",
"powerLevel": 1200000000,
}
]
}
}
// Requête
{
user(id: 1) {
email
username
friends {
id
username
}
}
}
// Réponse
{
"data": {
"user": {
"email": "son-goku@capsule-corp.com",
"username": "SonGoku",
"friends": [
{
"id": 2,
"username": "Piccolo"
},
{
"id": 5,
"username": "SonGoten"
}
]
}
}
}
Exemple de Schéma : https://github.com/xavierlefevre/chat/blob/master/server/data/schema.js
Exemple de Resolvers : https://github.com/xavierlefevre/chat/blob/master/server/data/resolvers.js
// Query
{
user(id: 1) {
email
username
friends {
id
username
}
}
}
Exemple de Query : https://github.com/xavierlefevre/chat/blob/master/client/src/graphql/groups/group.query.js
// Mutation
mutation createMessage {
createMessage(text: "kamehameha", groupId: 1) {
id
to {
id
}
from {
id
username
}
createdAt
text
}
}
Exemple de Mutation : https://github.com/xavierlefevre/chat/blob/master/client/src/graphql/groups/create-group.mutation.js
// Subscription
subscription onGroupAdded {
groupAdded(userId: 1){
id
name
messages(limit: 1) {
id
to {
id
}
from {
id
username
}
createdAt
text
}
}
}
Exemple de Souscription : https://github.com/xavierlefevre/chat/blob/master/client/src/graphql/groups/group-added.subscription.js