diff --git a/package/api/schema.py b/package/api/schema.py new file mode 100644 index 00000000..f1195417 --- /dev/null +++ b/package/api/schema.py @@ -0,0 +1,36 @@ +import graphene +from graphene_django.types import DjangoObjectType +from package.models import Category, Project, ProjectImage + + +class CategoryType(DjangoObjectType): + class Meta: + model = Category + fields = ('title', 'description') + + +class ProjectType(DjangoObjectType): + repository_url = graphene.Field(graphene.String) + + class Meta: + model = Project + fields = ('name', 'description', 'url', 'category', 'images',) + + def resolve_repository_url(self, info): + return self.repo_url + + +class ProjectImageType(DjangoObjectType): + class Meta: + model = ProjectImage + + +class Query(object): + categories = graphene.List(CategoryType) + projects = graphene.List(ProjectType) + + def resolve_categories(self, info, **kwargs): + return Category.objects.all() + + def resolve_projects(self, info, **kwargs): + return Project.objects.all() diff --git a/requirements.txt b/requirements.txt index 89601ee6..141dbf96 100644 --- a/requirements.txt +++ b/requirements.txt @@ -37,6 +37,8 @@ Pillow==4.2.1 rocketchat-API==0.6.3 markdown==2.6.11 python-dotenv==0.8.0 +graphene_django==2.8.2 +django-cors-headers==3.2.1 # Redis support django-redis==4.6.0 diff --git a/schema.py b/schema.py new file mode 100644 index 00000000..9762b2d7 --- /dev/null +++ b/schema.py @@ -0,0 +1,9 @@ +import graphene +import package.api.schema + + +class Query(package.api.schema.Query, graphene.ObjectType): + pass + + +schema = graphene.Schema(query=Query) diff --git a/settings/base.py b/settings/base.py index dba6c542..dca22751 100644 --- a/settings/base.py +++ b/settings/base.py @@ -82,6 +82,7 @@ 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'dj_pagination.middleware.PaginationMiddleware', 'social_auth_local.middleware.SocialAuthLocalExceptionMiddleware', + 'corsheaders.middleware.CorsMiddleware', ] TEMPLATES = [ @@ -141,6 +142,7 @@ "social_auth_local", "im", "timeline", + ] PREREQ_APPS = [ @@ -154,6 +156,7 @@ "django.contrib.humanize", "django.contrib.staticfiles", + # external "crispy_forms", "dj_pagination", @@ -167,6 +170,8 @@ 'rest_framework', 'chroniker', 'dynamic_preferences', + 'graphene_django', + 'corsheaders', ] INSTALLED_APPS = PREREQ_APPS + PROJECT_APPS @@ -541,3 +546,13 @@ CHRONIKER_CHECK_LOCK_FILE = False CHRONIKER_DISABLE_RAW_COMMAND = True CHRONIKER_EMAIL_SENDER = 'Chroniker' + +GRAPHENE = { + 'SCHEMA': 'schema.schema' +} +CORS_ORIGIN_WHITELIST = [ + 'http://localhost:8080', + 'https://localhost:8080', + ] + +CORS_ALLOW_CREDENTIALS = True diff --git a/urls.py b/urls.py index bbeee78a..3d17a878 100644 --- a/urls.py +++ b/urls.py @@ -19,6 +19,8 @@ from homepage.views import homepage, error_404_view, error_500_view, health_check_view, SitemapView from package.views import category, python3_list from django.contrib.auth.views import logout as contrib_logout_view +from graphene_django.views import GraphQLView +from django.views.decorators.csrf import csrf_exempt urlpatterns = [ @@ -81,6 +83,9 @@ view=apiv1_gone, name="apiv1_gone", ), + # url(r'^graphql/$', GraphQLView.as_view(graphiql=True)), + url(r'^explore', GraphQLView.as_view(graphiql=True)), + url(r'^graphql', csrf_exempt(GraphQLView.as_view())), # url(r'^api/v1/', include('core.apiv1', namespace="apitest")),