Skip to content

Commit

Permalink
isolate only if not available internally
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Sep 26, 2024
1 parent 7a9444b commit 9612353
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ export function isolateComputedFieldsTransformer(
for (const type of returnTypes) {
const returnTypeMergeConfig = subschemaConfig.merge[type.name];

// isolate the object type only if it's not accessible from other, non-isolated, objects' fields
if (
Object.values(subschemaConfig.schema.getTypeMap())
.filter(isObjectType) // only objects
.filter(t => t !== type) // not this type
.filter(t => !isolatedSchemaTypes[t.name]) // not an isolated type
.find(t => Object.values(t.getFields()).find(f => f.type === type)) // has a field returning this type
) {
continue;
}

if (isObjectType(type)) {
const returnTypeSelectionSet = returnTypeMergeConfig?.selectionSet;
if (returnTypeSelectionSet) {
Expand Down
45 changes: 45 additions & 0 deletions packages/stitch/tests/isolateComputedFieldsTransformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,51 @@ type Mutation {
}"
`);
});
it('does not isolate objects referenced from other fields', async () => {
const [baseConfig, computedConfig] = isolateComputedFieldsTransformer({
schema: makeExecutableSchema({
typeDefs: /* GraphQL */ `
scalar _Any
union _Entity = User
type Query {
someResolver: SomeTypeWithDisappearingField
_entities(representations: [_Any!]!): _Entity
}
type SomeRequiredType {
id: String
}
type SomeTypeWithDisappearingField {
otherField: String
disappearingField: SomeRequiredType
}
type User {
id: ID!
requiresField: SomeRequiredType
}
`,
}),
merge: {
User: {
selectionSet: '{ id }',
fields: {
requiresField: { selectionSet: '{ externalField }', computed: true },
},
fieldName: '_entities',
},
},
});

assertSome(baseConfig.merge);
expect(baseConfig.merge['SomeRequiredType']).toBeUndefined();

assertSome(computedConfig.merge);
expect(computedConfig.merge['SomeRequiredType']).toBeUndefined();
});
});

describe('with multiple entryPoints', () => {
Expand Down

0 comments on commit 9612353

Please sign in to comment.