Question for the hive-mind: I’m doing work for a c...
# prisma-whats-new
m
Question for the hive-mind: I’m doing work for a client and would like to use graphcool for it. It would entail the production of a relatively simple API where by my client can locally generate random codes (using the javascript API I would provide, like
myapi.generate()
), those codes then get stored in my graphcool account, and at some point my client’s customers would then consume the codes (
myapi.destroy('DKns83l2dn39')
, at which point they’d get deleted. So far I’ve only worked with GraphQL in one and the same project. Do you know of any resources I could use graphcool for to “build” an API that my client could then use in various other places and websites? How would that even work?
m
sounds fairly straight forwards unless I'm missing something... you can build out a graphcool api endpoint in minutes and then layer on serverless functions to handle the code deletion
what kind of resources are you looking for? the graphcool docs are relatively good
m
So I’m a bit like a 5-year old when it comes to APIs and backend stuff. For example, how would I use the API on another website that doesn’t load Graph.cool’s API?
m
oh okay no worries - I only got started on this 3m ago... making the API with graphcool is the easy part. Adding permissions and authentication is harder.
But still easy compared to having to write your own backend server
m
Yeah, for sure. I’m thinking of creating a kind of SaaS, and that would entail me creating an API I could hand out, which would interface inside my graphcool account. Wrapping my mind on how to do such a thing.
m
The other website would need to incorporate something that can interact with the GraphQL API provided by Graph.cool. I would look at Relay or Apollo Client
I think most people us the Apollo Client which works with most popular JS frameworks as well as plain JS
Here's the vanilla JS link (if you're not using a framework): http://dev.apollodata.com/core/
m
Yeah, I’ve been using the Apollo Client in my own web apps. That’s not the problem. Just don’t know how to create a new API that I could hand out to my client, which in turn would then interface with the Graph.cool API inside my app.
m
oh
I misunderstood you
m
No worries, maybe I miscommunicated.
m
So you're looking for something like a RESTFUL API which would then hit your graphcool api?
Clients -> new API -> your Graphcool API
m
I guess? Something that’s super simple for my client to install himself.
m
how would they interact with it?
m
Like I give him a javascript file URL
<https://www.mywebsite.com/api/codegenerator.js>
and tell him to put this code
myapi.generate()
inside his javascript to generate a code, and
myapi.destroy('couponcode')
to destroy a coupon.
m
oh okay got it... in that case I'm probably not the best person to ask. I've only ever used GraphQL API
🙌🏼 1
m
Thanks!
m
good luck!
👊🏼 1
a
Ah, not API, but SDK basically
You want a Javascript 'library', that a user can put on his website, that contains two methods: myapi.generate() and myapi.destroy()
Something like this
Copy code
window.myapi = (function () {
         
    var myapi = {
        generate: function () {
            fetch("__API_ENDPOINT", {
               method: "POST",
               body: JSON.stringify("{ query: { getCouponCode { couponcode } }")
            .then(function(json) { return .... }) 
        }
        destroy: function(couponcode) {
            fetch(......)
        }   
    };
     
    return myapi;
}());
Then think about an init method or something to pass your endpoint in.
m
Thank you, @agartha! That’s helpful. Might you also know some specific search terms or web resources I could look up to learn more about how to go about doing something like that? I searched to no avail so far.
This seems quite helpful so far. Not sure how to pass the SDK to the client, as you describe it, though. https://github.com/github/fetch
a
What I sent you, is something you put in a javascript file, that you can host somewhere or give out to the client, and they put it in a script tag on their site, like jquery or other libs
m
Ok, that makes sense. If I host this file somewhere and the client puts the url into a script tag, then
window.myapi
means that the client can use
myapi.generate()
locally?
a
Yes, they can use that in their javascript code
m
Thanks, @agartha! And what is the
__API_ENDPOINT__
?
e
That's a placeholder for the uri pointing toward your API
1
👍🏻 2
m
Can you translate that into dummy words (I’m still a beginner regarding cross-domain communications and backend things)? Or maybe give me an example? How would I set that up?
e
Couldn't hurt to do some learning on the browsers window object
👍🏼 1
Then @agartha s code sample would make more sense
👍🏻 1
a
the API endpoint is your Graphcool (simple) API endpoint, displayed using
graphcool info
, or in the Console
m
Gotcha! Thanks, @agartha! And thanks @eruby!
😎 2
The
JSON.stringify("{ query: { getCouponCode { couponcode } }")
then contains the GraphQL code, right?
a
Yes, that is the GraphQL query that you can copy right from the Playground
🙌🏼 1
m
Thanks ❤️
a
If you need any hands-on assistance, send me a PM
👊🏻 1
👊🏼 1
e
@agartha knows what's up
🤓 1
💪🏼 1
If you want to avoid writing a micro service, then this is the best approach. I've rolled my own servers before, and it's very doable, but the ability to avoid having to maintain for scale, and keeping things secure can get tough if you don't know what you're doing. This is why there's built in support for serverless functions in graphcool
👍🏼 1
m
This is excellent, @agartha: some real basic syntax they didn’t teach me in school 😊 http://checkman.io/blog/creating-a-javascript-library/
👍🏻 1
e
School is more there to get you started and build the connections to get a job. Your most valuable learning (data structures and algorithms aside) is gonna happen on your own time, or in the workplace
🎉 1
m
For sure… getting down and dirty with the code, messing up.
e
You got it. Google and stack overflow will become some of your most valuable resources. (Along with this slack 😁)
😂 1
👻 1