PC SOFT

FORUMS PROFESSIONNELS
WINDEVWEBDEV et WINDEV Mobile

Accueil → WINDEV 2025 → Exemple d'utilisation GRAPHQL
Exemple d'utilisation GRAPHQL
Débuté par bichem93, 05 juin 2018 17:45 - 12 réponses
Posté le 05 juin 2018 - 17:45
Bonjour,
Je sais que j'ai un peu d'avance sur PCSoft, mais quelqu'un aurait un exemple d'utilisation concret d'appel à un serveur API GraphQL?
J'ai implémenter un serveur GraphQL sous node et pas de probleme pour mes appels sous GraphiQL en revanche j'ai tout essayer avec les fonction RestEnvoie en mettant soit des string soit un objet variant (avec variantversjson).
J'ai meme essayer de remplacer ContentType par "application/json"

Voici un extrait de mon code de test.
cMaRequete est un restRequête
sData est une chaîne
sData =[
{query{
human(id:7654){
username
firstname
lastname
userphone
}
}}
]
NouveauCli est un Buffer
vCli est un Variant
vCli.query = "blogTitle"

NouveauCli = VariantVersJSON(vCli)

cMaRequete..URL = "http://localhost:3000"
cMaRequete..Méthode = httpPost
cMaRequete..ContentType="application/graphql"

//cMaRequete..Contenu = NouveauCli
cMaRequete..Contenu = ChaîneVersUTF8(sData)
Info(cMaRequete..Contenu)
cMaReponse est un restRéponse = RESTEnvoie(cMaRequete)
SI ErreurDétectée ALORS
Erreur(ErreurInfo(errComplet))
SINON
Info("réponse: "+cMaReponse..Contenu)
FIN


Peux-etre qu'il ne faut pas que j'utilise les type RestRequete?
Si quelqu'un peut m'aider?
Merci
Membre enregistré
2 682 messages
Posté le 06 juin 2018 - 07:11
Bonjour,

Quel est le problème que tu rencontres ???

--
Cordialement,

Philippe SAINT-BERTIN
Posté le 06 juin 2018 - 09:54
Bonjour Phillipe,
Le problème que je rencontre est que dans le retour cMaRéponse..Contenu j'ai le message d'erreur suivant:
{"error":[{"message":"Must provide query string."}]}

Sur le serveur node graphQL je vois l'appel en statut 400 (bad request).

Aurais-tu une idée?

Merci
Membre enregistré
2 682 messages
Posté le 06 juin 2018 - 20:00
As-tu essayé de retirer le chaineversutf8 ? Je ne suis pas sur que tu doives envoyer de l'utf8

--
Cordialement,

Philippe SAINT-BERTIN
Posté le 06 juin 2018 - 20:31
Oui j'ai essayé. J'ai même essayé de déclarer la chaine au format ANSI. J'ai aussi essayé d'inclure le corps de ma requête dans un objet variant puis convertit par la fonction variantversjson.
Là je pense vraiment avoir fait toutes les combinaisons possibles.
Je pense que GraphQL sera une nouveauté Windev 24 ou Windev 25.
Un autre idée?
Membre enregistré
953 messages
Posté le 07 juin 2018 - 14:15
bonjour,

Je viens de tester GraphQL avec Node.JS et çà fonctionne pour l'instant avec des requêtes simple.
Mon serveur GraphQL est disponible sur http://localhost:4000/graphql chez moi, et voici le code Windev :

GraphQL_Query est une chaîne=[
{"query": "{ message }"}
]
GraphQL_Request est un restRequête
GraphQL_Request..URL = "http://localhost:4000/graphql"
GraphQL_Request..Méthode = httpPost
GraphQL_Request..ContentType="application/json"

GraphQL_Request..Contenu =ChaîneVersUTF8(GraphQL_Query)
GraphQL_Respons est un restRéponse = RESTEnvoie(GraphQL_Request)
SI ErreurDétectée ALORS
Erreur(ErreurInfo(errComplet))
SINON
Info("réponse: "+GraphQL_Respons..Contenu)
FIN



Voici le code de mon 'server.js' :
var express = require('express');
var express_graphql = require('express-graphql');
var { buildSchema } = require('graphql');
// GraphQL schema
var schema = buildSchema(`
type Query {
Message: string
}
`);
// Root resolver
var Root = {
Message: () => 'Hello World!'
};
// Create an express server and a GraphQL endpoint
var app = express();
app.use('/graphql', express_graphql({
schema: schema,
rootValue: Root,
graphiql: True
}));
app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
Membre enregistré
953 messages
Posté le 07 juin 2018 - 15:46
et voici pour une requête un peu plus complexe :

GraphQL_Query est une chaîne=[
{"query": "query getSingleCourse($courseID: Int!) {course(id: $courseID) {title\r\nauthor\r\ndescription\r\ntopic\r\nurl\r\n}}","variables": {"courseID": 1}}
]
GraphQL_Request est un restRequête
GraphQL_Request..URL = "http://localhost:4000/graphql"
GraphQL_Request..Méthode = httpPost
GraphQL_Request..ContentType="application/json"
Info(Taille(GraphQL_Query))

GraphQL_Request..Contenu =GraphQL_Query //ChaîneVersUTF8(s_graphql_query)
GraphQL_Respons est un restRéponse = RESTEnvoie(GraphQL_Request)
SI ErreurDétectée ALORS
Erreur(ErreurInfo(errComplet))
SINON
Info("réponse: "+GraphQL_Respons..Contenu)
FIN


avec le server2.js associé :
var express = require('express');
var express_graphql = require('express-graphql');
var { buildSchema } = require('graphql');
// GraphQL schema
var schema = buildSchema(`
type Query {
course(id: int!): Course
courses(topic: string): [Course]
},
type Course {
id: int
title: string
author: string
Description: string
topic: string
url: string
}
`);
var coursesData = [
{
id: 1,
title: 'The Complete Node.js Developer Course',
author: 'Andrew Mead, Rob Percival',
description: 'Learn Node.js by building real-world applications with Node, Express, MongoDB, Mocha, and more!',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs/'
},
{
id: 2,
title: 'Node.js, Express & MongoDB Dev to Deployment',
author: 'Brad Traversy',
description: 'Learn by example building & deploying real-world Node.js applications from absolute scratch',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs-express-mongodb/'
},
{
id: 3,
title: 'JavaScript: Understanding The Weird Parts',
author: 'Anthony Alicea',
description: 'An advanced JavaScript course for everyone! Scope, closures, prototypes, this, build your own framework, and more.',
topic: 'JavaScript',
url: 'https://codingthesmartway.com/courses/understand-javascript/'
}
]
var getCourse = FUNCTION(args) {
var id = args.id;
RETURN coursesData.filter(course => {
RETURN course.id == id;
})[0];
}
var getCourses = FUNCTION(args) {
IF (args.topic) {
var topic = args.topic;
RETURN coursesData.filter(course => course.topic === topic);
} ELSE {
RETURN coursesData;
}
}
var Root = {
course: getCourse,
courses: getCourses
};
// Create an express server and a GraphQL endpoint
var app = express();
app.use('/graphql', express_graphql({
schema: schema,
rootValue: Root,
graphiql: True
}));
app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
Membre enregistré
953 messages
Posté le 07 juin 2018 - 15:47
Membre enregistré
953 messages
Posté le 07 juin 2018 - 15:51
La partie "variables" quand la query correspond aux "QUERY VARIABLES" lorsque vous êtes sur l'interface graphique de GraphiQL




Posté le 08 juin 2018 - 00:57
Bonjour Christophe,
Je viens de tester avec la façon dont vous avez écrit votre requête et cela fonctionne.
Mon erreur était du à une mauvaise syntaxe de l’écriture de la requête sous windev.

Mille mercis Christophe, vous m'avez fait économiser un temps précieux.
Merci aussi à Philippe.
Posté le 03 février 2023 - 21:42
Hello everybody, anyone have an example to how to do mutations in graphql windev? i searched a loot about this issue but i didn't have lucky to search an example to do this. I know how to do an query and works fine, but the real problem is do the string of mutation, i trying with this code.


sXGraphQL_Query is string=[
{"mutation": "mutation Authenticate($data: AuthInput!) {authenticate(data: $data) { token \r }}","variables": {"data": { email: "example@example.com", password: "123"}}}
]

cMyrequest is a httpRequest
cMyrequest..Method = httpPost
cMyrequest..URL = gsUrl
cMyrequest.IgnoreError = httpIgnoreExpiredCertificate
cMyrequest..ContentType = "application/json"
cMyrequest..Content = StringToUTF8( sXGraphQL_Query )
cMyResponse is a httpResponse = RESTSend(cMyrequest)
Posté le 03 février 2023 - 21:48
Hello everybody, anyone have an example to how to do mutations in graphql windev? i searched a loot about this issue but i didn't have lucky to search an example to do this. I know how to do an query and works fine, but the real problem is do the string of mutation, i trying with this code.


sXGraphQL_Query is string=[
{"mutation": "mutation Authenticate($data: AuthInput!) {authenticate(data: $data) { token \r }}","variables": {"data": { email: "example@example.com", password: "123"}}}
]

cMyrequest is a httpRequest
cMyrequest..Method = httpPost
cMyrequest..URL = gsUrl
cMyrequest.IgnoreError = httpIgnoreExpiredCertificate
cMyrequest..ContentType = "application/json"
cMyrequest..Content = StringToUTF8( sXGraphQL_Query )
cMyResponse is a httpResponse = RESTSend(cMyrequest)
Membre enregistré
1 message
Posté le 03 février 2023 - 21:50
Hello everybody, anyone have an example to how to do mutations in graphql windev? i searched a loot about this issue but i didn't have lucky to search an example to do this. I know how to do an query and works fine, but the real problem is do the string of mutation, i trying with this code.

sXGraphQL_Query is string=[
{"mutation": "mutation Authenticate($data: AuthInput!) {authenticate(data: $data) { token \r }}","variables": {"data": { email: "example@example.com", password: "123"}}}
]
cMyrequest is a httpRequest
cMyrequest..Method = httpPost
cMyrequest..URL = gsUrl
cMyrequest.IgnoreError = httpIgnoreExpiredCertificate
cMyrequest..ContentType = "application/json"
cMyrequest..Content = StringToUTF8( sXGraphQL_Query )
cMyResponse is a httpResponse = RESTSend(cMyrequest)

IF ErrorOccurred THEN
Error(ErrorInfo(errFullDetails))
END