Skip to content

Commit 662e20a

Browse files
committed
schema-design: Address input type feedback.
1 parent fc187b5 commit 662e20a

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

docs/source/guides/schema-design.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -296,35 +296,48 @@ Following this pattern for mutations provides detailed information about the dat
296296

297297
<h3 id="mutation-input-types">Input types</h3>
298298

299-
Input types are a special type in GraphQL which are defined as arguments to queries and, more commonly, mutations. They can be thought of as object types for arguments, in addition to the other scalar types. Input types are especially useful when multiple mutations require similar information; for example, when creating a user and updating a user require the same fields, like `age` and `name`.
299+
Input types are a special type in GraphQL which allows an object to be passed as an argument to both queries and mutations and is helpful when simple scalar types aren't sufficient.
300300

301-
Input types are used like any other type and defining them is similar to a typical object type definitions, but with the `input` keyword rather than `type`.
301+
This allows arguments to be structured in an more manageable way, similar to how switching to an `options` argument might be appreciated when `function` arguments become too iterative.
302302

303-
Here is an example of two mutations that operate on a `User`, _without_ using input types:
303+
For example, consider this mutation which creates a post along with its accompanying media URLs (e.g. images):
304304

305-
```
305+
```graphql
306306
type Mutation {
307-
createUser(name: String, age: Int, address: String, phone: String): User
308-
updateUser(id: ID!, name: String, age: Int, address: String, phone: String): User
307+
createPost(title: String, body: String, mediaUrls: [String]): Post
309308
}
310309
```
311310

312-
To avoid the repetition of argument fields, this can be refactored to use an input type, as follows:
311+
This could be easier to digest, and the arguments would be easier to re-use within the mutation, by using an `input` type with the relevant fields.
313312

314-
```
313+
An input type is defined like a normal object type but using the `input` keyword. To introduce an `input` type for this example, we'd do:
314+
315+
```graphql
315316
type Mutation {
316-
createUser(user: UserInput): User
317-
updateUser(id: ID!, user: UserInput): User
317+
createPost(post: PostAndMediaInput): Post
318318
}
319319

320-
input UserInput {
321-
name: String
322-
age: Int
323-
address: String
324-
phone: String
320+
input PostAndMediaInput {
321+
title: String
322+
body: String
323+
mediaUrls: [String]
324+
}
325+
```
326+
327+
Not only does this facilitate passing the `PostAndMediaInput` around within the schema, it also provides a basis for annotating fields with descriptions which are automatically exposed by GraphQL-enabled tools:
328+
329+
```graphql
330+
input PostAndMediaInput {
331+
"A main title for the post"
332+
title: String
333+
"The textual body of the post."
334+
body: String
335+
"A list of URLs to render in the post."
336+
mediaUrls: [String]
325337
}
326338
```
327339

340+
Input types can also be used when different operations require the exact same information, though we urge caution on over-using this technique since changes to `input` types are breaking changes for all operations which utilize them.
328341

329342
<h2 id="gql">Wrapping documents with `gql`</h2>
330343

0 commit comments

Comments
 (0)