Skip to content

Commit

Permalink
fix: update query object comparison logic (#395)
Browse files Browse the repository at this point in the history
Update query comparison logic to look at ALL query type extensions and query type fields.
  • Loading branch information
dariuszkuc authored Mar 5, 2023
1 parent c6a474b commit 5aeada3
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions packages/compatibility/src/utils/schemaComparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ObjectTypeDefinitionNode,
ObjectTypeExtensionNode,
TypeNode,
DefinitionNode,
} from 'graphql';
import { resolve } from 'path';

Expand All @@ -22,19 +23,28 @@ const productsReferenceSchema = parse(productsRaw);
function findObjectTypeDefinition(
document: DocumentNode,
typeName: string,
): any | null {
): ObjectTypeDefinitionNode | null {
return document.definitions.find(
(d) => d.kind == 'ObjectTypeDefinition' && d.name.value === typeName,
) as any;
) as ObjectTypeDefinitionNode;
}

function findObjectTypeExtensionDefinition(
document: DocumentNode,
typeName: string,
): any | null {
): ObjectTypeExtensionNode | null {
return document.definitions.find(
(d) => d.kind == 'ObjectTypeExtension' && d.name.value === typeName,
) as any;
) as ObjectTypeExtensionNode;
}

function findObjectTypeExtensionDefinitions(
document: DocumentNode,
typeName: string,
): ObjectTypeExtensionNode[] {
return document.definitions
.filter((d) => d.kind == 'ObjectTypeExtension' && d.name.value === typeName)
.map((value) => value as ObjectTypeExtensionNode);
}

function isListReturnType(type: TypeNode | null): boolean {
Expand Down Expand Up @@ -252,10 +262,14 @@ export function compareSchemas(schemaToCompare: string): boolean {
const expectedQueryDefinition =
findObjectTypeExtensionDefinition(productsReferenceSchema, 'Query') ??
findObjectTypeDefinition(productsReferenceSchema, 'Query');
const actualQueryDefinition =
findObjectTypeExtensionDefinition(schemaDefinition, 'Query') ??
findObjectTypeDefinition(schemaDefinition, 'Query');
const queryFields = actualQueryDefinition.fields.filter((field) => {
const allFields: FieldDefinitionNode[] = findObjectTypeExtensionDefinitions(
schemaDefinition,
'Query',
)
.map((value) => value.fields ?? [])
.flat()
.concat(findObjectTypeDefinition(schemaDefinition, 'Query')?.fields ?? []);
const queryFields = allFields.filter((field) => {
return ['product', 'deprecatedProduct'].includes(field.name.value);
});
errors += compareTypeFields(
Expand Down

0 comments on commit 5aeada3

Please sign in to comment.