Python + Marvel + GraphQL = PyMarvelQL
Tags: python, marvel, graphql, graphene
Published: 2019-06-03 17:08:01

<h1>Introduction 🌋</h1>

With the release of Avengers Endgame, there was no better time to build PyMarvelQL, a program that allows you to query the Marvel Database using GraphQL.

PyMarvelQL allows you to send GraphQL queries to Marvel’s REST API to get information about characters, comic series, stories, creators and much more.

![Alt text of image](https://cdn-images-1.medium.com/max/1600/1*ABLw585PPvYD3vFQyvwEeQ.gif)



<h1>Try it out 😲</h1>

Here is a query for characters with names starting with Captain. The query getCharacter() accepts the parameter nameStartsWith, which can be set to any value you want. Inside the query, you can include any fields that you want to fetch from the Marvel API. The available queries and parameters are based on the Marvel API documentation.

If you would like to download this tool head on over to this <a href="https://github.com/Novvum/MarvelQL/tree/master/packages/python">Github repository.</a>


<h1>How it works 🏗</h1>

PyMarvelQL is built with 5 tools — Graphene 2.1, Python 3.7, Flask, flask-graphql, and Marvelous. Flask and Graphene are the main tools in this project. flask-graphQl is used to host a local GraphQL server, and Graphene is used to create the GraphQL resolvers and Object Types that allow the user to query the JSON data from the Marvel database.

<h1>Flask and Flask-GraphQL 🧪</h1>

“Flask is a web framework for python that provides you with tools, libraries, and technologies that allow you to build a web application.” This project relies on Flask to support running GraphiQL in the browser. It uses GraphQLView from the flask-graphql package to add a /graphql endpoint to the flask app. This is what the setup file looks like for flask-graphql.

![Alt text of image](https://cdn-images-1.medium.com/max/1600/1*NtEVlqNULDqGXMZhcpI9ag.png)

Navigate to the /graphql endpoint to visit the GraphiQL playground to query comics, characters, creators and events.

<h1>Marvelous 🦸</h1>

“Marvelous is an Marvel API wrapper for python”. PyMarvelQL uses Marvelous to call a Marvel endpoint and retrieve a JSON object with all of the data needed for the resolvers. PyMarvelQL ships with our API key, but you can register for your own by visiting Marvel’s website. After getting an API key, you can fetch JSON objects like this:

![Alt text of image](https://cdn-images-1.medium.com/max/1600/1*avcU_DY4oKYZLFQqEQecDw.png)

The variable m contains an instance of the marvelous API that fetches data in the Marvel database data in the Marvel database. As shown above, the function accessData returns a JSON object depending on what the variable name is.

<h1>Graphene-Python 2.1 👌</h1>

“Graphene-Python is a library for building GraphQL APIs in Python easily, its main goal is to provide a simple but extendable API for making developers’ lives easier.” One of the main features of Graphene is that it allows us to create resolver methods and ObjectTypes. The resolver methods in PyGraphQL return JSON Data to the queries. For example, when fetching for Marvel characters the accessData function(mentioned above) is called and the JSON object is stored in the variable characters. Afterwards, the data is returned using these two functions:

![Alt text of image](https://cdn-images-1.medium.com/max/1600/1*C-hFWME7L0GZirxLMtqCyQ.png)
![Alt text of image](https://cdn-images-1.medium.com/max/1600/1*z3xaYTdqlYuwqju9JozZKw.png)

These helper functions read the object as a string and return the query as an object. An ObjectType is also essential to returning the data that is being queried. As stated in Graphene documentation, “it is the single definitive source of information about your data. It contains the essential fields and behaviors of the data you’re querying.”

When creating an ObjectType, you must create the “fields and behaviors” exactly the same as how it is presented in the API’s documentation. This will ensure that the data will be returned correctly.

![Alt text of image](https://cdn-images-1.medium.com/max/1600/1*0AFRp6MQu9SbiKc4HomW8A.png)

For instance, the <a href="https://developer.marvel.com/
">Marvel API Documentation</a> lists the fields that the Creator object has.. Most importantly, it shows what data type a field is. Using that information, you can create an ObjectType like this:

![Alt text of image](https://cdn-images-1.medium.com/max/1600/1*J7yzmvgy7gL8yioAbQPQxw.png)

As shown above, the ObjectType is similar to the way Marvel creates their Creator class. The main difference is that the datatype of each field is declared with Graphene functions: String(), List(), and Field(). A detailed explanation of all the different Graphene functions can be found in its  <a href="https://docs.graphene-python.org/en/latest/types/scalars/">documentation.</a>

<h1>Contact Us</h1>

If you like PyMarvelQL, please follow us on twitter <a href="https://twitter.com/novvumio">(@novvumio)</a> and give us a star 🌟 on <a href="https://github.com/Novvum/MarvelQL/tree/master/packages/python">Github!</a> If you find any issues, we’d love to fix them! You can submit them <a href="https://github.com/Novvum/MarvelQL/issues">here.</a>