Установка, начало работы. Установка, начало работы. python django graphql graphene

Установка, начало работы.

Open in new window

Установка, начало работы.

pip install graphene_django

Добавляем в settings.py.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'graphene_django',
    'bet'
]

Определяем модель в приложении джанго.

class Fixture(models.Model):
    fixture_id = models.IntegerField(default=0)
    event_timestamp = models.IntegerField(default=0)
    event_date = models.DateField(blank=True, null=True)
    league_id = models.IntegerField(blank=True, null=True)
    homeTeam_id = models.IntegerField(blank=True, null=True)
    awayTeam_id = models.IntegerField(blank=True, null=True)
    homeTeam = models.CharField(max_length=150, blank=True, null=True)
    awayTeam = models.CharField(max_length=150, blank=True, null=True)
    status = models.CharField(max_length=150,blank=True, null=True)
    statusShort = models.CharField(max_length=15,blank=True, null=True)
    goalsHomeTeam = models.IntegerField(blank=True, null=True)
    goalsAwayTeam = models.IntegerField(blank=True, null=True)
    halftime_score = models.CharField(max_length=15,blank=True, null=True)
    final_score = models.CharField(max_length=15, blank=True, null=True)
    penalty = models.CharField(max_length=15, blank=True, null=True)
    elapsed = models.CharField(max_length=15, blank=True, null=True)
    firstHalfStart = models.CharField(max_length=15, blank=True, null=True)
    secondHalfStart = models.CharField(max_length=15, blank=True, null=True)

Создаем файл schema.py в приложении.

import graphene

from graphene_django.types import DjangoObjectType
from .models import Fixture

class FixtureType(DjangoObjectType):
    class Meta:
        model = Fixture

class Query(graphene.ObjectType):
    all_fixtures = graphene.List(FixtureType)
    def resolve_all_fixtures(self,info, **kwargs):
        return Fixture.objects.all()

schema = graphene.Schema(query=Query)

Определяем роутинг.

from django.contrib import admin
from django.urls import path
from graphene_django.views import GraphQLView


urlpatterns = [
    path('admin/', admin.site.urls),
    path('graphql/', GraphQLView.as_view(graphiql=True))
]

graphql django

Other topics