Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: GraphQL  (Read 11476 times)
Candy M.
Posts: 139


« on: February 25, 2021, 08:52:34 pm »

Does FourJs support GraphQL?  https://graphql.org/

If not, does it plan to?   

We are integrating our MasterTools ERP package with Shopify and this is another method of communicating besides using REST.  The JSON sent is in the request is a little bit different and I wasn't sure if I could create json in the GraphQL format.

Thanks!
Candy
Reuben B.
Four Js
Posts: 1119


« Reply #1 on: March 02, 2021, 12:33:28 am »

Candy,

You should still be able to use the low-level HTTP classes in Genero.  I have attached some code that does the equivalent of the call here https://shopify.dev/tools/graphiql-storefront-api

(I won't put the Access Token in clear text, if you run their demo in your browser and use Dev Tools you can find it amongst the headers sent, and add it to your fglprofile as shopify.storefront.access.token = "<access token goes here>")

I also have an example using https://github.com/trevorblades/countries if you want something without an access token.

In general if you can see an example using curl then you should be able to make the same requests using our low-level HTTP classes.

Reuben

{code}
IMPORT com
IMPORT util

MAIN
    DEFINE req com.HttpRequest
    DEFINE resp com.HttpResponse

    DEFINE q, r STRING

    -- Type derived from use of proposeType
    TYPE responseType RECORD
        data RECORD
            shop RECORD
                name STRING
            END RECORD
        END RECORD
    END RECORD
    DEFINE response responseType

    LET req = com.HttpRequest.Create('https://graphql.myshopify.com/api/2021-01/graphql.json')

    CALL req.setHeader("Content-Type", "application/json")
    CALL req.setHeader("Accept", "application/json")
    CALL req.setHeader("X-Shopify-Storefront-Access-Token", fgl_getresource("shopify.storefront.access.token"))
    CALL req.setMethod("POST")

    LET q = '{"query":"{shop {name}}"}'
    CALL req.doTextRequest(q)
    LET resp = req.getResponse()

    DISPLAY resp.getStatusCode()
    IF resp.getStatusCode() = 200 THEN
        LET r = resp.getTextResponse()
        DISPLAY r
        --DISPLAY util.JSON.proposeType(r) -- Use this to determine record structure for j
        CALL util.JSON.parse(r, response)
        DISPLAY response.data.shop.name
    END IF
END MAIN
{code}

Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Candy M.
Posts: 139


« Reply #2 on: March 03, 2021, 07:16:04 pm »

Thank you so much Reuben.   I appreciate this.  I'm going to play with some of the GraphQL today as I have the Rest Api working.   I guess I was wondering if Four Js would have a method to create the JSON in the text request (variable q) in your example.   But we can certainly create it by hand, just be sure about the syntax.

Candy
Candy M.
Posts: 139


« Reply #3 on: March 04, 2021, 12:11:38 am »

Reuben,
I was successful making a GraphQL call to our customer's store, so now I just need to figure out our queries or possibly REST endpoints.

Candy
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines