Define object types for mutations
@@ -37,12 +37,14 @@ For the sake of simplicity, our mutations return bools, but really there is no s
type_def = """
type Mutation {
- login(username: String!, password: String!) {
- status: String!
+ login(username: String!, password: String!): LoginPayload
+ }
+
+ type LoginPayload {
+ status: Boolean!
error: Error
user: User
}
- }
"""
@@ -97,12 +99,14 @@ Imagine a mutation for creating ``Discussion`` that takes category, poster, titl
type_def = """
type Mutation {
- createDiscussion(category: ID!, title: String!, isAnnouncement: Boolean, isClosed: Boolean) {
+ createDiscussion(category: ID!, title: String!, isAnnouncement: Boolean, isClosed: Boolean): CreateDiscussionPayload
+ }
+
+ type CreateDiscussionPayload {
status: Boolean!
error: Error
discussion: Discussion
}
- }
"""
Our mutation takes only four arguments, but it is already too unwieldy to work with. Imagine adding another one or two arguments to it in future - its going to explode!
@@ -111,12 +115,14 @@ GraphQL provides a better way for solving this problem: ``input`` allows us to m
type_def = """
type Mutation {
- createDiscussion(input: DiscussionInput!) {
+ createDiscussion(input: DiscussionInput!): CreateDiscussionPayload
+ }
+
+ type CreateDiscussionPayload {
status: Boolean!
error: Error
discussion: Discussion
}
- }
input DiscussionInput {
category: ID!
@@ -151,21 +157,25 @@ Another advantage of ``input``-s is that they are reusable. If we later decide t
type_def = """
type Mutation {
- createDiscussion(input: DiscussionInput!) {
+ createDiscussion(input: DiscussionInput!): CreateDiscussionPayload
+ updateDiscussion(discussion: ID!, input: DiscussionInput!): UpdateDiscussionPayload
+ }
+
+ type CreateDiscussionPayload {
status: Boolean!
error: Error
discussion: Discussion
}
- updateDiscussion(discussion: ID!, input: DiscussionInput!) {
+
+ type UpdateDiscussionPayload {
status: Boolean!
error: Error
discussion: Discussion
}
- }
input DiscussionInput {
category: ID!
- title: String!,
+ title: String!
isAnnouncement: Boolean
isClosed: Boolean
}
