A New Look Into APIS - Graphene

0 0

today I will be speaking about graphene graphene is a new way for us to create API is very early swing before I start I would like to give up a quick introduction about myself she was like Barry I am clueless like developer I've been working with back-end and front-end for almost 10 years right now right now I'm working on a firm in charge of the data communication between services using rothko if you find this talk interesting just let me know we are hiring a lot of people will be welcome to hire more you might know some of my packages or you might not I did hydrate which is my template engine for Python a value they email by leaving email unlike other packages you can check our predator that leaving it have /co Cyclery today i will be doing a google interaction about caffeine but before that I will review a little bit like what is West what we can include from rest from there we'll go to Rocco what Rocco makes better than rest and then to graphene which is a framework for using Rockville Pike and then from there we can see how we can use for free with Django very very easily so on first slide what is the typical jungle structure the ground of jungle structure is using the jungle in our back-end or our models and also I mean a program you know I am in panel and usually we used to have server-side general templates so what's younger the one who was in charge of rendering the layout however in the past years we also have desktop web apps which are powered by react and angular and mobile apps and the way they communicate with Django is through some APRs so for communicating between these desktop web apps or mobile apps with or Django back-end they there was a dominant market which was rest how many of you know what what is rest or reading using the demo whispering worried their applications that's a lot perfect you will love cooking so I will do a very quick example about rest imagine we want to create a conference application where we have talks and we have a speakers and these thoughts have a title meet have a timescale so when the fog is happening and also we have the user model which have a name and have an avatar and Paul have a speaker which is a user and we can get the focus of our user which is referencing so if we want to create this conference application and we want to get all the thoughts what is the title for each talk what is the name and the avatar of the speaker of each of the parts more or less this is how we have to do it in a cool restaurant so we love the schedule the scale will return a list of thoughts a list of ideas then we love we love we load a detail of each talk it's all we will return us like the title of the talk and probably the idea of the speaker I'm from the idea of the speaker we will fetch again the user information from this ad as we can see when we are making almost 72-pin quest for just feeling one view and this is not performing at all but before going forward on that what are the main points about that we have to deal about using a part of these events we have to deal with API version we have to deal with input valuation the means if you if we want to create a user or we want to filter or do pagination we have to make sure for example for pagination that we wish to see by integer another string we have to make sure that they output reflect the types that we want also we have to deal with data under fetching or over fetching that means like a model have a lot of fields some of you doesn't want all the fields loaded right like sometimes we just want the name of our user and not anything else we have to deal with network and with network what you see for the first three bullet points we can see the jungle rest frame worry solved this you know partially good way but whatever the last three what about the you want to specify what is the data we want how we can solve that so I usually are usually a good way of something that is moving everything together all these seven HTTP and points into what so we will create a new one point which is Alta with username and other time for example and it's very normal that after some time we can get questions like oh could you please have an option to get the data back without this field but without honor extra field for a new video that I'm creating because of that we can see that the logic of fetching is moved from the kind who previously knows what we want to serve it which isn't now the one responsible knowing what the client was and because of that each time the client wants to fetch more or less say that we have to go back to the server and change your logic there and it's not very scholar why because if dream money we have a lot of times and we just change a little bit the logic there we always have to go back to a server to change and we start and everything and it's not it's not the best way that we can do there because of that reasons a lot of companies are investing a lot of time in how we can improve this process one of these companies was faced it could presented Rockwell's one year ago but they didn't back you up since three years ago I think um what is what girl what girl is a way that let us define or quarry the data we want this is the whole world in this case I'm just getting the name of my user as you can see this rock quarry is very similar to geisha and the rock girl response or they use unresponsive we'll get from requesting this is just like that for a beard me but fill with beta so we can think of rock you'll like the Jason we want to fetch but without anybody just the keys so going to a more complicated example we want to get my name but also my talks the title of each of the talks that I'm doing and the time of like when they talk is happening I hope you can already guess what will be the response of that is just the data we are requesting but feel like the days before requesting filled with data and it's very tweeted knowing what we are querying because the response will it will look exactly like that for a year ago so let's see how we can create that rock club and skim away from that we have to create the root query which is a type of we did when we first worried this great this quarry type Dimitri on it the me field is returning a user type user type of name may feel an alternate and thought is returning a list oh sorry a list of thoughts and Paul have a title and a time on it so what are the real advantages of battle we have for evaluation if I can do up worried that we don't expect for example is scoring a feel that that's Nexus we will honor but not only that God oculus is pretty tight both input and output so if we expect to receive a number and we will take a swing it will show on the road without executing anything and also for the actor so if we expect or the clients face to receive in the hair or sample for our page then it should receive it and it will check it always but not only that we will not have the problem of data under fetching or over pretty why because he's the kind now the one who knows what it wants and then the client sites how much data you want to reside to see also we have introspection but what I mean with this introspection what I mean is actually the client could see what are all the types that are in my universe and could see like description or what are the relation between that going back sorry going back to introspection another powerful powerful thing about introspection is let us have validation without query and it let us have for example an IDE per query that I will show you later and it makes things very very easy also we have the resolver context so it's time we are resolving appeal we know what is it's fine for example if I thought you might talks I know that I'm fetching the talks from my user and I can I also we will have only one round three for data fetching so we will not have any more the point of any poor latency or HTTP errors okay another cool thing is we have gradual in a lot of languages not just JavaScript which was the main implementation that Facebook page we have gradually iPhone we have gradually in Ruby rails we have graphical and go in LEC running almost any language you can imagine that but let's go deeper into the walk or for Python graphing we are frame is a frame where I traded for using Rockwell and become very very easy so it's the main way of using graphical Python so what I'm going to give some data of cocaine it's the most popular repository outside Facebook we have right now around 770 stars it's used across 20 companies more or less in production after amis included it have a very large community and these let us fix things or improved very very fast and not only that we support pipe from Python 2.6 to Python 3 very easy like without any change in your code and it's fully compatible from Django 1.6 1.6 plus so going back to or more we have we want to create your compressed application and we have follows the talk model on the user model with we feel that they described before if we want to create or we want to implement the following a graphical schema that could execute this query this is how we have to do it in Python so we create the query type which is inheriting from graphene object type and defines the mean field which is referencing the user the user is a more graphic type or the type to have a name which is a string and port which is a list and talk is just our object ID we have title and dynamite title is the strain time is a day time so we can see that with just nine nights of code we can define a world human universe and it's very easy to follow it one thing I'm more proud of is a play on the playground let us pour your play with graphene or broccoli - berries so you can actually go your computer's go to this URL and play with it by yourself I'm going to do a very quick demo about how it works so first we pour the paint we need it the is using pi 500 GS which let us haba python interpreter in your browser so we create the user type we have a string we need a name it to the screen we have the query type which is another object type of Tokyo this prototype had me feeling it depends on the user and then we create the scheme and we define which is a route prototype which in this case is if we try to execute the following query we want to get my name right now we have to give of course it will return now because we don't that we are not defining how we want how we are resolving this thing so with if we trade the following function you resolve me um we can say like okay each time you are requesting a user my username is your Sigma and we put it again we can see how you filter the data we want but not only that let's complicate it a little bit so we can make the previous example that I was talking before so we want to create the top top the top the top type will have a title which is a string and we have a speaker which is referencing the user in this case we can use the same jumbo reference with the strings and we will map to the correct class in this also we are adding the thoughts view which is a list not reference the cost the class type so right now everyone we are not requesting the pottsfield so we are fetching the same data but it would want to fetch a talk so we can see how now it receives like a empty empty list in this case I'm just saying like ok I'm creating or I'm returning the user we have this for tonight if we have to give this again we can see how right now it's fetches the data for each of the tops but let's complicate it again under it we want to query not just like a random user but we wanted to worry for example I use are given an ID so we are saying when we create a user field in the query and we say what we have the ID argument which is a string also we are defining how we want to resolve a random user given an ID so I'm pretty here a dictionary of users which in this case is just like a mapping with an ID and the correct object type or the correct instance and let's try it and see how it works so for example to me I'm just returning I'm just gonna get the user with the ID one I'm gonna execute it and see how it works okay it works perfectly in the same way and also I'm independent how we want to resolve a user so we get the ID from the argonauts ID and now we can execute again but now we can change from me to you sir and if we execute this query will return no because we are not giving there any ID but actually random data okay so here if we specify an argument for example ready we can say for daily one is fetching serious Akbari which is me myself by 382 is fetching there the Peter user so this is more or less valid later on good but but let's go a little bit deeper I have Co means but I will try to go fast ittle bit deeper about like how graphene work with Jack let's see we have these models in our Django application so we have the user mode have our name which is a char field and avatar which is our image field and a poke model with our idle time and our reference to the speaker a very good thing about graphene is written actually Mac ultimately from our jungle models to rock will types so we don't have to type again and again the same things so let's see how it will look like actually we create a class user if you are ready have been using it's very similar to Astoria lighters so we specify which model we want in this case each of these types it will get the fields from the jungle model and we can also specify for example which fields we want to map into Bravo so for user we just want the ID name and after we don't want the password we don't want any other field and for the toad we want to exclude the ID I always wanted to do a demo but I don't have time so why we should use graphing in your pattern white brought to graph your graphene is better the first thing is much easier to maintain our various APRs you will have one recommendation on UI for free given the introspection you can recreate the recommendation from the schema universe you have so you don't need to spend any time more in documentation or in the idea it's everything also you can have a very quick easy integration with your phone from 10 with react thanks to react related library by paper as you have seen have seamless integration with China but overall the most important things is the development process will be much faster in a basing our own experience we lower the times by the development time by 2 or 3 and that's quite important why because we don't need to go back to the server always each time we want to face two different things graphene is a service I know that can be used on the web and Burdine also be run on a neo4j database in my own data center yeah it could be run like any data like it could be run in Django it could be run like actually one guy built graphene integration with google appengine and you can build with almost anything you want it's transparent like if you want to be another tool on top of that it's very easy there it's not required to use John Cornyn and you have the tools for doing it with John but actually you can integrate with flask or Google App Engine or almost anything you want I thank you for your talk um does the graphene library have a sense of permissioning where a user might have access to some data but not others yes actually there is one thing but I didn't cover today because I didn't have enough time which is middle work middle work at this time we are resolving appeal for example thoughts for a user and we can check it there you are you sir have permission for getting the thoughts upon our user so yeah we can have like permissions very easy and for example with Django quarry on upon your open source a more thing that we are using is here so it's very easy to add permissions on top of that and see what user what the user is capable of yes thanks for your talk is very interesting my question is specifically around you know like in the example the data was defined in the query file yes at what point do you put that data into like some kind of graph database or like how does that generate IDs you there is no matter you can find you can decide how you want to fetch it a data so actually if you want I can give you every if you visit my github like you've had a slice feels like very I just the last repo I created like that sample what I did using Django and you can check there that we have a result method and we want to for example get certain tasks right I mean there is other method we defined how we want to fetch you say so actually there you can specify how you want to do the integration with project actually Raphael have nothing to with grass-like have to do with grass in the way that you have types and you have a relation between types but other than that you can do integration without sequel database or no sequel or whatever the second general you know time to show you're gonna show how the graphene query models to map to django models similar to the rest framework does that have support for the Postgres a new post quest fields like today saamana reveals actually - it not only have support with the latest possible fuels but also have support for example the sequel to me but it's yeah it's it's very easy to an integration with these things like we have support with JSON it's or in the kind of hard coded example the query object kind of like drilled down to define the relations explicitly when you integrate with django will a graph QL query and it would do this joins under the hood automatically for you some yes actually there is a very good example I recommend you to check which is a Star Wars API example if you go to graphene python.org there is a button there which is like check out check out or Star Wars whatever all the relations and all of these are will be automatically gone without any your side and I told you mine was very related to the Commission's when I was just wondering about off if that even is a thing with the graduates go yes actually like you can check very easily like what is the context and each type you are you are requesting in the case of the Jama integration the context will be the request context so you can check there each time you always always something what is a user who is requesting data from there it's very very see what I ran for a sucker