Rust and GraphQL: A match made in heaven (CHRISTIAN LEGNITTO - Engineering Manager at Robinhood)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
thank you thank you very much for having me I'm super stoked to be here at the graph QL summit Thank You Apollo for inviting me yes I'm going to be talking about graph QL and rust I think it's a match made in heaven to be clear this is rust the programming language not the video game so I know people get that confused sometimes but yeah first a little bit about me I'm an engineering manager at Robin Hood I manage our business automation teams so what we do is try to automate humans and machines to make it so robin hood isn't this big brokerage with a ton of humans faxing things back and forth but before that I was actually retired surfing around for three years and picked up rust on the side because I've been managing for a while and kind of missed programming and rust seemed like a good thing to learn I'll be talking about that before that was at Facebook Mozilla and Apple I really love graph QL and I really love rust so apologies if this comes off as like a rust fanboy I maintain juniper which is the rust oh this is not going forward sorry about this let me fix my slides area sorry about that so I maintained juniper this is a rust server-side graph QL library my other claim to fame with graph QL is I was in the room when graph QL was invented at Facebook which is pretty exciting I was part of the team that rewrote our mobile apps and kind of got our act together on mobile but I had nothing to do with it so I take no credit with graph QL whatsoever I also work at Robin Hood with Lee Byron who is the co-creator of graph QL and he is gracing us with his presence right here in the front row supporting me so thanks Lee I believe he's also the head of the graph QL foundation is that true yes so there's a lot of graphical stuff in my world and you could too we're hiring as a manager I think I'm legally obligated to say this ok so what is rust so on the website the rust language team says it is a language empowering everyone to build reliable and efficient software and that sounds really great to me because I like reliable and not like efficient software hopefully you do too but why would you use rust the first thing I think that's really compelling to me is performance so rust is just ridiculously blazingly fast and memory efficient it has no runtime and no garbage collector so when you're putting rust in production you're not tweaking a garbage collector you're not doing a bunch of that sort of stuff you're not dealing with runtime abstractions that are high cost you only pay for the abstractions that you use so generally rust is going to be as fast as like C or C++ and it has some additional nice features on top of it I don't know about you but I don't trust myself to write C or C++ period let alone accepting stuff from the internet I don't know if folks have seen this before this is the tech and power benchmarks they may be true that maybe not true benchmarks are always taken with a giant grain of salt but for most of these rust is in the top five if not the top one and this particular one these two purple bars on the top are two different rust frameworks and you can see there's a lot going on in these slides but these are this is the languages the top two are rust and the sea and there's go and Java and C++ code Java C++ and so in production on the server side people are often seen rust be ten or a hundred times faster than comparable code written in node or Python or Ruby or and of course the gap is smaller verse first of things like Java or go and things like that so it is not absurd to write something Russ and the first time it is much faster than you could ever imagine in your normal language and to me that just blows my mind quite honestly because there's no garbage collection because memory is manually managed typically rust also uses one to ten times less memory so even if you're doing something that's CPU gham that maybe when the Java JIT kicks in and and it's one for one and speed usually rusts memory is a lot lower and as the industry moves to things like server lists this becomes more and more important you need things to be fast and you have memory overhead and you'll obviously want to use eschews resources as possible so that's okay like sees that fast C++ is that fast other language are what is an benefit of rust over those I rest also is reliable so it has a type system that prevents bugs the type system is very similar to typescript if you've used it generics all these sorts of stuff it guarantees memory safety and thread safety so going multiple threads distributing computation is pretty safe in rust in C++ I don't know about you but I would not trust myself to do threads and memory stuff in the C++ just just asking for security bugs asking for crashes and things like that and and rust gives you some tools to make that a lot more safe variables are immutable by default if you've ever used immutable dot J s and JavaScript you kind of know the benefits of that this is another foot gun rust takes away from you and kind of does best practices by default in the language and one of the things I really like is rust makes air handling explicit you have to handle the case or you have to explicitly say I do not care about these air cases so in other languages for example if you get a path back from the operating system a path is just a string right well strings can be encoded in different ways and on some operating systems like Windows you can have different character sets that maybe don't map to Russ Natives string type for 99.9999% of the people we don't care about that but that point 1% may care about it so in rust you explicitly have to say do I care about this case if not just soldier on if I do care about this case handle the error but you have to cognitively do it the system just doesn't handle it for you and then for me it's really important that rust is practical and they're Mozilla created rust because they had a practical need and they want to put in production it's not some research language that's in ivory tower research it is made to integrate with the real world and other languages that may not be the case so one I think is really nice about rust similarly with go it has single binary deployments you don't need to bring a JVM you don't have to bundle and all this sort of stuff you build a binary and you can put it in docker and things like that and run it another thing I love about rust is rust can act like C to integrate with other languages so almost every language has to in talk to C have some extension point in Python numpy a lot of the stuff underneath is written in C to make it fast very often in nodejs let's say you're doing image resizing you're using the very popular sharp NPM package that's actually implemented in C++ under the hood and so node Python the JVM all these other languages usually have an interface to include C code and call it and call back and forth and because Russ can act like C you can write a high quality library in rust and bring it to your existing service written another language and get the benefits of the rust in that very small language domain you don't have to rewrite the world and that's very practical and I think it's very hard to get adoption for a language even if it's 10 times better if you have a ton of code already existing so this is critical for Russ actually being used and we're seeing it being used more and more and for me I'm lazy when I was surfing in Mexico and waiting for good waves um you know I thought about what I wanted to learn and Russ kind of ran everywhere it runs on Windows it runs on Linux runs on iOS it runs on Android you can even compile it for the web and run it in the browser via web assembly and this seems pretty cool that potentially down the road you can learn one language and regardless of where you're working or what problem domain you're working in your skills might actually be relevant and help and something I think is really important is the Russ community is welcoming it has a great community has a code of conduct that people adhere to the documentation is amazing and human friendly a lot of the rust core developers are hard core C++ people who have worked on Firefox worked at Facebook worked at Google and the C++ community does some amazing things but they're also kind of like oh if you have that bug that just means you're not a good C++ programmer and there's this kind of gatekeeping and rust is like we know we're not good we know the compilers are good like let's trust in the machines and we're all just kind of trying to figure it out so there's not a chip on people's shoulders the compiler errors are very useful and very informative very similar to Elm if you've ever used Elm and rust encourages open source contributions so all the rest language stuff is in github in PRS all the organizational changes happen in the open so that's really cool that you as a community member are on the same footing as people maybe at Mozilla or other companies that are heavily involved and you trust wears rust use I don't need to pontificate about how much I like it or don't need to do quotes I think the proof is in the pudding of people actually using rust so large parts of Firefox are written in rust this is why it was designed as a language and AWS is lambda VM rust so if you're running code on lambda it's running on top of rust face mercurial server all server which is critical right that goes down if that has correctness bugs like game over then you libros stuff cloud fair chrome OS so rust is starting to creep in everywhere where speed correctness and safety is necessary and I would argue you want those three things all the time okay so why not rest steep learning curve not gonna lie I'm not I don't assistant program background and it was pretty steep for me very small ecosystem frequently you will try to do something a library will not be there but when it is there is generally high quality and then long compile times it's a compiled time language it's not as fast as go when you compile it and that's a huge bummer when you're writing so I think those are the biggest three okay that was enough rust evangelizing I think it's compelling you may not but I think with graph QL and rust it is materially compelling so I'm gonna talk about those interactions and we'll just basically be stepping through code for the most of time we'll see how we do on time so like I said I maintain the raspy rust graph geoserver library juniper so you can grab my slides and click there later all these examples are gonna be using juniper can you see this okay yeah okay so on the left is the graphical schema we're going to be doing and on the right is rust so hopefully you folks are familiar with graph QL this is us defining a person object and some fields emails a string that's non null wool age is an int that's not malleable how do we do that in rust it looks basically exactly the same way so Russ objects are called structs we say it's a person says email say a string and probably I should have done it unsigned int here again Russ is a bit low level so you can't just say int or number you have to actually say the size of what you're doing but that's just pure rust there's nothing graphic you all about that how do we how do we like convert it to this graph TL schema how do we make it graph you all aware which unit per is is all you need to do it's basically just a simple annotation that's like derived graph QL object and that attaches all the graph QL machinery on this particular object on this particular struct so when you query for person email it will use the the attribute in that object pretty straightforward now let's say we have something like password hash that we don't want our clients to be able to query juniper has a way to say skip it so this is very intuitive very quick not a ton of boilerplate just really get to to the point and this is not a DSL this is just standard rust these attributes and all the stuff is standard rust okay but you just can't just have an object you have to hold whole schema in order to create it so let's walk through a schema and graph QL and a schema in rust so we have our personal object from the previous slide we have to have a top level query so let's do this we're gonna have everyone field and that's gonna have a list of persons and in Gretton rust it's basically exactly the same the query is just an object it's one of the nice things about graph QL so in rust it does have a raise but they're they're kind of wonky so you come out this is a vector which is basically just like an array that can go big in small and you can see in this slide rust has generics so vector is a container and the type in the vector is a person very similar to typescript and and other languages of generics in this particular example we do not have a mutation so that's just an empty object in graph QL in rust we have a nice type thing that you can say this is empty to make sure you don't accidentally add fields or stuff on to it and then you take the query the quick take the mutation put them together and that's your schema and that's how you do it and rust with juniper so very straightforward looks very similar to the graphical schema and it's pretty insane that this is all you need to do that this is a low-level system language supposedly and this looks very high-level and almost like Python and very similar to draft you all but that's not telling us too much the graphical has this great feature called descriptions I hope people know of because everything in graph QL is introspective you know you want to say what is the email what is age maybe they're straightforward here but but it might change in the future so in graph QL you can do this with string literals on the left you can say Oh a person in our system this is the email they used to register and the self-reported age how you do that in rust are comments so rust has multiple types of con up comments the first one is just your standard two slashes that's just like a normal comment but three slashes are duck comments so you may be familiar with these from Java and things of that nature this is built into the rust language so anyone not familiar with graph QL not familiar anyone familiar with rust would know exactly what that is and would know intuitively how to add comments which would then show up in your graph QL schema this is a good time to introduce cargo so cargo is like NPM but better for rust so when you download rust cargo is your main way to like start builds and add dependencies and and run tests and all that sort of stuff and carbo has this cool feature that you can run cargo dock on your rust project and it will take these dock comments that are littered throughout your screen or your code and will generate documentation for you so you get this nice documentation that has all these links this is all local you get all your comments in line and you can see there's this trait implementation traits are like interfaces in other languages kind of like functions you can attach to an object and you can see because we annotate it with that graph QL object thing that okay yeah has an email it has an age but ass Allah also has all this gobbledygook this graphical type and that's the magic that does it behind the scenes I'm not gonna go over this gobbledygook because it's not a a rust training presentation you can do cool things because this is in the language you can do that anyway in other languages you can extract comments and make documentation but this isn't the language this is an every rust project and it ties into all the other nice infrastructure in rust so you can say okay I want to actually deny missing dogs I want my code to not even compile if it doesn't have documentation and you get you would get stuff like this you're missing documentation for a struct where it is exactly where it is and underlined so this is really great this helps your your your code scale rust error messages are out of this world but what's nice this happens that compiled time out of the box you don't have to add stuff on it you don't have to add linters you don't have to add a separate test for this it's just in rust and that blows my mind rust took first class like comments those first class citizens in the language so a couple of things to point let's say we we put this dot comment on top of our person to show an example of how to use it well the first thing is we have the backticks everyone kind of does that does markdown I think naturally now with github and with all this sort of stuff it's table stakes Russ supports markdown in comments natively so if you do markdown when you do that cargo doc you'll get all your nice formatting all that sort of stuff usually you put a nice example especially if you're doing a library for other consumers like hey here's how to use my library do this maybe don't do that Russ kind of takes it to the next extreme and will extract code in comments and run them as tests with your tests and that prevents your examples in your documentation from getting out of sync with the actual implementation so if later I made the age which is here 42 later I made that a unsigned int and or passed in a string it would fail at compile time so yeah card would test runs code and doc says test is really great there's a couple projects I think for JavaScript Java that extracts stuff and then we'll run them but I don't have to deal with it and I just know in every time I'm writing rust it'll just do the right thing okay I'm writing on time so I'm gonna blast through the sort of stuff so let's say we want to add a UUID we want to tell people to not use the email anymore graph QL has this great feature deprecated you can say don't use this field we have the same exact thing on rust this is built in the rust almost the same so you can do this not only in context of graph tool as well as in the context of your rust code and simile is in graph QL you can actually give the reason why you can say hey like don't use email use you do ID and rust same thing its deprecated use UUID and again because it's all built in rust you can do similar things like don't let my code use this deprecated thing and you can poke holes in it and say okay don't let anyone use this deprecated thing but for my graph to on point I'll let people use it and you can really pick and choose and move your code base forward and it's built into rust all right let's go through what a more complicated example looks like so we had of our person but we're now adding a can vote bool filled in graph QL so we have to kind of expand what that annotation graphical object was doing so what we do is we say hey this is now a Juniper object and this is an implementation for a person and we say okay here's an email it's now a function call and now we can do anything we want here we're just returning the email here we're just return the age here we're actually doing some calculations in some code this should look very familiar if you're if you've used other languages server-side language with graph QL and I think that's sort of the magic here is you get all these crazy improvements you get all these benefits and it doesn't really hurt you as a developer it still feels like you're writing Python or you're writing a JavaScript code okay let's say we add a age label for so if your age is a certain range we want to return a specific string in this case we're returning a result so like I said rust has good errors and so we're saying this could fail and whenever we return something from this function we have to say is that case a success case which is which is which we denote with okay or is that case a failure case which we denote with error and because we're matching on different values okay if it's zero through one do this if it's three to four do that we have to match all values otherwise rust would not compile so we have to handle the case where we don't know so rust just like makes us handle that and decide what to do and decide if that is a failure case or a or a success case and Juniper just like any standard back-end stuff when you query it it returns the data that it can fetch but also returns the errors automatically all that plumbing that's just handled for you rust Maps very closely to graph QL schema so we have enums we have interfaces we have traits we have all this sort of stuff very straightforward and there's always an escape valve if the Juniper auto stuff of ok it'll take terrible twos and it'll just capitalize it but if you want it to be terrible underscore tubes you can you can override it very quickly n plus one in graph QL is a thing it's still a thing in rust it's just inherent in the problem space graph juniper the library gives you a look Eady and it's up to you how to use it we do have a data loader thing in rust we have eager loading so if you want to load everything up front and pass it through you can use that we have ways to go from your graph QL code or sorry your rust code to your schema so if you want to use other tools like Apollo you can have a nice schema keep your source of truth in rust and just just print out the schema or you can have a schema language and generate all this juniper boilerplate you can go the other way and there's a product for that you can run graft you all you can have a client it can run in the browser by webassembly or if you just need to write to Lee on top of graph QL there's a parser for further reading the Russ book is very good it's free it's online and it's very newbie friendly the Juniper book for graph QL is a work in progress but it's pretty pretty good and we have an end to an example of using rust with graph QL with the database and if you need help please contact me on Twitter I'm very passionate about graph QL and rust that's all I got thank you very much [Applause]
Info
Channel: Apollo GraphQL
Views: 22,808
Rating: undefined out of 5
Keywords:
Id: QXJ0wKBLt-8
Channel Id: undefined
Length: 23min 42sec (1422 seconds)
Published: Wed Nov 06 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.