Build a GraphQL API for Text Analysis with Python, Flask, and Fauna

04 May 2023 Balmiki Mandal 0 Python

How to Build a GraphQL API for Text Analytics with Python, Flask and Fauna

Can you imagine a world without information? That’s right, nothing - no internet, no books, no conversations, no news. Nowadays, it is almost impossible to go through a day without consulting information in almost any form. This makes text analytics solutions even more valuable. By understanding written content, companies can take advantage of the data that has been accumulating over the years.

In this article we will show you how to build a GraphQL API for text analytics using Python, Flask and Fauna. GraphQL is an open-source data query and manipulation language which provides an alternative to REST and other traditional database interfaces. It is often used as the interface between web services and client applications, enabling fast and efficient development.

Prerequisites

To follow along in this tutorial, you should be familiar with the following concepts:

  • Python programming language
  • Flask web framework
  • Fauna database technology
  • GraphQL query language

Step 1: Setting up the Environment

Before we can start building the GraphQL API, we need to set up the environment. First, you need to have Python installed on your machine. After Python is installed, you can create a directory for the project and install the necessary packages.

The packages which we will need are Flask, Fauna, GraphQL, and Flask-GraphQL.

To install these packages, open up a terminal window and type in the following commands one by one:

pip install Flask

pip install faunadb

pip install graphql

pip install flask-graphql

Once the packages are installed, we can move on to the next step.

Step 2: Creating the Database

In this step, we will create a database in Fauna to store the data. To do this, log in to Fauna and create a new database.

Once the database is created, run the following code to create the necessary collections:

client.query(
  q.CreateCollection({
    name: 'documents'
  }),
  q.CreateCollection({
    name: 'results'
  })
);

We will also need to create some indexes for our queries. To do this, run the following code:

client.query(
  q.CreateIndex({
    name: 'docs_by_id',
    source: q.Collection('documents'),
    terms: [
      { field: ['data', 'id'] }
    ],
    unique: true
  }),
  q.CreateIndex({
    name: 'res_by_docId',
    source: q.Collection('results'),
    terms: [
      { field: ['data', 'documentId'] }
    ],
    unique: false
  })
);

Once the database is ready, we can move on to the next step.

Step 3: Coding the Application

Now that the environment and database are set up, we can begin coding the application. First, create a file called main.py and add the following code:

from flask import Flask
from flask_graphql import GraphQLView 
import faunadb
import graphql

app = Flask(__name__)

client = faunadb.FaunaClient(secret='') #add your Fauna secret

schema = graphql.build_schema('''
type Query {
  documents: [Document]
  results: [Result]
}

type Document {
  id: ID!
  text: String
}

type Result {
  id: ID!
  documentId: String
  sentiment: String
}
''')

def get_documents():
    query = q.Map(lambda doc: q.Select('data', doc), 
    q.Paginate(q.Documents(q.Collection('documents'))))
    documents = client.query(query)
    return documents['data']

def get_results():
    query = q.Map(lambda res: q.Select('data', res), 
    q.Paginate(q.Documents(q.Collection('results')))) 
    results = client.query(query)
    return results['data']

def create_document(text):
    document_ref = client.query(
    q.Create(q.Collection('documents'), { "data": { "text": text }}))
    document = client.query(q.Get(document_ref))
    return document['data']

def create_result(documentId, sentiment):
    result_ref = client.query(
    q.Create(q.Collection('results'), { "data": { "documentId": documentId, "sentiment": sentiment }}))
    result = client.query(q.Get(result_ref))
    return result['data']

class Query(object):
    def __init__(self):
        pass
    documents = get_documents()
    results = get_results()

def root_query():
    query = Query()
    return query

def root_mutation():
    return {
        'createDocument': create_document,
        'createResult': create_result
    }

app.add_url_rule(
    '/graphql',
    view_func=GraphQLView.as_view(
        'graphql',
        schema=schema,
        graphiql=True, # for having the GraphiQL interface
        root_value=root_query,
        root_mutation=root_mutation
    )
)

if __name__ == '__main__':
    app.run()

Now run the application by typing in the following command:

python main.py

This will launch the application, and you can access the GraphiQL interface at http://localhost:5000/graphql. From here, you can write GraphQL queries and mutations to interact with the database.

Conclusion

In this article, we have shown you how to build a GraphQL API for text analytics using Python, Flask and Fauna. We have seen how to set up the environment, create the database and code the application. With this setup, you are now ready to make use of the power of text analytics and turn unstructured data into actionable insights.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.