Skip to content

Commit

Permalink
Make the fragment matcher deterministic
Browse files Browse the repository at this point in the history
This exposes a new plugin option `deterministic` that when enabled will
ensure that the fragment matcher will always return the same result for
the same schema, no matter the order it is in the schema.

It does this by sorting the results so that they are always in the same
order.

The default is `false` to maintain backwards compatibility.

This fixes #7367
  • Loading branch information
tgandrews committed Dec 8, 2024
1 parent 5724234 commit b848b1e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
29 changes: 23 additions & 6 deletions packages/plugins/other/fragment-matcher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export interface FragmentMatcherConfig {
*/
useExplicitTyping?: boolean;
federation?: boolean;
/**
* @description When enabled sorts the fragment types lexicographically. This is useful for deterministic output.
* @default false
*/
deterministic?: boolean;
}

const extensions = {
Expand All @@ -128,6 +133,7 @@ export const plugin: PluginFunction = async (
federation: false,
apolloClientVersion: 3,
useExplicitTyping: false,
deterministic: false,
...pluginConfig,
};

Expand Down Expand Up @@ -157,23 +163,34 @@ export const plugin: PluginFunction = async (
throw new Error(`Plugin "fragment-matcher" couldn't introspect the schema`);
}

const filterUnionAndInterfaceTypes = type => type.kind === 'UNION' || type.kind === 'INTERFACE';
const sortStringsLexicographically = (a: string, b: string) => {
if (!config.deterministic) {
return 0;
}
return a.localeCompare(b);
};

const unionAndInterfaceTypes = introspection.data.__schema.types
.filter(type => type.kind === 'UNION' || type.kind === 'INTERFACE')
.sort((a, b) => sortStringsLexicographically(a.name, b.name));

const createPossibleTypesCollection = (acc, type) => {
return { ...acc, [type.name]: type.possibleTypes.map(possibleType => possibleType.name) };
return {
...acc,
[type.name]: type.possibleTypes.map(possibleType => possibleType.name).sort(sortStringsLexicographically),
};
};

const filteredData: IntrospectionResultData | PossibleTypesResultData =
apolloClientVersion === 2
? {
__schema: {
...introspection.data.__schema,
types: introspection.data.__schema.types.filter(type => type.kind === 'UNION' || type.kind === 'INTERFACE'),
types: unionAndInterfaceTypes,
},
}
: {
possibleTypes: introspection.data.__schema.types
.filter(filterUnionAndInterfaceTypes)
.reduce(createPossibleTypesCollection, {}),
possibleTypes: unionAndInterfaceTypes.reduce(createPossibleTypesCollection, {}),
};

const content = JSON.stringify(filteredData, null, 2);
Expand Down
68 changes: 68 additions & 0 deletions packages/plugins/other/fragment-matcher/tests/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,72 @@ describe('Fragment Matcher Plugin', () => {

expect(content).toEqual(introspection);
});
it('should create the result deterministically when configured to', async () => {
const complexSchema = buildASTSchema(gql`
type Droid {
model: String
}
type Character {
name: String
}
type Jedi {
side: String
}
union People = Jedi | Droid | Character
union People2 = Droid | Jedi | Character
type Query {
allPeople: [People]
}
`);

const reorderedComplexSchema = buildASTSchema(gql`
type Droid {
model: String
}
type Character {
name: String
}
type Jedi {
side: String
}
union People2 = Droid | Jedi | Character
union People = Jedi | Droid | Character
type Query {
allPeople: [People]
}
`);

const contentA = await plugin(
complexSchema,
[],
{
apolloClientVersion: 2,
deterministic: true,
},
{
outputFile: 'foo.json',
}
);
const contentB = await plugin(
reorderedComplexSchema,
[],
{
apolloClientVersion: 2,
deterministic: true,
},
{
outputFile: 'foo.json',
}
);

expect(contentA).toEqual(contentB);
});
});

0 comments on commit b848b1e

Please sign in to comment.