Skip to content

Commit

Permalink
Preserve defaultValue literals
Browse files Browse the repository at this point in the history
Fixes graphql#3051

This change solves the problem of default values defined via SDL not always resolving correctly through introspection by preserving the original GraphQL literal in the schema definition. This changes argument and input field definitions `defaultValue` field from just the "value" to a new `GraphQLDefaultValueUsage` type which contains either or both "value" and "literal" fields.

Since either of these fields may be set, new functions for resolving a value or literal from either have been added - `getLiteralDefaultValue` and `getCoercedDefaultValue` - these replace uses that either assumed a set value or were manually converting a value back to a literal.

Here is the flow for how a default value defined in an SDL would be converted into a functional schema and back to an SDL:

**Before this change:**

```
(SDL) --parse-> (AST) --coerceInputLiteral--> (defaultValue config) --valueToAST--> (AST) --print --> (SDL)
```

`coerceInputLiteral` performs coercion which is a one-way function, and `valueToAST` is unsafe and set to be deprecated in graphql#3049.

**After this change:**

```
(SDL) --parse-> (defaultValue literal config) --print --> (SDL)
```
  • Loading branch information
leebyron authored and yaacovCR committed Sep 17, 2024
1 parent d512a52 commit 4ccc601
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/utilities/__tests__/coerceInputValue-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -892,3 +892,26 @@ describe('coerceDefaultValue', () => {
expect(parseValueCalls).to.deep.equal(['hello']);
});
});

describe('coerceDefaultValue', () => {
it('memoizes coercion', () => {
const parseValueCalls: any = [];

const spyScalar = new GraphQLScalarType({
name: 'SpyScalar',
parseValue(value) {
parseValueCalls.push(value);
return value;
},
});

const defaultValueUsage = {
literal: { kind: Kind.STRING, value: 'hello' },
} as const;
expect(coerceDefaultValue(defaultValueUsage, spyScalar)).to.equal('hello');

// Call a second time
expect(coerceDefaultValue(defaultValueUsage, spyScalar)).to.equal('hello');
expect(parseValueCalls).to.deep.equal(['hello']);
});
});
18 changes: 18 additions & 0 deletions src/utilities/coerceInputValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,21 @@ export function coerceDefaultValue(
}
return coercedValue;
}

/**
* @internal
*/
export function coerceDefaultValue(
defaultValue: GraphQLDefaultValueUsage,
type: GraphQLInputType,
): unknown {
// Memoize the result of coercing the default value in a hidden field.
let coercedValue = (defaultValue as any)._memoizedCoercedValue;
if (coercedValue === undefined) {
coercedValue = defaultValue.literal
? coerceInputLiteral(defaultValue.literal, type)
: defaultValue.value;
(defaultValue as any)._memoizedCoercedValue = coercedValue;
}
return coercedValue;
}

0 comments on commit 4ccc601

Please sign in to comment.