From 17e0d2a77a97419165aac7c0ab944d23ca2e6e65 Mon Sep 17 00:00:00 2001 From: Ken Powers Date: Tue, 1 Jan 2019 02:02:17 -0500 Subject: [PATCH 1/2] Update Learn-Schema.md Add `__typename` to the query to show that union types can still be differentiated on the client and add a second example showing common fields being queried in one spot. --- site/learn/Learn-Schema.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/site/learn/Learn-Schema.md b/site/learn/Learn-Schema.md index db6cdde821..7e3589d1a1 100644 --- a/site/learn/Learn-Schema.md +++ b/site/learn/Learn-Schema.md @@ -311,6 +311,7 @@ In this case, if you query a field that returns the `SearchResult` union type, y # { "graphiql": true} { search(text: "an") { + __typename ... on Human { name height @@ -327,6 +328,31 @@ In this case, if you query a field that returns the `SearchResult` union type, y } ``` +In this case, since `Human` and `Droid` share a common interface (`Character`), you can query their common fields in one place rather than having to repeat the same fields across multiple types: + +```graphql +{ + search(text: "an") { + __typename + ... on Character { + name + } + ... on Human { + height + } + ... on Droid { + primaryFunction + } + ... on Starship { + name + length + } + } +} +``` + +Note that `name` is still specified on `Starship` because otherwise it wouldn't show up in the results given that `Starship` is not a `Character`! + ### Input types So far, we've only talked about passing scalar values, like enums or strings, as arguments into a field. But you can also easily pass complex objects. This is particularly valuable in the case of mutations, where you might want to pass in a whole object to be created. In the GraphQL schema language, input types look exactly the same as regular object types, but with the keyword `input` instead of `type`: From 3b4163c575aa3789df0962144e7fc0b73a090954 Mon Sep 17 00:00:00 2001 From: Ken Powers Date: Tue, 1 Jan 2019 21:15:44 -0500 Subject: [PATCH 2/2] Add note about the use of `__typename` --- site/learn/Learn-Schema.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/site/learn/Learn-Schema.md b/site/learn/Learn-Schema.md index 7e3589d1a1..40a5b7ced5 100644 --- a/site/learn/Learn-Schema.md +++ b/site/learn/Learn-Schema.md @@ -328,7 +328,9 @@ In this case, if you query a field that returns the `SearchResult` union type, y } ``` -In this case, since `Human` and `Droid` share a common interface (`Character`), you can query their common fields in one place rather than having to repeat the same fields across multiple types: +The `__typename` field resolves to a `String` which lets you differentiate different data types from each other on the client. + +Also, in this case, since `Human` and `Droid` share a common interface (`Character`), you can query their common fields in one place rather than having to repeat the same fields across multiple types: ```graphql {