Graphene-django: When creating Mutation I get: AttributeError: 'Options' object has no attribute 'name'

Created on 1 May 2017  路  6Comments  路  Source: graphql-python/graphene-django

Created simple Queries with no issues. No trying to create a Mutation and getting this error.

  File "C:\Program Files\Python36\lib\site-packages\graphene\types\typemap.py",
line 236, in construct_fields_for_type
    field_type = self.get_field_type(map, field.type)
  File "C:\Program Files\Python36\lib\site-packages\graphene\types\typemap.py",
line 295, in get_field_type
    return map.get(type._meta.name)
AttributeError: 'Options' object has no attribute 'name'

This is the code

class AddProduct(graphene.Mutation):
    class Input:
        name = String(required=True)

    product = Field(Product)
    org = Field(Org)

    @classmethod
    def mutate(cls, input, context, info):
        product_name = input.get('product_name')
        org = Org.objects.first();
        desc = "description whatever...."
        product = Product(name = product_name, org = org, description = desc)

        return AddProduct(product = product)

class Mutation(ObjectType):
    add_product = AddProduct.Field()

Most helpful comment

Without knowing more about your code, I assume you are using Product and Org from your models and not making them into graphql types. If you make product and Org into graphene types this should work.

All 6 comments

Without knowing more about your code, I assume you are using Product and Org from your models and not making them into graphql types. If you make product and Org into graphene types this should work.

It worked after I changed both, the error is gone. Using OrgNode and ProductNode now which are both DjangoObjectType.

However in the mutate method, do I still have to use model class to retrieve the Org I want?

org = Org.objects.first();

Here's the whole code now.

orgs.schema.py

import graphene
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Org


class OrgNode(DjangoObjectType):
    class Meta:
        model = Org
        interfaces = (graphene.relay.Node, )


class Query(graphene.AbstractType):
    all_orgs = DjangoFilterConnectionField(OrgNode)

    def resolve_all_orgs(self, args, context, info):
        return Org.objects.all()

products.schema.py

import graphene
from graphene import relay, ObjectType, AbstractType, List, String, Field
from graphene_django.types import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from .models import Product
from orgs.models import Org



class ProductNode(DjangoObjectType):
    class Meta:
        model = Product
        filter_fields = {
            'name': ['exact', 'icontains', 'istartswith'],
            'org__name': ['icontains'],
            'org__admin__username':['icontains'],
        }
        interfaces = (relay.Node, )


class AddProduct(graphene.Mutation):
    class Input:
        name = String(required=True)

    product = Field(ProductNode)
    org = Field(OrgNode)

    @classmethod
    def mutate(cls, input, context, info):
        product_name = input.get('product_name')
        org = Org.objects.first();
        desc = "description whatever...."
        product = ProductNode(name = product_name, org = org, description = desc)

        return AddProduct(product = product)


class Query(AbstractType):
    all_products = DjangoFilterConnectionField(ProductNode)

    def resolve_all_products(self, args, context, info):
        return Product.objects.all()

class Mutation(ObjectType):
    add_product = AddProduct.Field()


However in the mutate method, do I still have to use model class to retrieve the Org I want?
yes, you need to query using django models and then you can return those in your mutate method and they will automatically get mapped to graphql types.

Scanning over your code quickly i see one problem. you need to return org in your mutate method
return AddProduct(product = product, org=org)

Return org=org, or org=OrgNode(org), and what is the difference? Isn't it only a matter what will get back as payload?

Obviously I would need to know what _DjangoObjectType_ does and what all powers does it have, but that is not very well documented. It would be great to see the complete reference for these basic types.

Having also just stumbled on this just following the mutation example in the docs ( http://docs.graphene-python.org/en/latest/types/mutations/) , it seems that the exception message could be a bit more helpful.

This one tripped me up too! Part of the confusion for me is the examples and docs using inconsistent naming for the various classes. An explicit naming convention like OrgModel, OrgType and OrgNode (and not the ambiguous Org) would help here, as well as a more helpful error message.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amiyatulu picture amiyatulu  路  3Comments

MilanRgm picture MilanRgm  路  3Comments

nickhudkins picture nickhudkins  路  3Comments

Dawidpol picture Dawidpol  路  4Comments

hyusetiawan picture hyusetiawan  路  4Comments