Discover the 💯 toolings and learnings from creating a production ready modern API, made on 🌎 by & for humans!
Next destination: SpaceX Land 🌖
How tall is a rocket? How many launches have already successfully landed-back?, What’s the maximum crew capacity of a capsule?
The SpaceX 🚀 GraphQL API is now live 🎉
Before going into technical details, let me be gentle with the 90% of people who don’t know what an API is, just telling an anecdote…
During the traditional 2018 🎄Christmas dinner, I was having a talk with my physics fanatic uncle about the work I was doing with the SpaceX API (he had never hear about what’s an API, but he does like rocket stuff so was willing to listen), I picked up my 💻, opened the browser and with a few button presses, we got all the SpaceX rocket’s name, description & engine details, he was surprised of how easy it was (now you could even do it with your📱, it’s just mind blowing).
After that, yep, I lost my laptop for the next hour 😂, he knows a lot about thrusters & such, thus, he started to explore the API meanwhile I was playing with the little ones. At some point, he fetched some launches images and genuinely, my 5yo cousin asked if I was inside of that 🚀 (I love those kiddo’s life perspective, they do believe anything is possible). By then, it had already happened, thanks to such accessible technology & their incredible features, we, the three of us, spent part of the evening just being amazed about rocket missions, capsules & other SpaceX data!
P.S.: The little one already know more about SpaceX than me (as he was playing around with the API during all this month). I’m so 😃 just because of the fact he’s curious to learn.
Ok, now, let’s dig into the technical details. As I strongly believe that accessibility & a great U/DX are essentials in order for a Software to have adoption, you’ll probably have to deal with other concerns as I had when I discovered the existing SpaceX API implementation…
When I found it, I was really excited to look into the available data but… within a few minutes I was already** dealing with versions, docs & 3rd party SWs**, basically, I gave up then I decided to start the migration!
In this post you’d find in detail the main reasons about why it happened.
Migrating SpaceX API to GraphQL 🚀
*As another lover of physics, astronomy and rockets, when I found the SpaceX Public API (thanks to Full Stack Apollo…*medium.com
Tons of articles have been written by experts about the pros/cons of GraphQL & REST APIs. I’m not going to repeat those here but I’d totally encourage you to research about it, you could also check the GraphQL official website to get introduced to it main features!
GraphQL: A query language for APIs.
*GraphQL provides a complete description of the data in your API, gives clients the power to ask for exactly what they…*graphql.org
Let’s talk about project structure, type-safety & security showing the main tools & best practices used:
⚫️ It follows the Schema-First approach using a domain modular **schema **structure.
The schema is splitted by the different data collections (capsule, company, core, dragon, history, landpad, launch, launchpad, mission, payload, roadster, rocket & ship), also contains the global type definitions (*composed by both, commons & primitive GraphQL type definition*s)
All of them are subdivided into the following:
It contains the data collection type definitions modularized by concerns.
Functions responsible for fetching & filtering the desired data.
Composed by parse functions or any operation that the returned data could need!
Even knowing that the current GraphQL API implementation is not too complex, it might/will grow, that means we have to care about modularity in order to avoid code refactors, having said that…
It’ll allow you to easily identify your trivial resolvers & reuse the logic functions within them 👌
⚫️ It provides Type-Safety auto generating typeDefs & resolvers TypeScript types.
Having our GraphQL schema as Single Source of Truth, **we might want to **auto-generate all our types in order to keep the data structures in sync over the whole application. Here is where the **GraphQL Code Generator** (created by Dotan Simha) comes in!
GraphQL Code Generator *Generate code from your GraphQL schema with a single function call*
Don’t miss the following post if you’d like to more about it implementation & benefits!
Type-Safe GraphQL Servers 🔎
*This post exhibits the motivations & steps for adding Type-Safety into GraphQL Servers*medium.com
⚫️ It’s protected from infinite **& complex requests** providing custom error messages.
Thinking in GraphQL as a huge endpoint where all the data is available, not having the right protections might cause vulnerabilities such as being affected by DoS (Denial of Service) attacks.
If you’re interested on securing your GraphQL API, don’t miss this Max Stoiber article where it gives a 💯 overview about the main problems & its respective solutions!
Securing Your GraphQL API from Malicious Queries
*Working with GraphQL is amazing, but also has complex security implications. Let’s dig into some essential protections…*blog.apollographql.com
You’ve probably wondered about where the data is coming from, before dig into that, you have to know that the data exposed over the API…
Although, IT IS REAL DATA provided by several SpaceX information sources where the **main **is the Reddit SpaceX forum
It Wiki section is full of data written by the community members, also, there’re more coming from the following sites:
This incredible work was done by the most popular non official SpaceX REST API (which is running already for 2 years). I wanna give them a great shout-out for such a work giving us the capabilities to access all this interesting data.
r-spacex/SpaceX-API 🚀Open Source REST API for rocket, core, capsule, pad, and launch data
I’d totally encourage you to help them proposing new features or fixing existing bugs, they’ll definitely appreciate it!
You might want to take a 👀 at the codebase where you’ll find all the implementation about the mentioned topics, here it is!
It really does, you’d want to get all the advantages of GraphQL while using & exposing REST, such as:
🔘 Fully up-to-date generated documentation.
🔘 Runtime data validation **ensuring type-safety.
🔘 No more** painful versioning.
🔘 Forget write manually routes & controllers.
➕ much more
The SpaceX REST API is also UP 🎊
Although, if you’re looking for performance, I’d recommend you use the existing REST API!
If you’ve already implemented a GraphQL API, within a few minutes you could also have a REST one, check how you could implemented it!
⚪️ Use ‘express’ and share your schema (composed by typeDefs & resolvers), as well as your context over the servers!
⚪️ Use sofa & OpenAPI in order to generate a* REST API* based on your GraphQL implementation. You might also wanna use ‘swagger-ui-express’ to serve the docs!
⚪️ Use ‘apollo-server-express’ & apply the ‘app’ express instance to the GraphQL server!
There’re still a lot of work to do, starting from error handling, testing, etc… I’d love to receive any proposal/issue in order to improve the existing!
spacexland/api 🚀GraphQL & REST APIs to explore rockets, launches & other SpaceX’s data*github.com
Also I’d animate you to contribute with all the mentioned technologies, they’ve already done such an incredible work but I’m pretty sure that they’d appreciate some help!
It was never easier to create & share 💯 web application projects using your favs FE libraries, thanks to Codesanbox Ives van Hoorne’s creation. Whether you’re starting with GraphQL or not, give a 👀 at the differents live examples, you could discover interesting ways of use!
Learn how to interact with a GraphQL API using TypeScript within a MVVM architecture.
If self-learning wasn’t a enough reason to build something, you might wanna know that there’re people from all over the 🌍 willing to learn from your projects, speaking off…
If Elon couldn’t convince you, do it for them, the OSS community will really appreciate your work! Anyways I’ve got great plans for every single SpaceX CodeSandbox created, in special for the 💡 ones, help me out with that!
First I want to encourage anybody to begin writing blog posts, you’re totally capable to do that 💪, I’ve just started a couple months ago (I don’t even write a proper English) and I cannot be happier about it, you’ll get better, it’s just matter of time. Share whatever you 💜, people will learn, like & thank it!
If you’re a GraphQL enthusiast as I am, I’d definitely recommend post on Open GraphQL. It’s a publication open to anything & everything GraphQL related runned by Nader Dabit, message him!, surely he’ll be willing to have you on board!
Also, I’d want to give a shout-out to The Guild Urigo’s foundation for supporting the GraphQL community with tooling like GraphQL Code Generator, 😉 sofa, GraphQL Modules & GraphQL Inspector among others, it really changes the DX!
To wrap it up, just wishing that you could learn something from this post, in addition to, please, keep contributing with the incredible OSS community 🤗
Please, consider🙏🏻ing, contribut♻️ing and shar💜ing it!