Introduction 🌋
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.
Try it out 😲
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 Github repository.
How it works 🏗
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.
Flask and Flask-GraphQL 🧪
“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.
Navigate to the /graphql endpoint to visit the GraphiQL playground to query comics, characters, creators and events.
Marvelous 🦸
“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:
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.
Graphene-Python 2.1 👌
“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:
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.
For instance, the Marvel API Documentation 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:
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 documentation.
Contact Us
If you like PyMarvelQL, please follow us on twitter (@novvumio) and give us a star 🌟 on Github! If you find any issues, we’d love to fix them! You can submit them here.
Top comments (0)