Four Js Development Tools Forum

Discussions by product => Genero BDL => Topic started by: Candy M. on February 25, 2021, 08:52:34 pm



Title: GraphQL
Post by: Candy M. 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


Title: Re: GraphQL
Post by: Reuben B. 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}


Title: Re: GraphQL
Post by: Candy M. 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


Title: Re: GraphQL
Post by: Candy M. 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