Graphene Django - Mutation with one to many relation foreign key












1















I would like to know how to properly create mutation for creating this django model:



class Company(models.Model):

class Meta:
db_table = 'companies'
app_label = 'core'
default_permissions = ()

name = models.CharField(unique=True, max_length=50, null=False)
email = models.EmailField(unique=True, null=False)
phone_number = models.CharField(max_length=13, null=True)
address = models.TextField(max_length=100, null=False)
crn = models.CharField(max_length=20, null=False)
tax = models.CharField(max_length=20, null=False)
parent = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
currency = models.ForeignKey(Currency, null=False, on_delete=models.CASCADE)
country = models.ForeignKey(Country, null=False, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)


As you see, there are three Foreign keys. For model Currency, Country and Parent(self). Company DjangoObjectType looks very simple like this:



class CompanyType(DjangoObjectType):
class Meta:
model = Company


And finally my mutation class CreateCompany have Currency, Country and Self(Parent) defined like graphene.Field():



class CompanyInput(graphene.InputObjectType):
name = graphene.String(required=True)
email = graphene.String(required=True)
address = graphene.String(required=True)
crn = graphene.String(required=True)
tax = graphene.String(required=True)
currency = graphene.Field(CurrencyType)
country = graphene.Field(CountryType)
parent = graphene.Field(CompanyType)
phone_number = graphene.String()


class CreateCompany(graphene.Mutation):
company = graphene.Field(CompanyType)

class Arguments:
company_data = CompanyInput(required=True)

@staticmethod
def mutate(root, info, company_data):
company = Company.objects.create(**company_data)
return CreateCompany(company=company)


When i want to start django server, Assertion error will be raised.



AssertionError: CompanyInput.currency field type must be Input Type but got: CurrencyType.


I was finding some good tutorial for one to many foreign key for a long time, so if someone know how to implement this solution nice and clear I would be very glad.



PS: Please can you also show me example of GraphQL query, so I would know how to call that mutation? Thank you very much.










share|improve this question

























  • Have you found a solution to this issue?

    – KeykoYume
    Jan 7 at 13:01











  • @KeykoYume Yes. To graphene.Field goes InputType not Type object. As you see class CompanyInput. All you need to do is change for example graphene.Field(CurrencyType) to graphene.Field(CurrencyInput). The same class for currency like CompanyInput for company.

    – idature
    Jan 7 at 14:23











  • Could you please write up the answer along with what you defined for CurrencyInput and the associated graphql query? I am having trouble with a create mutation because of a foreign-key field, I've tried implementing your above approach but obviously, I am short of something important.

    – jupiar
    Feb 12 at 10:17
















1















I would like to know how to properly create mutation for creating this django model:



class Company(models.Model):

class Meta:
db_table = 'companies'
app_label = 'core'
default_permissions = ()

name = models.CharField(unique=True, max_length=50, null=False)
email = models.EmailField(unique=True, null=False)
phone_number = models.CharField(max_length=13, null=True)
address = models.TextField(max_length=100, null=False)
crn = models.CharField(max_length=20, null=False)
tax = models.CharField(max_length=20, null=False)
parent = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
currency = models.ForeignKey(Currency, null=False, on_delete=models.CASCADE)
country = models.ForeignKey(Country, null=False, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)


As you see, there are three Foreign keys. For model Currency, Country and Parent(self). Company DjangoObjectType looks very simple like this:



class CompanyType(DjangoObjectType):
class Meta:
model = Company


And finally my mutation class CreateCompany have Currency, Country and Self(Parent) defined like graphene.Field():



class CompanyInput(graphene.InputObjectType):
name = graphene.String(required=True)
email = graphene.String(required=True)
address = graphene.String(required=True)
crn = graphene.String(required=True)
tax = graphene.String(required=True)
currency = graphene.Field(CurrencyType)
country = graphene.Field(CountryType)
parent = graphene.Field(CompanyType)
phone_number = graphene.String()


class CreateCompany(graphene.Mutation):
company = graphene.Field(CompanyType)

class Arguments:
company_data = CompanyInput(required=True)

@staticmethod
def mutate(root, info, company_data):
company = Company.objects.create(**company_data)
return CreateCompany(company=company)


When i want to start django server, Assertion error will be raised.



AssertionError: CompanyInput.currency field type must be Input Type but got: CurrencyType.


I was finding some good tutorial for one to many foreign key for a long time, so if someone know how to implement this solution nice and clear I would be very glad.



PS: Please can you also show me example of GraphQL query, so I would know how to call that mutation? Thank you very much.










share|improve this question

























  • Have you found a solution to this issue?

    – KeykoYume
    Jan 7 at 13:01











  • @KeykoYume Yes. To graphene.Field goes InputType not Type object. As you see class CompanyInput. All you need to do is change for example graphene.Field(CurrencyType) to graphene.Field(CurrencyInput). The same class for currency like CompanyInput for company.

    – idature
    Jan 7 at 14:23











  • Could you please write up the answer along with what you defined for CurrencyInput and the associated graphql query? I am having trouble with a create mutation because of a foreign-key field, I've tried implementing your above approach but obviously, I am short of something important.

    – jupiar
    Feb 12 at 10:17














1












1








1


1






I would like to know how to properly create mutation for creating this django model:



class Company(models.Model):

class Meta:
db_table = 'companies'
app_label = 'core'
default_permissions = ()

name = models.CharField(unique=True, max_length=50, null=False)
email = models.EmailField(unique=True, null=False)
phone_number = models.CharField(max_length=13, null=True)
address = models.TextField(max_length=100, null=False)
crn = models.CharField(max_length=20, null=False)
tax = models.CharField(max_length=20, null=False)
parent = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
currency = models.ForeignKey(Currency, null=False, on_delete=models.CASCADE)
country = models.ForeignKey(Country, null=False, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)


As you see, there are three Foreign keys. For model Currency, Country and Parent(self). Company DjangoObjectType looks very simple like this:



class CompanyType(DjangoObjectType):
class Meta:
model = Company


And finally my mutation class CreateCompany have Currency, Country and Self(Parent) defined like graphene.Field():



class CompanyInput(graphene.InputObjectType):
name = graphene.String(required=True)
email = graphene.String(required=True)
address = graphene.String(required=True)
crn = graphene.String(required=True)
tax = graphene.String(required=True)
currency = graphene.Field(CurrencyType)
country = graphene.Field(CountryType)
parent = graphene.Field(CompanyType)
phone_number = graphene.String()


class CreateCompany(graphene.Mutation):
company = graphene.Field(CompanyType)

class Arguments:
company_data = CompanyInput(required=True)

@staticmethod
def mutate(root, info, company_data):
company = Company.objects.create(**company_data)
return CreateCompany(company=company)


When i want to start django server, Assertion error will be raised.



AssertionError: CompanyInput.currency field type must be Input Type but got: CurrencyType.


I was finding some good tutorial for one to many foreign key for a long time, so if someone know how to implement this solution nice and clear I would be very glad.



PS: Please can you also show me example of GraphQL query, so I would know how to call that mutation? Thank you very much.










share|improve this question
















I would like to know how to properly create mutation for creating this django model:



class Company(models.Model):

class Meta:
db_table = 'companies'
app_label = 'core'
default_permissions = ()

name = models.CharField(unique=True, max_length=50, null=False)
email = models.EmailField(unique=True, null=False)
phone_number = models.CharField(max_length=13, null=True)
address = models.TextField(max_length=100, null=False)
crn = models.CharField(max_length=20, null=False)
tax = models.CharField(max_length=20, null=False)
parent = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
currency = models.ForeignKey(Currency, null=False, on_delete=models.CASCADE)
country = models.ForeignKey(Country, null=False, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)


As you see, there are three Foreign keys. For model Currency, Country and Parent(self). Company DjangoObjectType looks very simple like this:



class CompanyType(DjangoObjectType):
class Meta:
model = Company


And finally my mutation class CreateCompany have Currency, Country and Self(Parent) defined like graphene.Field():



class CompanyInput(graphene.InputObjectType):
name = graphene.String(required=True)
email = graphene.String(required=True)
address = graphene.String(required=True)
crn = graphene.String(required=True)
tax = graphene.String(required=True)
currency = graphene.Field(CurrencyType)
country = graphene.Field(CountryType)
parent = graphene.Field(CompanyType)
phone_number = graphene.String()


class CreateCompany(graphene.Mutation):
company = graphene.Field(CompanyType)

class Arguments:
company_data = CompanyInput(required=True)

@staticmethod
def mutate(root, info, company_data):
company = Company.objects.create(**company_data)
return CreateCompany(company=company)


When i want to start django server, Assertion error will be raised.



AssertionError: CompanyInput.currency field type must be Input Type but got: CurrencyType.


I was finding some good tutorial for one to many foreign key for a long time, so if someone know how to implement this solution nice and clear I would be very glad.



PS: Please can you also show me example of GraphQL query, so I would know how to call that mutation? Thank you very much.







django django-models graphql graphene-python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:26









Mark Chackerian

9,15037068




9,15037068










asked Nov 20 '18 at 10:35









idatureidature

6610




6610













  • Have you found a solution to this issue?

    – KeykoYume
    Jan 7 at 13:01











  • @KeykoYume Yes. To graphene.Field goes InputType not Type object. As you see class CompanyInput. All you need to do is change for example graphene.Field(CurrencyType) to graphene.Field(CurrencyInput). The same class for currency like CompanyInput for company.

    – idature
    Jan 7 at 14:23











  • Could you please write up the answer along with what you defined for CurrencyInput and the associated graphql query? I am having trouble with a create mutation because of a foreign-key field, I've tried implementing your above approach but obviously, I am short of something important.

    – jupiar
    Feb 12 at 10:17



















  • Have you found a solution to this issue?

    – KeykoYume
    Jan 7 at 13:01











  • @KeykoYume Yes. To graphene.Field goes InputType not Type object. As you see class CompanyInput. All you need to do is change for example graphene.Field(CurrencyType) to graphene.Field(CurrencyInput). The same class for currency like CompanyInput for company.

    – idature
    Jan 7 at 14:23











  • Could you please write up the answer along with what you defined for CurrencyInput and the associated graphql query? I am having trouble with a create mutation because of a foreign-key field, I've tried implementing your above approach but obviously, I am short of something important.

    – jupiar
    Feb 12 at 10:17

















Have you found a solution to this issue?

– KeykoYume
Jan 7 at 13:01





Have you found a solution to this issue?

– KeykoYume
Jan 7 at 13:01













@KeykoYume Yes. To graphene.Field goes InputType not Type object. As you see class CompanyInput. All you need to do is change for example graphene.Field(CurrencyType) to graphene.Field(CurrencyInput). The same class for currency like CompanyInput for company.

– idature
Jan 7 at 14:23





@KeykoYume Yes. To graphene.Field goes InputType not Type object. As you see class CompanyInput. All you need to do is change for example graphene.Field(CurrencyType) to graphene.Field(CurrencyInput). The same class for currency like CompanyInput for company.

– idature
Jan 7 at 14:23













Could you please write up the answer along with what you defined for CurrencyInput and the associated graphql query? I am having trouble with a create mutation because of a foreign-key field, I've tried implementing your above approach but obviously, I am short of something important.

– jupiar
Feb 12 at 10:17





Could you please write up the answer along with what you defined for CurrencyInput and the associated graphql query? I am having trouble with a create mutation because of a foreign-key field, I've tried implementing your above approach but obviously, I am short of something important.

– jupiar
Feb 12 at 10:17












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391097%2fgraphene-django-mutation-with-one-to-many-relation-foreign-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391097%2fgraphene-django-mutation-with-one-to-many-relation-foreign-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings