Skip to content

Commit 4e7dc06

Browse files
committed
Add oneOf blog post
1 parent 0b2d07c commit 4e7dc06

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ pnpm-lock.yaml
55
!src/pages/blog/2024-08-15-graphql-local-initiative.mdx
66
!src/pages/community/foundation/community-grant.mdx
77
!src/pages/blog/2025-06-01-graphiql-4/index.mdx
8+
!src/pages/blog/2025-06-19-multioption-inputs-with-oneof/index.mdx
89
*.jpg
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
title: Safer Multi-option Inputs with `@oneOf`
3+
tags: [announcements, spec]
4+
date: 2025-06-19
5+
byline: Benjie Gillam
6+
---
7+
8+
We’re excited to announce
9+
**[OneOf Input Objects](https://github.com/graphql/graphql-spec/pull/825)** has
10+
landed in the GraphQL specification! This enhancement solves a long-standing
11+
challenge in API design: how to allow users to provide **exactly one** of
12+
several possible options as input, in a clean and enforceable way. This feature
13+
is a small change that packs a big return for developers building modern digital
14+
products with GraphQL.
15+
16+
## Simplifying entrypoints
17+
18+
Most GraphQL queries start at a single node, and traverse the data graph from
19+
there. But often, there is more than one way of locating that node; for example
20+
you might find a user by their ID, email address, or username. Traditionally,
21+
that meant multiple root-level fields:
22+
23+
```graphql
24+
type Query {
25+
user(id: ID!): User
26+
userByEmail(email: String!): User
27+
userByUsername(username: String!): User
28+
}
29+
```
30+
31+
_(An alternative approach was a less-than-type-safe field presenting all the
32+
options along with custom runtime validation to enforce the constraints. Either
33+
way, neither solution was ideal.)_
34+
35+
With `@oneOf`, you can now consolidate those options into a single,
36+
user-friendly input which ensures users supply _exactly one_ input field, the
37+
value of which must not be null. The result: a user-friendly schema with fewer
38+
root-level fields and without sacrificing type safety:
39+
40+
```graphql
41+
input UserBy @oneOf {
42+
id: ID
43+
email: String
44+
username: String
45+
}
46+
47+
type Query {
48+
user(by: UserBy!): User
49+
}
50+
```
51+
52+
## Input polymorphism
53+
54+
Of course, `@oneOf`'s use isn't limited to simple scalarsit can also be used
55+
to choose between multiple complex input types, allowing for polymorphism in
56+
GraphQL inputs.
57+
58+
Imagine you were building a user-friendly blogging website, and each post is
59+
made up of elementsparagraphs, image galleries, block quotes, code blocks,
60+
and the like. Each of these elements come with their own set of (potentially
61+
overlapping) attributes, and you want to feed a list of them into your mutation.
62+
With @oneOf you can do so in a type safe manner:
63+
64+
```graphql
65+
type Mutation {
66+
createPost(elements: [PostElementInput]): Post
67+
}
68+
input PostElementInput @oneOf {
69+
paragraph: ParagraphInput
70+
blockquote: BlockQuoteInput
71+
gallery: GalleryInput
72+
}
73+
input ParagraphInput {
74+
text: String!
75+
}
76+
input BlockQuoteInput {
77+
text: String!
78+
attribution: String
79+
attributionUrl: String
80+
}
81+
input GalleryInput {
82+
imageUrls: [String!]!
83+
caption: String
84+
attribution: String
85+
}
86+
```
87+
88+
## What Makes `@oneOf` The Right Solution?
89+
90+
- **Backward Compatible**: Existing tools, queries and clients still work,
91+
meaning no major overhauls are required. Existing clients can even use oneOf
92+
inputs without updating; just be careful to always supply exactly one value!
93+
- **Minimal Complexity**: This feature introduces only a small change to the
94+
existing type system, but delivers very significant benefits.
95+
- **Type-Safe Input Polymorphism**: Offers a safe and scalable way to accept a
96+
variety of inputs under a single structuresomething previously hard to
97+
achieve in GraphQL.
98+
- **Now part of the GraphQL standard:** Several GraphQL implementations
99+
including Ruby, Java, JavaScript and .NET already ship `@oneOf` as a stable
100+
feature
101+
102+
## The Bottom Line
103+
104+
`@oneOf` allows for more expressive, capable, and less overwhelming schemas,
105+
helping technical teams to move faster with increased safety. Its simple and
106+
easy to implement, try it today!

0 commit comments

Comments
 (0)