if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Run the python file:
Open the terminal or command prompt and navigate to the directory containing main.py.
Run the FastAPI server by executing the command: uvicorn main:app --reload.
main specifies the name of your Python file.
app refers to the FastAPI instance you defined.
--reload enables auto-reloading of the server on code changes (optional but helpful during development).
You can access your API using the URL http://localhost:8000 or http://127.0.0.1:8000 in your browser or any HTTP client like Postman.
This is just a basic example; you can add more routes and functionalities according to your requirements. FastAPI provides excellent documentation (https://fastapi.tiangolo.com/) with detailed examples and advanced features to explore.
pip install graphql-core
Create a Python file, e.g., app.py, and import the required modules:
from flask import Flask
from flask_graphql import GraphQLView
from graphene import ObjectType, String, Schema
app = Flask(__name__)
@app.route("/")
def root():
return "Welcome to the GraphQL API!"
app.add_url_rule(
"/graphql",
view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True)
)
Run the Flask app:
if __name__ == "__main__":
app.run(debug=True)
Open a web browser and navigate to http://localhost:5000/graphql to access the GraphiQL interface. Here, you can test and execute GraphQL queries and mutations.
Note: If you don't want to use Flask, you can use other frameworks like Django or FastAPI with compatible GraphQL libraries to create your API.
Remember to customize and extend the schema and resolvers according to your requirements. The Graphene documentation (https://graphene-python.org/) provides detailed examples and advanced features to explore in building GraphQL APIs with Python.