question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implementation Example?

See original GitHub issue

Hello! It’s great to have this, I’m just a little confused on implementation. I’ve added the authentication backends settings, the middleware settings, and the verify/refresh token mutations added.

This is probably obvious, but how do I create a login mutation? This is what I have from the howtographql tutorial:

from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from graphene import relay
from graphene_django import DjangoObjectType


class UserNode(DjangoObjectType):
    class Meta:
        model = User
        filter_fields = [
            'username',
        ]
        interfaces = (relay.Node, )


class LogIn(graphene.Mutation):
    user = graphene.Field(UserNode)

    class Arguments:
        username = graphene.String()
        password = graphene.String()

    def mutate(self, info, username, password):

        user = authenticate(
            username=username,
            password=password,
        )

        if not user:
            raise Exception('Invalid username or password')

        info.context.session['token'] = user.token
        return LogIn(user=user)


class Mutation(object):
        login = LogIn.Field()

Of course that’s looking for the token property for a user, which is not found. Any thoughts? Forgive my newness to django/graphql!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mongkokcommented, Nov 27, 2017

Hey @evanheckert!

welcome to GraphQL.

you can use a resolver method for the token and others non-model fields: http://docs.graphene-python.org/en/latest/types/objecttypes/#resolvers

import graphene

from django.contrib.auth import get_user_model
from graphene_django import DjangoObjectType

from graphql_jwt.utils import jwt_encode, jwt_payload

class UserNode(DjangoObjectType):
    token = graphene.String()

    class Meta:
        model = get_user_model()
        filter_fields = [
            'username',
        ]
        interfaces = (relay.Node,)

        def resolve_token(self, info, **kwargs):
            if info.context.user != self:
                return None

            payload = jwt_payload(self)
            return jwt_encode(payload)

Add Django login function to your mutation to attach authenticated user to the current session: https://docs.djangoproject.com/en/1.11/topics/auth/default/#how-to-log-a-user-in

import graphene

from django.contrib.auth import authenticate, login

class LogIn(graphene.Mutation):
    user = graphene.Field(UserNode)

    class Arguments:
        username = graphene.String()
        password = graphene.String()

    @classmethod
    def mutate(cls, root, info, username, password):
        user = authenticate(username=username, password=password)

        if user is None:
            raise Exception('Please enter a correct username and password')

        if not user.is_active:
            raise Exception('It seems your account has been disabled')

        login(info.context, user)
        return cls(user=user)
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is an implementation plan? 6 steps to create one
An implementation plan is a document that outlines the steps your team should take to accomplish a shared goal or initiative. Implementation ...
Read more >
What is Implementation?
Implementation can mean different things in different industries where goals and supporting actions vary. The following are some examples: Computer science and ...
Read more >
Example Implementation Plan - How to ...
Example Implementation Plan - How to Implement Business Strategy · 1. Mission, Vision and Values · 2. Strategic Plan · 3. Organizational Goals...
Read more >
Strategic Implementation: Definition, Plan, Process Examples
Strategic implementation is a process of turning plans into action using a specified strategy. · The five steps of the strategic implementation process...
Read more >
What is an Implementation Plan & How Do I Create One? ...
Here's a simple implementation plan example we've created using ProjectManager to help you better understand how implementation plans work.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found