Skip to content

Commit

Permalink
Add first version of graphql-python, yay
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonatas Baldin committed Aug 15, 2017
1 parent 2bd20b1 commit 8183432
Show file tree
Hide file tree
Showing 29 changed files with 695 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
@@ -0,0 +1,26 @@
# graphql-python
A Hacketnews project developed for the [How to GraphQL tutorial](https://www.howtographql.com/graphql-python/0-introduction/) using Python, Django and Graphene. Tested on Python 3.6.

## Installation and Usage
Clone the project.

Create a virtual environment:
```bash
python3.6 -m venv venv
source venv/bin/activate
```

Install everything needed:
```bash
pip install -r requirements.txt
```

Create the database and run the server:
```
python hackernews/manage.py migrate
python hackernews/manage.py runserver
```

You should be able to access the server on [here](http://localhost:8000/graphql).

To get the most of the project, please read the [tutorial](https://www.howtographql.com/graphql-python/0-introduction/).
Empty file.
26 changes: 26 additions & 0 deletions hackernews/hackernews/schema.py
@@ -0,0 +1,26 @@
import graphene

import links.schema
import links.schema_relay
import users.schema


class Query(
users.schema.Query,
links.schema.Query,
links.schema_relay.RelayQuery,
graphene.ObjectType
):
pass


class Mutation(
users.schema.Mutation,
links.schema.Mutation,
links.schema_relay.RelayMutation,
graphene.ObjectType,
):
pass


schema = graphene.Schema(query=Query, mutation=Mutation)
130 changes: 130 additions & 0 deletions hackernews/hackernews/settings.py
@@ -0,0 +1,130 @@
"""
Django settings for hackernews project.
Generated by 'django-admin startproject' using Django 1.11.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ter^mmllz+zk!g^6ht#1r82i4cm+i6$csgoacgsq%4)hmp6-02'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

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

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'hackernews.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'hackernews.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'

GRAPHENE = {
'SCHEMA': 'hackernews.schema.schema',
}

AUTH_USER_MODEL = 'users.User'
25 changes: 25 additions & 0 deletions hackernews/hackernews/urls.py
@@ -0,0 +1,25 @@
"""hackernews URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from django.views.decorators.csrf import csrf_exempt

from graphene_django.views import GraphQLView

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^graphql/', csrf_exempt(GraphQLView.as_view(graphiql=True))),
]
16 changes: 16 additions & 0 deletions hackernews/hackernews/wsgi.py
@@ -0,0 +1,16 @@
"""
WSGI config for hackernews project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hackernews.settings")

application = get_wsgi_application()
Empty file added hackernews/links/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions hackernews/links/admin.py
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

# Register your models here.
8 changes: 8 additions & 0 deletions hackernews/links/apps.py
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.apps import AppConfig


class LinksConfig(AppConfig):
name = 'links'
24 changes: 24 additions & 0 deletions hackernews/links/migrations/0001_initial.py
@@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2017-07-31 23:29
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Link',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('url', models.URLField()),
('description', models.TextField(blank=True, null=True)),
],
),
]
23 changes: 23 additions & 0 deletions hackernews/links/migrations/0002_link_posted_by.py
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2017-08-08 22:47
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('links', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='link',
name='posted_by',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
26 changes: 26 additions & 0 deletions hackernews/links/migrations/0003_vote.py
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.3 on 2017-08-08 23:40
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('links', '0002_link_posted_by'),
]

operations = [
migrations.CreateModel(
name='Vote',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('link', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='votes', to='links.Link')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
Empty file.
16 changes: 16 additions & 0 deletions hackernews/links/models.py
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.

class Link(models.Model):
url = models.URLField()
description = models.TextField(null=True, blank=True)
posted_by = models.ForeignKey('users.User', null=True)


class Vote(models.Model):
user = models.ForeignKey('users.User')
link = models.ForeignKey('links.Link', related_name='votes')

0 comments on commit 8183432

Please sign in to comment.