Skip to content

Commit

Permalink
add graphene for graphql support and user app
Browse files Browse the repository at this point in the history
adds signup and jwt packages for authentication
  • Loading branch information
kadenbarlow committed Jan 13, 2019
1 parent 7c125b7 commit 295586b
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 4 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
bin/
lib/
include/
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
include
bin
lib
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Django==2.1.5
psycopg2==2.7.6.1
psycopg2==2.7.6.1
graphene-django>=2.0
PyJWT==1.7.1
django-graphql-jwt==0.2.0
16 changes: 16 additions & 0 deletions server/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import graphene
import users.schema
import graphql_jwt

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

class Mutation( users.schema.Mutation,
graphene.ObjectType):
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
revoke_token = graphql_jwt.Revoke.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)
25 changes: 25 additions & 0 deletions server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import os
from datetime import timedelta

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -31,14 +32,36 @@
# Application definition

INSTALLED_APPS = [
'users.apps.UserAppConfig',
'app.apps.AppConfig',

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'graphene_django',
]

GRAPHENE = {
'SCHEMA': 'server.schema.schema', # Where your Graphene schema lives
'MIDDLEWARE': [
'graphql_jwt.middleware.JSONWebTokenMiddleware',
],
}

AUTHENTICATION_BACKENDS = [
'graphql_jwt.backends.JSONWebTokenBackend',
'django.contrib.auth.backends.ModelBackend',
]

GRAPHQL_JWT = {
'JWT_VERIFY_EXPIRATION': False,
'JWT_EXPIRATION_DELTA': timedelta(minutes=5),
'JWT_REFRESH_EXPIRATION_DELTA': timedelta(days=7),
}

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down Expand Up @@ -84,6 +107,8 @@
}
}

AUTH_USER_MODEL = 'users.User'


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators
Expand Down
4 changes: 4 additions & 0 deletions server/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from graphene_django.views import GraphQLView
from django.views.decorators.csrf import csrf_exempt

urlpatterns = [
path('admin/', admin.site.urls),
url(r'^graphql', csrf_exempt(GraphQLView.as_view(graphiql=True))),
]
Empty file added users/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import User

admin.site.register(User, UserAdmin)
5 changes: 5 additions & 0 deletions users/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class UserAppConfig(AppConfig):
name = 'users'
44 changes: 44 additions & 0 deletions users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated by Django 2.1.5 on 2019-01-12 23:31

import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0009_alter_user_last_name_max_length'),
]

operations = [
migrations.CreateModel(
name='User',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
],
options={
'verbose_name': 'user',
'verbose_name_plural': 'users',
'abstract': False,
},
managers=[
('objects', django.contrib.auth.models.UserManager()),
],
),
]
Empty file added users/migrations/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions users/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
pass
41 changes: 41 additions & 0 deletions users/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import graphene
from graphene_django.types import DjangoObjectType
from users.models import User
from graphql import GraphQLError
from graphql_jwt.decorators import login_required

class UserType(DjangoObjectType):
class Meta:
model = User

class Query(graphene.ObjectType):
user = graphene.Field(
UserType,
description='Return Current User\'s Information'
)

@login_required
def resolve_user(self, info):
return info.context.user



class SignUp(graphene.Mutation):
class Arguments:
email = graphene.String(required=True)
password = graphene.String(required=True)

user = graphene.Field(UserType)

def mutate(self, info, email, password):
try:
user = User.objects.create_user(email, email, password)
user.save()
return SignUp(user)
except:
raise GraphQLError('Something went wrong while creating user')

class Mutation(graphene.ObjectType):
signup = SignUp.Field()


3 changes: 3 additions & 0 deletions users/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions users/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.

0 comments on commit 295586b

Please sign in to comment.