⬡ GraphQL
Beginner
What are inline fragments in GraphQL?
Answer
Inline fragments allow you to query fields on a specific concrete type within a union or interface. Syntax: ... on TypeName { fields }. Example with a union: query { search(term: "GraphQL") { ... on Book { title author } ... on Article { headline publication } } }. If the result is a Book, title and author are returned; if an Article, headline and publication. Use __typename to determine the concrete type at runtime: { search { __typename ... on Book { title } } }. Inline fragments are also used without a type condition as a way to apply directives to a group of fields: ... @include(if: $showDetails) { createdAt updatedAt }.