1π
TLDR: instantiate new client right in the view method = new client for each request. (good option β overriding initial
view method). Improve later.
It depends.
If there are no request / user options for the client (= client is the same for every request / user):
-
import one from submodule as @ElPapi42 suggested (= same as instantiate new client in views.py globally, not as part of method logic)
-
or instantiate new client right in the view method β new client for each request
If there are request / user specific options (i.e. client need to have request.user
specific options / credentials):
- instantiate new client right in the view method
Option 1 β Client is instantiated once per worker view. Although it provides some performance benefits they are subtle and obscured:
- you may not know what it means: shared client => possibly use methods that modify client state for different requests / users that rely on the state
- worker lifetime is (should be) actually not that long
- hard to assess performance benefits with hard to assess proper implementation
In reality you may want to connect to github with some current request.user
specific options, perform multiple requests (= cookies may be involved and auto-added in subsequent requests => client cannot be shared with multiple users) = at least separate client for each user.
1π
As per definition a graphql client needs itβs own configuration, modules and error handling I would suggest you to create a namespace adjacent to your other projects. Django uses the concept of Applications which is providing seperated configuration, logging and error handling for this case. You define additional Applications in your project in your settings.py
in INSTALLED_APPS
(Docs). Your project structure would then look smth like:
.
βββ LICENSE
βββ README.md
βββ graphql-client
β βββ __init__.py
β βββ main.py
β βββ client.py
β βββ ressources
β β βββ __init__.py
β βββ βββ repository.py
β β βββ user.py
β βββ settings.py
β βββ views.py
βββ api
β βββ __init__.py
β βββ admin.py
β βββ apps.py
β βββ migrations
β β βββ __init__.py
β βββ models.py
β βββ tests.py
β βββ urls.py
β βββ views.py
βββ db.sqlite3
βββ manage.py
βββ portfolio
β βββ __init__.py
β βββ asgi.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ requirements.txt
βββ setup.py
- [Django]-How to automatic control uwsgi log size
- [Django]-SynchronousOnlyOperation error when upgrading from python 3.6 to python 3.7 using django channels
0π
I ended up creating a python module in the /api
Application called graphql.py
that init the graphql client. from here, it is easily importable.
I decide not to create a separate Django Application for this because that will be overkill on this context, due to the simplicity of the code:
in graphql.py
:
from gql import Client
from gql.transport.requests import RequestsHTTPTransport
from django.conf import settings
# Instaciate a graphql client
client = Client(
transport=RequestsHTTPTransport(
url='https://api.github.com/graphql',
headers={'Authorization': 'bearer {token}'.format(token=settings.GITHUB_TOKEN)},
verify=False,
retries=3,
),
fetch_schema_from_transport=True,
)
- [Django]-Python β Building wheel for lxml (setup.py) β¦ error
- [Django]-Iterate two dictionaries simultaneously in template
- [Django]-Maximum recursion depth exceeded on logout(request)