Is there any example with GraphQL?

The 1C:Enterprise developers forum

#1
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

Hello,
I want to make an API connection with an online store. That site uses GraphQL instead of RestAPI. Is there any example with 1C?

 
#2
People who like this:0Yes/0No
Administrator
Rating: 23
Joined: Oct 3, 2019
Company:

Unfortunately, the GraphQL functionality is not yet supported by the 1C platform.

 
#3
People who like this:0Yes/0No
Just came
Rating: 1
Joined: Jan 17, 2023
Company: Techatom Pvt Ltd

Well, connecting to an online store that uses GraphQL involves understanding the schema, choosing a GraphQL client library, constructing and executing queries, handling responses, and potentially implementing mutations. With the right tools and approach, you can effectively integrate with the online store's GraphQL API.
Thanks

 
#4
People who like this:0Yes/0No
Administrator
Rating: 23
Joined: Oct 3, 2019
Company:

Hello Eyup,

I'm sorry, I probably misled you.

There is no direct GraphQL support in 1C, but the developer can work with services that support this protocol using regular HTTP(S) anyway.

Please see the example below:

Code
textQuery = "{ ""query"": ""{ loggedInAccount { id name slug }}"" }";
   
   httpConnection = New HTTPConnection("api.opencollective.com/graphql/v2", 443, ,,,60, New OpenSSLSecureConnection);
   
   httpRequest = New HTTPRequest;
   
   httpRequest.SetBodyFromString(textQuery, TextEncoding.UTF8);
   
   httpRequest.Headers.Insert("Content-Type",   "application/json");
   httpRequest.Headers.Insert("Api-Key",      api_key);
   
   httpResponse = httpConnection.Post(httpRequest);
   
   If httpResponse.StatusCode = 200 Then
      
      JSONReader = New JSONReader;
       JSONReader.SetString(httpResponse.GetBodyAsString());
       structureResponse = ReadJSON(JSONReader, False);
      
   EndIf;


The result of this request is shown in the screenshot.

Also keep in mind that GraphQL queries need to be converted to JSON so that they can be transmitted over HTTP.

This service will help with this, for example: https://datafetcher.com/graphql-json-body-converter

take a look pls at the second screenshot

Download Pic_1.png (47.75 KB)
Download Pic_2.png (50.16 KB)
 
#5
People who like this:0Yes/0No
Active user
Rating: 4
Joined: Mar 6, 2018
Company: Bodega LLC

I asked this question for wayfair api connection. Today I found the way;

Code
Function Authentication() 
   Credentials = GetCredentials.WayFairCredentials();
   apiKey = Credentials.apiKey;
   apiSecret = Credentials.apiSecret;
   Data = New Structure;
   Data.Insert("grant_type","client_credentials");
   Data.Insert("client_id",apiKey);
   Data.Insert("client_secret",apiSecret);
   Data.Insert("audience","api.wayfair.com/");
   JSONWriter = New JSONWriter;   
   JSONWriter.SetString();   
   WriteJSON(JSONWriter, Data);
   result = JSONWriter.Close();
   
   //token = GetToken();
   
   Connection = New HTTPConnection("sso.auth.wayfair.com/oauth/token",,,,,,New OpenSSLSecureConnection());
   request = New HTTPRequest();
   request.SetBodyFromString(result);
   request.Headers.Insert("Content-type", "application/json");
   //  request.GetBodyAsString()
   HTTPAnswer = Connection.Post(request);
   res = HTTPAnswer.GetBodyAsString();
   
   jsonReader = New JSONReader;
   jsonReader.SetString(res);
   Dat = New Structure;
   Dat = ReadJSON(jsonReader);
   jsonReader.Close();
   
   return Dat;
   
EndFunction

Function GetOrders() Export
   auth_data = Authentication();
   access_token = auth_data.access_token;
   Auth = "Bearer " + access_token ;
   
   Connection = New HTTPConnection("api.wayfair.com/v1/graphql",,,,,,New OpenSSLSecureConnection());
   req ="";
   
   graphqlll = New Structure;   
   graphqlll.Insert("query","{getDropshipPurchaseOrders(limit:100,hasResponse:false,sortOrder:DESC){poNumber poDate estimatedShipDate customerName customerAddress1 customerAddress2 customerCity customerState customerPostalCode orderType shippingInfo{shipSpeed carrierCode} packingSlipUrl warehouse{id name} products{partNumber quantity price event{startDate endDate}} }}");
   graphql = WriteJSONValue(graphqlll);
   request = New HTTPRequest(req);
   request.Headers.Insert("Authorization",Auth);
   request.Headers.Insert("Content-type", "application/json");
   request.SetBodyFromString(graphql);
   HTTPAnswer = Connection.Post(request);
   res = HTTPAnswer.GetBodyAsString();

   
   jsonReader = New JSONReader;
   jsonReader.SetString(res);
   Dat = New Structure;
   Dat = ReadJSON(jsonReader);
   jsonReader.Close();
   return Dat;

EndFunction





This examples get orders from wayfair. Thank you Aleksandr Biryukov and Gulshan Negi.

 
#6
People who like this:0Yes/0No
Administrator
Rating: 23
Joined: Oct 3, 2019
Company:

Great! It's nice to know that our tips help you in your work! :-)

 
Subscribe
Users browsing this topic (guests: 1, registered: 0, hidden: 0)
Be the first to know tips & tricks on business application development!

A confirmation e-mail has been sent to the e-mail address you provided .

Click the link in the e-mail to confirm and activate the subscription.