Skip to content

Commit

Permalink
Preserve default values for input object fields when extracting subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
cysp committed Oct 26, 2022
1 parent d88c328 commit 6f02b6a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gateway-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This CHANGELOG pertains only to Apollo Federation packages in the 2.x range. The

## vNext

- Preserve default values of input object fields [PR #2218](https://github.com/apollographql/federation/pull/2218).

## 2.1.4

- Ensures supergraph `@defer`/`@stream` definitions of supergraph are not included in the API schema [PR #2212](https://github.com/apollographql/federation/pull/2212).
Expand Down
1 change: 1 addition & 0 deletions internals-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## vNext

- Preserve default values of input object fields [PR #2218](https://github.com/apollographql/federation/pull/2218).

## 2.1.4

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildSupergraphSchema, extractSubgraphsFromSupergraph } from "..";
import { buildSupergraphSchema, extractSubgraphsFromSupergraph, InputObjectType } from "..";


test('handles types having no fields referenced by other objects in a subgraph correctly', () => {
Expand Down Expand Up @@ -534,6 +534,69 @@ test('handles unions types having no members in a subgraph correctly', () => {
expect(a.type('D')).toBeDefined();
})

test('preserves default values of input object fields', () => {
const supergraph = `
schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(url: "https://specs.apollo.dev/join/v0.2", for: EXECUTION)
{
query: Query
}
directive @join__field(graph: join__Graph!, requires: join__FieldSet, provides: join__FieldSet, type: String, external: Boolean, override: String, usedOverridden: Boolean) repeatable on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
directive @join__graph(name: String!, url: String!) on ENUM_VALUE
directive @join__implements(graph: join__Graph!, interface: String!) repeatable on OBJECT | INTERFACE
directive @join__type(graph: join__Graph!, key: join__FieldSet, extension: Boolean! = false, resolvable: Boolean! = true) repeatable on OBJECT | INTERFACE | UNION | ENUM | INPUT_OBJECT | SCALAR
directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA
input Input
@join__type(graph: SERVICE)
{
a: Int! = 1234
}
scalar join__FieldSet
enum join__Graph {
SERVICE @join__graph(name: "service", url: "")
}
scalar link__Import
enum link__Purpose {
"""
\`SECURITY\` features provide metadata necessary to securely resolve fields.
"""
SECURITY
"""
\`EXECUTION\` features provide metadata necessary for operation execution.
"""
EXECUTION
}
type Query
@join__type(graph: SERVICE)
{
field(input: Input!): String
}
`;

const schema = buildSupergraphSchema(supergraph)[0];
const subgraphs = extractSubgraphsFromSupergraph(schema);

const subgraph = subgraphs.get('service')
const inputType = subgraph?.schema.type('Input') as InputObjectType | undefined
const inputFieldA = inputType?.field('a')

expect(inputFieldA?.defaultValue).toBe(1234)
})


test('throw meaningful error for invalid federation directive fieldSet', () => {
const supergraph = `
schema
Expand Down
4 changes: 3 additions & 1 deletion internals-js/src/extractSubgraphsFromSupergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,9 @@ function addSubgraphInputField(
const copiedType = encodedType
? decodeType(encodedType, subgraph.schema, subgraph.name)
: copyType(supergraphField.type!, subgraph.schema, subgraph.name);
return (subgraphType as InputObjectType).addField(supergraphField.name, copiedType);
const field = (subgraphType as InputObjectType).addField(supergraphField.name, copiedType);
field.defaultValue = supergraphField.defaultValue
return field
} else {
return undefined;
}
Expand Down

0 comments on commit 6f02b6a

Please sign in to comment.