Guest User

Untitled

a guest
Jan 10th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. class CreatePerson(graphene.Mutation):
  2. class Input:
  3. name = graphene.String()
  4. age = graphene.Int()
  5.  
  6. ok = graphene.Boolean()
  7. person = graphene.Field(lambda: Person)
  8.  
  9. @staticmethod
  10. def mutate(root, args, context, info):
  11. person = Person(name=args.get('name'), age=args.get('age'), mobile=args.get('mobile'))
  12. ok = True
  13. return CreatePerson(person=person, ok=ok)
  14.  
  15. class PersonInput(InputObjectType):
  16. name = graphene.String()
  17. age = graphene.Int()
  18.  
  19. class CreatePeople(graphene.Mutation):
  20. class Input:
  21. people = graphene.List(PersonInput)
  22.  
  23. people = graphene.List(lambda: Person)
  24.  
  25. @staticmethod
  26. def mutate(root, args, context, info):
  27. people = [Person(name=person.name, age=person.age) for person in args.get('people')]
  28. ok = True
  29. return CreatePeople(people=people, ok=ok)
  30.  
  31. class CreatePerson(graphene.Mutation):
  32. class Input:
  33. name = graphene.List(graphene.String)
  34.  
  35. ok = graphene.Boolean()
  36. people = graphene.List(Person)
  37.  
  38. @staticmethod
  39. def mutate(root, args, context, info):
  40. people = [Person(name=name) for name in args.get('name)]
  41. ok = True
  42. return CreatePerson(people=people, ok=ok)
  43.  
  44. mutation {
  45. c001: createPerson(
  46. name: "Donald Duck"
  47. age: 42
  48. ) {
  49. id
  50. }
  51.  
  52. c002: createPerson(
  53. name: "Daisy Duck"
  54. age: 43
  55. ) {
  56. id
  57. }
  58.  
  59. c003: createPerson(
  60. name: "Mickey Mouse"
  61. age: 44
  62. ) {
  63. id
  64. }
  65. }
  66.  
  67. class UserType(DjangoObjectType):
  68.  
  69. class Meta:
  70. model = User
  71. interfaces = (CustomGrapheneNode, )
  72. filter_fields = {}
  73. only_fields = (
  74. 'name',
  75. 'email'
  76. )
  77.  
  78. class UserInput(graphene.InputObjectType):
  79. name = graphene.String(required=True)
  80. password = graphene.String(required=True)
  81.  
  82. class CreateUser(graphene.Mutation):
  83. users = graphene.List(UserType)
  84.  
  85. class Input:
  86. data = graphene.List(UserInput)
  87.  
  88. Output = UserType
  89.  
  90. def mutate(self, info, data):
  91. users = []
  92. for item in data:
  93. user = User.objects.create(name=data['name'],
  94. password=data['password'])
  95. users.append(user)
  96. return CreateUser(users=users)
  97.  
  98. class Mutation():
  99. create_user = CreateUser.Field()
  100.  
  101. mutation{
  102. createUser(data:[{name:"john", password:"1234"},
  103. {name:"john", password:"1234"}]) {
  104. user{
  105. name
  106. }
  107. }
  108. }
Add Comment
Please, Sign In to add comment