Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the fragment matcher deterministic #10214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-tables-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/fragment-matcher': minor
---

Add new flag to make the fragment matcher results deterministic
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);
});
});
Loading