RFC 3 / Accepted

Default value coercion rules

Opened on
Merged on

At a glance

Project
GraphQL Spec
Identifier
#793
Stage
RFC 3 / Accepted
Champion
@benjie
Updated
2026-07-07
Latest activity
Pull request merged on
Source

Pull request description

Coercing Field Arguments states:

5.c. Let defaultValue be the default value for argumentDefinition. [...] 5.h. If hasValue is not true and defaultValue exists (including null): 5.h.i. Add an entry to coercedValues named argumentName with the value defaultValue. [...] 5.j.iii.1. If value cannot be coerced according to the input coercion rules of argumentType, throw a field error. 5.j.iii.2. Let coercedValue be the result of coercing value according to the input coercion rules of argumentType. 5.j.iii.3. Add an entry to coercedValues named argumentName with the value coercedValue.

Here we note that there is no run-time coercion of defaultValue, which makes sense (why do at runtime that which can be done at build time?). However, there doesn't seem to be a rule that specifies that defaultValue must be coerced at all, which leads to consequences:

  1. you could use any value for defaultValue and it could break the type safety guarantees of GraphQL
  2. nested defaultValues are not applied

When building the following GraphQL schema programmatically (code below) with GraphQL.js:

type Query {
  example(inputObject: ExampleInputObject! = {}): Int
}

input ExampleInputObject {
  number: Int! = 3
}

And a resolver for Query.example:

resolve(source, args) {
  return args.inputObject.number;
}

You might expect the following queries to all gave the same result:

query A {
  example
}
query B {
  example(inputObject: {})
}
query C {
  example(inputObject: { number: 3 })
}
query D($inputObject: ExampleInputObject! = {}) {
  example(inputObject: $inputObject)
}

However, it turns out that query A's result differs:

{"example":null}
{"example":3}
{"example":3}
{"example":3}

This is because defaultValue for Query.example(inputObject:) was not coerced, so none of the defaultValues of ExampleInputObject were applied.

This is extremely unexpected, because looking at the GraphQL schema definition it looks like there's no circumstance under which ExampleInputObject may not have number as an integer; however when the defaultValue of Query.example(inputObject:) is used, the value of number is undefined.

<details> <summary>Example runnable with GraphQL.js</summary>
const {
  graphqlSync,
  printSchema,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLInputObjectType,
  GraphQLInt,
  GraphQLNonNull,
} = require("graphql");

const ExampleInputObject = new GraphQLInputObjectType({
  name: "ExampleInputObject",
  fields: {
    number: {
      type: new GraphQLNonNull(GraphQLInt),
      defaultValue: 3,
    },
  },
});

const Query = new GraphQLObjectType({
  name: "Query",
  fields: {
    example: {
      args: {
        inputObject: {
          type: new GraphQLNonNull(ExampleInputObject),
          defaultValue: {},
        },
      },
      type: GraphQLInt,
      resolve(source, args) {
        return args.inputObject.number;
      },
    },
  },
});

const schema = new GraphQLSchema({
  query: Query,
});

console.log(printSchema(schema));

// All four of these should be equivalent?
const source = /* GraphQL */ `
  query A {
    example
  }
  query B {
    example(inputObject: {})
  }
  query C {
    example(inputObject: { number: 3 })
  }
  query D($inputObject: ExampleInputObject! = {}) {
    example(inputObject: $inputObject)
  }
`;
const result1 = graphqlSync({ schema, source, operationName: "A" });
const result2 = graphqlSync({ schema, source, operationName: "B" });
const result3 = graphqlSync({ schema, source, operationName: "C" });
const result4 = graphqlSync({ schema, source, operationName: "D" });

console.log(JSON.stringify(result1.data));
console.log(JSON.stringify(result2.data));
console.log(JSON.stringify(result3.data));
console.log(JSON.stringify(result4.data));
</details>

This was raised against GraphQL.js back in 2016, but @IvanGoncharov closed it early last year stating that GraphQL.js conforms to the GraphQL Spec in this regard.

My proposal is that when a defaultValue is specified, the GraphQL implementation should coerce it to conform to the relevant type just like it does for runtime values as specified in Coercing Variable Values and Coercing Field Arguments.


This is validated for query documents (and schema defined as SDL), because:

Literal values must be compatible with the type expected in the position they are found as per the coercion rules defined in the Type System chapter. -- http://spec.graphql.org/draft/#sel-FALXDFDDAACFAhuF

But there doesn't seem to be any such assertion for GraphQL schemas defined in code.


GraphQL.js implementation: https://github.com/graphql/graphql-js/pull/3814

Timeline

July 2025

June 2025

  • Advanced to RFC 3 on by leebyron
  • Discussed in GraphQL WG
    Next Steps

    Lee: Perform final review of "default value coercion rules" RFC

    Zoom summary · GraphQL WG · 2025-06-05

    The team discussed several proposals and RFCs related to GraphQL specifications. Mark proposed updates to the spec and implementation, which Lee supported and encouraged for conclusion. Benjie presented updates on data collection trilogy and one-off tasks, which Lee approved for merging. Rob shared progress on execution result definitions, which Lee praised for improving the type system coherence. The team celebrated the completion of default value coercion rules, which Benjie and Lee had worked on for a long time. Lee committed to reviewing and merging several proposals, including prevent skip and include on route subscription and discussion section 3 type system and execution versus request error.

    Zoom summary · GraphQL WG · 2025-06-05

May 2025

  • Top comment edited on by benjie
  • 2 commits pushed on
  • 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

    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

February 2025

December 2023

June 2023

  • On the agenda for GraphQL WG

    12 listed attendees · 10 agenda items · 4 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 prior secondary meetings (5m, Lee)
    • Review previous meeting's action items (5m, Lee)
    • Default Value Validation Status Check (5m, Yaacov) #793
    • Clarification on Nested List Input Coercion (5m, Abhinand)
    • CollectSubFields (30m, Yaacov) #999 #1026
    • @deprecated on OBJECT - next steps (15m, Stephen) #997

March 2023

  • On the agenda for GraphQL WG

    11 listed attendees · 12 agenda items · 2 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 prior secondary meetings (5m, Lee)
    • Review previous meeting's action items (5m, Lee)
    • RFC "Root" operation types and schema definitions (15m, Lee) #1015
    • Discuss GraphiQL v3 big features and request-for-help (10m, Jonathan)
    • @graphiql/* npm write access issues blocking releases since January (10m, Rikki)
    • Default Value Validation Status Check (5m, Yaacov) #793
    • Defer & Stream (20m, Rob)
    • GraphQL over HTTP Status Codes (10m, Michael)

February 2023

  • On the agenda for GraphQL WG

    18 listed attendees · 12 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 prior secondary meetings (5m, Lee)
    • Review previous meeting's action items (5m, Lee)
    • TSC: Election Results (5m, TSC)
    • TSC: Management of build and publish infrastructure, secrets, etc (20m, Benjie / Rikki)
    • Fix ambiguity around when schema definition may be omitted (10m, Benjie) #987
    • Advance argument name uniqueness to Stage 3 (10m, Benjie) #891
    • @defer: fully deduplicate response (30m, Ivan)
    • Default Value Validation Status Check (5m, Yaacov) #793

January 2023

  • @benjie committed "Merge branch 'main' into input-object-default-value" on
  • On the agenda for GraphQL WG

    12 listed attendees · 10 agenda items · 2 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 prior secondary meetings (5m, Lee)
    • Review previous meeting's action items (5m, Lee)
    • Updates on Fragment Arguments (15m, Matt) #865
    • Review of pending approved PRs (15m, Roman)
    • Default Value Validation Status Check (15m, Yaacov) #793
    • Updates on defer/stream (15m, Rob)

January 2022

May 2021

March 2021

February 2021

January 2021

December 2020

November 2020