Graphene Django - Mutation with one to many relation foreign key
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
django django-models graphql graphene-python
edited Nov 21 at 19:26
Mark Chackerian
8,47536867
8,47536867
asked Nov 20 at 10:35
idature
367
367
add a comment |
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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