RFC 3 / Accepted

OneOf Input Objects

Opened on
Merged on

At a glance

Project
GraphQL Spec
Identifier
#825
Stage
RFC 3 / Accepted
Champion
@benjie
Updated
2025-09-01
Latest activity
Pull request merged on
Source
Related RFCs

Pull request description

Follow up of the @oneField directive and the Tagged type.

Introducing: OneOf Input Objects.

OneOf Input Objects are a special variant of Input Objects where the type system asserts that exactly one of the fields must be set and non-null, all others being omitted. This is represented in introspection with the __Type.isOneOf: Boolean field, and in SDL via the @oneOf directive on the input object.

This variant of an input object introduces a form of input polymorphism to GraphQL.

Example 1 - addPet

The following PetInput oneof input object lets you choose between a number of potential input types:

input PetInput @oneOf {
  cat: CatInput
  dog: DogInput
  fish: FishInput
}

input CatInput { name: String!, numberOfLives: Int }
input DogInput { name: String!, wagsTail: Boolean }
input FishInput { name: String!, bodyLengthInMm: Int }

type Mutation {
  addPet(pet: PetInput!): Pet
}

Example 2 - user(by:)

Previously you may have had a situation where you had multiple ways to locate a user:

type Query {
  user(id: ID!): User
  userByEmail(email: String!): User
  userByUsername(username: String!): User
  userByRegistrationNumber(registrationNumber: Int!): User
}

with OneOf Input Objects you can now express this via a single field without loss of type safety:

input UserBy @oneOf {
  id: ID
  email: String
  username: String
  registrationNumber: Int
}
type Query {
  user(by: UserBy!): User
}

FAQ

Why is this a directive?

At its core, it's a property of the type that's exposed through introspection - much in the same way that deprecation is. There's nothing in introspection, nor in the types exposed through the reference implementation (new GraphQLInputObjectType({ name: "...", isOneOf: true, ... })) that relates to directives. It just happens to be that after I analysed a number of potential syntaxes (including keywords and alternative syntax) I've found that when representing the schema as SDL, using a directive to do so is the least invasive (all current GraphQL parsers can already parse it!) and none of the alternative syntaxes sufficiently justified the increased complexity they would introduce.

Why is this a good approach?

This approach, as a small change to existing types, is the easiest to adopt of any of the solutions we came up with to the InputUnion problem. It's also more powerful in that it allows additional types to be part of the "input union" - in fact any valid input type is allowed: input objects, scalars, enums, and lists of the same. Further it can be used on top of existing GraphQL tooling, so it can be adopted much sooner. Finally it's very explicit, so doesn't suffer the issues that "duck typed" input unions could face.

Why did you go full circle via the tagged type?

When the @oneField directive was proposed some members of the community felt that augmenting the behaviour of existing types might not be the best approach, so the Tagged type was born. (We also researched a lot of other approaches too.) However, the Tagged type brought with it a lot of complexity and controversy, and the Input Unions Working Group decided that we should revisit the simpler approach again. This time around I'm a lot better versed in writing spec edits :grin:

Why are all the fields nullable? Shouldn't they be non-nullable?

To make this change minimally invasive I wanted:

  • to make it so that existing GraphQL clients could still validate queries against a oneOf-enabled GraphQL schema (if the fields were non-nullable the clients would think the query was invalid because it didn't supply enough data)
  • to allow existing GraphQL implementations to change as little code as possible

To accomplish this, we add the "exactly one value, and that value is non-null" as a validation rule that runs after all the existing validation rules - it's an additive change.

Can this allow a field to accept both a scalar and an object?

Yes!

type Query {
  findUser(by: FindUserBy!): User
}

input FindUserBy @oneOf {
  id: ID
  organizationAndRegistrationNumber: OrganizationAndRegistrationNumberInput
}

input OrganizationAndRegistrationNumberInput {
  organizationId: ID!
  registrationNumber: Int!
}

Can I use existing GraphQL clients to issue requests to OneOf-enabled schemas?

Yes - so long as you stick to the rules of one field / one argument manually - note that GraphQL already differentiates between a field not being supplied and a field being supplied with the value null.

Without explicit client support you may lose a little type safety, but all major GraphQL clients can already speak this language. Given this nonsense schema:

type Query {
  foo(by: FooBy!): String
}
input FooBy @oneOf {
  id: ID
  str1: String
  str2: String
}

the following are valid queries that you could issue from existing GraphQL clients:

  • {foo(by:{id: "..."})}
  • {foo(by:{str1: "..."})}
  • {foo(by:{str2: "..."})}
  • query Foo($by: FooBy!) {foo(by: $by)}

If my input object has only one field, should I use @oneOf?

Doing so would preserve your option value - making a OneOf Input Object into a regular Input Object is a non-breaking change (the reverse is a breaking change). In the case of having one field on your type changing it from oneOf (and nullable) to regular and non-null is a non-breaking change (the reverse is also true in this degenerate case). The two Example types below are effectively equivalent - both require that value is supplied with a non-null int:

input Example @oneOf {
  value: Int
}

input Example {
  value: Int!
}

Can we expand @oneOf to output types to allow for unions of objects, interfaces, scalars, enums and lists; potentially replacing the union type?

:shushing_face: :eyes: :wink:

Timeline

September 2025

July 2025

June 2025

May 2025

  • 2 commits pushed on
  • Advanced to RFC 3 on by leebyron
  • Discussed in GraphQL WG

    The GraphQL specification Working group discussed the deprecation of old versions of GraphQL JS, the proposal for defining a GraphQL request's response, and the clarification of the types of GraphQL responses. They also discussed the implementation of one-off features in GraphQL, Java, and GraphQL.js, the prevention of skip and include on route subscription selection sets, and the default value coercion rules. The group also discussed the need for a source of truth for built-in scalars and introspection types, the syntax for defining schemas in GraphQL, and the issue of error propagation in GraphQL.

    Zoom summary · GraphQL WG · 2025-05-01
    Next Steps

    Lee to review and merge the "one of" PR.

    Zoom summary · GraphQL WG · 2025-05-01

    Lee and Benjie discussed the implementation of one-off features in GraphQL, Java, and GraphQL.js, with Benjie suggesting a final check before merging. They also discussed the prevention of skip and include on route subscription selection sets, with Benjie stating that it has already been merged into GraphQL.js. Lee expressed interest in revisiting subscriptions in general to improve clarity. Benjie then brought up the default value coercion rules, which he believed had been merged into GraphQL.js and was good to go. Martin added a discussion about making include deprecated, non-nullable, which Lee agreed to merge.

    Zoom summary · GraphQL WG · 2025-05-01

April 2025

  • On the agenda for GraphQL WG

    11 listed attendees · 16 agenda items · 14 linked RFCs

    • Let's set a July 1st deadline for spec release (2m, Benjie)
    • Open Telemetry Sub Commitee announcement (2m, Pascal)
    • Add 'extensions' to request (5m, Benjie) #976
    • Provision for unknown request information (5m, Martin)
    • Defer/Stream Updates (10m, Rob) DeferStream
    • Benjie's editorial PRs (20m, Benjie)
    • Clarify "before execution begins" (10m, Benjie and Martin)
    • Disabling error propagation proposal (10m, Alex) #1153
    • Reaching a consensus on Semantic Nullability (25m, Alex) SemanticNullability wg#1700 #1065
    • Make includeDeprecated non-null (10m, Martin) #1142
    • Please give feedback on the following (2m, Martin)
    • Implementations may not deprecate a field that the interface hasn't deprecated (2m, Benjie) #1053
    • Prevent @skip and @include on root subscription selection set (5m, Benjie) #860
    • OneOf Input Objects (10m, Benjie) #825
    • Default value coercion rules (15m, Benjie) #793
    • Define Data Collections used in the spec (20m, Benjie) #1102 #1092 #1063

March 2025

October 2024

September 2024

July 2024

  • @benjie committed "Merge branch 'main' into oneof-v2" on
  • Discussed in GraphQL WG

    @oneOf update (10m, Benjie)

    • Adds polymorphism to input types. Proposed in 2021, oneOf multiple proposals. Like for being least breaking.
    • Been implemented in GraphQL JS and backported to stable release
    • Benjie: Go and use it
    • Benjie: Currently RFC 2, would love to move it to RFC 3 in the next few months
    • Jordan: What places are you looking feedback for? What should we look out for?
    • Benjie: personal concern: is it the good solution? Feels like we should mirror input and output. Now is a good time for a better, more symmetrical solution.
    • Michael: People are already doing this in the wild.
    • Michael: There was a cleaner proposal for inputUnion which looked good, but introduced a lot of other problems, like __typename on input types.
    • Benjie: I have personally dragged my heels on this because I would love us to adopt the struct type and that might solve this problem but it got feedback that it was too broad. Struct and oneOf could coexist, just something to be aware of.
    Meeting notes · GraphQL WG · 2024-07-18

June 2024

March 2024

November 2023

December 2022

  • On the agenda for GraphQL WG

    17 listed attendees · 9 agenda items · 3 linked RFCs

    • Agree to Membership Agreement, Participation & Contribution Guidelines and Code of Conduct (1m, Lee)
    • Introduction of attendees (5m, Lee)
    • Determine volunteers for note taking (1m, Lee)
    • Review agenda (2m, Lee)
    • Review previous meeting's action items (5m, Lee)
    • 🗳 2023 Technical Steering Committee (TSC) Elections 🗳
    • Meeting cadence for 2023 (5m, Lee)
    • Next steps for @oneOf (5m, Hugh) #825 Struct
    • Defer/Stream discussion (60m, Rob) #742

June 2022

  • On the agenda for GraphQL WG

    22 listed attendees · 14 agenda items · 7 linked RFCs

    • Agree to Membership Agreement, Participation & Contribution Guidelines and Code of Conduct (1m, Lee)
    • Introduction of attendees (5m, Lee)
    • Determine volunteers for note taking (1m, Lee)
    • Review agenda (2m, Lee)
    • Review previous meeting's action items (5m, Lee)
    • Proposal: Composite Schemas Working Group (10m, Benjie) CompositeSchemas
    • add @experimental directive (20m, Martin)
    • clarify root operation types (5m, Ben) #955
    • CCN status update (10m, Alex Reilly) #895
    • @oneOf update (20m, Benjie Gillam) #948 #825
    • "Unions implementing interface" update (10m, Yaacov Rydzinski) #939
    • "Unions of interfaces and/or unions" (15m, Yaacov Rydzinski) #950
    • General spec issues (15m, Roman Ivantsov)
    • Adding "meta" fields to SDL and introspection (15m, Ivan Goncharov)

May 2022

March 2022

January 2022

December 2021

October 2021

April 2021

March 2021

February 2021