Prototype your Python models in GraphQL with Rust backend in one line of code

Dan Stein - DJ Fresh

Dan Stein - DJ Fresh

As 2022 drew to a close, and the distant stars of a million AI generated skies were everywhere you looked on your Galaxy Fold, I was doing some consulting for, helping with ideas for how to publish APIs around generative text to image models. While thinking about how researchers prototype their work, I realised that GraphQL was the perfect tool for quickly prototyping and sharing models.

The Python package inspired by these conversations, created with Chris Bishop and now called FastQL allows you to easily prototype and share models like Stable Diffusion, Midjourney or RunwayML in seconds. FastQL can be used to serve up any model or Python function and thanks to it’s use of Rust under the hood and the powerful Actix web framework, FastQL is lightning fast and keeps your API separate from your model code.

“FastQL allows you to easily prototype and share models like Stable Diffusion, Midjourney or RunwayML in seconds”

FastQL is incredibly simple to use. We’ve included simple steps and Docker files that allow you to quickly spin up example Hugging face diffusion models and start running them on AWS in minutes. We even included a fun prototype that allows you to fine tune Hugging Face diffusers models on your own GDrive photo uploads. You can find the FastQL package on pypi with code and resources on Github. Go ahead and install FastQL with

pip install fastqlapi.

This is how simple it is to spin up a blazing fast GraphQL API around your model…

from fastqlapi import fastql_server

def my_callback(**kwargs):
  input = kwargs[‘input’]
  # do some transformation to your input params
return {‘output’: [f”{input}”]}

  args={"input": { "type": "String!", "description": "This is my input param"}}, 
  fields={"output": { "type": "[String]", "description": "An array of image links"}

First define your schema as a Python dictionary, give your GraphQL query a name and pass:
fastql_server.start a callback to call when a GraphQL request arrives. Then pull out your defined arguments from the request, work your magic on them and finally return a dict containing the fields you’ve made available for the response.

When you run your code, a GraphQL server matching your schema definition will spin up automatically in a separate process.

Configuration for the API server is all environment variables. In development a GraphQL Playground UI will be served up at localhost:8000/graphql for you to test your model.

If you haven’t tried GraphQL yet, you are seriously missing out! Originally created by Facebook, GraphQL’s popularity has been exploding with it being involved in 22.7% of application integrations. According to the last dZone survey in 2020 usage of GraphQL had skyrocketed from its inception in 2018 to over 47% and in 2022 Apollo GraphQL was among the top 500 fastest growing tech companies. GraphQL is used in production by Meta, Airbnb, Adobe, Netflix, Twitter, Paypal, Github and over 2000 other large enterprise companies.

“In our benchmarks FastQL performance was nearly twice as fast as an Ariadne and FastAPI implementation with the same schema.”

Along with the many benefits of GraphQL such as the incredible developer experience and tools like GraphQL playground, Hasura and with auto-documentation out of the box. FastQL also works really well with Apollo Federation, enabling you to mix fixed and dynamic schemas to publish a federated API, aggregating multiple models into a single supergraph that is secure, simple to version and easy to extend.

The performance of Actix which is currently TechEmpower’s no.5 overall fastest web framework improves on the speed of native Python implementations and in our benchmarks FastQL performance was nearly twice as fast as an Ariadne and FastAPI implementation with the same schema, achieving sub 20ms response times according to Apollo tracing (available to activate by env variable in FastQL).

This is just the beginning…

This is a passion project, we’d love your help! Message me on Twitter @djfreshuk if you want to get involved ❤️

Thanks to the folks at Huggingface, ActiX, [email protected], the folks at Maturin and Nin, Danjo & everybody from the Stable Diffusion community for their help, libraries and inspiration!

⚡ Speed up your GraphQL API development

The GraphQL Editor is a supportive tool for both advanced GraphQL users as well as those taking their first steps with GraphQL APIs. Our all-in-one development environment for GraphQL will help you build, manage & deploy your GraphQL API much faster thanks to dozens of built-in micro features. Its graphical interface will also fix communication within your product team. Visualization is the key!

Try it for free