Launching SpaceX GraphQL API 🚀
Tags: graphql, typescript, spacex
Published: 2019-08-09 11:54:44

Discover the 💯 toolings and learnings from creating a production ready modern API, made on 🌎 by & for humans!

![Next destination: [SpaceX Land](https://spacex.land) 🌖](https://cdn-images-1.medium.com/max/3840/1*ixnzMP6t4Iot9nZpBmr1Vw.png)*Next destination: [SpaceX Land](https://spacex.land) 🌖*
> # Did you ever 🤔 yourself…
> # How tall is a rocket? How many launches have already successfully landed-back?, What’s the maximum crew capacity of a capsule?
> # If you did so, I might have great news for you!

### 🥁 I’m pleased to announce…
> # The [SpaceX 🚀 GraphQL API](https://api.spacex.land/graphql) is now live 🎉

![](https://camo.githubusercontent.com/532c82955f92f382873cb9c31801703d4141c0ca/68747470733a2f2f6d656469612e67697068792e636f6d2f6d656469612f3273594432464d4f5a63373868396a4666372f67697068792e676966)

> # An API for
> # 👩‍💻👨‍🏫 Techies
> # 👴👩‍🌾 & no techies
> # 🌏🌖 from everywhere
> # 💻📱 with any device
> # also,
> # for those
> # 👩‍🚀 little dreamers 👨‍🚀

## Open the link & just enjoy interacting with real SpaceX data ✨
> # Discover all the interesting details about capsules, cores, landpads, launches, missions, payloads, rockets, ships & much more. You could even check if Elon’s 🚗 Roadster has finally arrived to Mars!

**Before going into technical details, let me be gentle with the 90% of people who don’t know what an [API](https://en.wikipedia.org/wiki/Application_programming_interface) 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!
> # Moral: Let’s build accessible software to keep motivating people passions & dreams!

![For humans & everyone else 👽](https://cdn-images-1.medium.com/max/2000/1*uiMzAvGBwBhU5tNYnvJcrQ.jpeg)*For humans & everyone else 👽*

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.

## Developer’s space

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…

### Motivations

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](https://medium.com/open-graphql/migrating-spacex-api-to-graphql-e1fe69a3a8e7)

### Benefits

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](https://graphql.org/)

### Implementation

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.

![](https://cdn-images-1.medium.com/max/2000/1*4bDeJQj6EFlgntOqS52xrw.png)

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:

🔘 **typeDefs**
It contains the *data collection* type definitions *modularized by concerns*.

🔘 **resolvers**
Functions responsible for *fetching & filtering* the desired *data*.

🔘 **utils**
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…
> # 💡 Break your schema by concern, not by types!

It’ll allow you to easily identify your [trivial resolvers](https://graphql.org/learn/execution/#trivial-resolvers) & reuse the logic functions within them 👌
> # ❌ By types

![](https://cdn-images-1.medium.com/max/2000/1*n1IiMijXeAlPBUc6yleHAg.png)

![](https://cdn-images-1.medium.com/max/2000/1*vJCg_LNA5_BgKJa5b-OLfw.png)

![Even if you’d split your resolvers, you could mess everything up whenever you’d have to import functions from other data collections 👎](https://cdn-images-1.medium.com/max/2308/1*e81P9nP0P8qDHKVNnLG-mA.png)*Even if you’d split your resolvers, you could mess everything up whenever you’d have to import functions from other data collections 👎*
> # ✅ By concern

![](https://cdn-images-1.medium.com/max/2000/1*SW93Mo9EDoCRCKV-EtHK-Q.png)

![](https://cdn-images-1.medium.com/max/2000/1*NxV6nYeBz6jZFsspdh9NWg.png)

![Identify smartly your trivial resolvers & reuse their functions, it’d be a huge win if you’d have to scale in the future 👍](https://cdn-images-1.medium.com/max/2000/1*pxjOgS1Kbb3GOzJN_Rou9w.png)*Identify smartly your trivial resolvers & reuse their functions, it’d be a huge win if you’d have to scale in the future 👍*

⚫️ 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](https://graphql-code-generator.com/)** (created by [Dotan Simha](undefined)) comes in!
[**GraphQL Code Generator *Generate code from your GraphQL schema with a single function call***
graphql-code-generator.com](https://graphql-code-generator.com/)
> # ⚙️ It generates all the typeDefs & resolver functions (including args & returned data) TypeScript types.

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](https://medium.com/open-graphql/type-safe-graphql-servers-3922b8a70e52)

⚫️ 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.

It can be easily avoided with tooling like [graphql-rate-limit](https://github.com/teamplanes/graphql-rate-limit), [graphql-depth-limit ](https://github.com/stems/graphql-depth-limit)& [graphql-validation-complexity](https://github.com/4Catalyzer/graphql-validation-complexity)

If you’re interested on securing your GraphQL API, don’t miss this [Max Stoiber](undefined) 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](https://blog.apollographql.com/securing-your-graphql-api-from-malicious-queries-16130a324a6b)

### Data

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…
> # ⚠️ Is not official data

Although, **IT IS REAL DATA** provided by several SpaceX information sources where the **main **is the [Reddit SpaceX forum] (https://www.reddit.com/r/spacex/)

It [Wiki](https://www.reddit.com/r/spacex/wiki) section is full of data written by the community members, also, there’re more coming from the following sites:

* [https://www.spacex.com](https://www.spacex.com/webcast)

* [https://www.space-track.org](https://www.space-track.org)

* [https://ssd.jpl.nasa.gov](https://ssd.jpl.nasa.gov)

* [https://www.marinetraffic.com](https://www.marinetraffic.com)
> # All the data from these platforms is parsed & collected in a public Mongo database from where the information is obtained.

This incredible work was done by the most popular non official [SpaceX REST API](https://api.spacexdata.com/) (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](https://github.com/r-spacex/SpaceX-API)

I’d totally encourage you to help them proposing new features or fixing existing bugs, they’ll definitely appreciate it!

## The SpaceX GraphQL API

You might want to take a 👀 at the codebase where you’ll find all the implementation about the mentioned topics, here it is!
> # [👉 The Repo & The API & The Docs & The Everything👇](https://codesandbox.io/s/yv004pqnq9)

{% codesandbox yv004pqnq9 %}


## ✋, there’s more…
> # What if you don’t wanna use GraphQL? That’s fair, but… What if you could generate a REST API based on your GraphQL implementation to get all the benefits from both worlds? 🤔, does it even make sense?

### 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
> # Now, this is possible 🤯 , just Rest with Sofa (Kamil Kisiela's invention) auto-generating a REST API based on your GraphQL schema!
[😉sofa — The best way to REST (is GraphQL). Ending the REST vs GraphQL debate once and for all](https://medium.com/the-guild/sofa-the-best-way-to-rest-is-graphql-d9da6e8e7693)

### Speaking of which, 🥁 I also gotta announce…
> # The [SpaceX REST API](https://api.spacex.land/rest/) is also UP 🎊

![](https://cdn-images-1.medium.com/max/2000/1*ox_Zf1Iqm-hn4x0IT-ag0A.gif)

## Open the link & try the available endpoints out💫
> # If you’re a REST API fan, you can fetch the data exposed through the documented endpoints. In addition you’ll be able to get all the mentioned benefits from GraphQL, such as, up-to-date typed Docs!

Although, if you’re looking for performance, I’d recommend you use the [existing REST API](https://api.spacexdata.com)!

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!

<iframe src="https://medium.com/media/8d6baa314eb8335363f8cd8990557538" frameborder=0></iframe>

⚪️ 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!

<iframe src="https://medium.com/media/0742f8148624a0b1828c020d68a6f31f" frameborder=0></iframe>

⚪️ Use ‘*apollo-server-express*’ & apply the ‘*app*’ express instance to the GraphQL server!

<iframe src="https://medium.com/media/48f77988a7fe25ec898a9ece1e9f191b" frameborder=0></iframe>
> # That’s it! GraphQL💜 REST, stop arguing & take advantage from both worlds!

## PRs are more than welcome 🤗

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](https://github.com/spacexland/api)

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!

## Build cool 🚀 stuff

It was never easier to create & share 💯 web application projects using your favs FE libraries, thanks to Codesanbox [Ives van Hoorne](undefined)’s creation. Whether you’re starting with **GraphQL** or not, give a 👀 at the differents live examples, you could discover interesting ways of use!

### ❤️ Angular

Learn how to interact with a GraphQL API using TypeScript within a MVVM architecture.

{% codesandbox o9nmplk96y %}

### 💙 React

Hooks are already here (not kidding, check [this](https://github.com/facebook/react/pull/14679) out)and you can use them together with GraphQL client libraries as ‘[react-apollo-hook](https://github.com/trojanowski/react-apollo-hooks)s’ 🤩

{% codesandbox r4p8q1kmvp %}

### 💚 Vue

You might want to use learn how to write Queries & fetch data with the coolest Progressive JavaScript Framework!

{% codesandbox m3v8yv0nw9 %}

> # Pick whichever programming language & FE library you feel confident with, schedule some ⏳ in your calendar & enjoy coding 👌 webapps!

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…

<iframe src="https://medium.com/media/07a04c37c607088cae322ec1de306bf6" frameborder=0></iframe>

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!

## Credits

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](https://medium.com/open-graphql). It’s a publication open to anything & everything GraphQL related runned by [Nader Dabit](undefined), message him!, surely he’ll be willing to have you on board!

Also, I’d want to give a shout-out to [The Guild](https://medium.com/the-guild) [Urigo](undefined)’s foundation for supporting the GraphQL community with tooling like [GraphQL Code Generator](https://graphql-code-generator.com/), [😉 sofa](https://sofa-api.com/), [GraphQL Modules](https://graphql-modules.com/) & [GraphQL Inspector](https://github.com/kamilkisiela/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 🤗

![](https://cdn-images-1.medium.com/max/3840/1*f6IMUipNmjSrEp9iEAX4sw.jpeg)

## We are approaching our next destination 🌖, stay tuned not to miss the landing!

Please, consider🙏🏻ing, contribut♻️ing and shar💜ing it!