Skip to content

Commit

Permalink
FAI-10363: Remove superfluous FKs from incremental queries (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
ted-faros authored Apr 2, 2024
1 parent 21b33e7 commit c5eb845
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/graphql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,8 @@ export function buildIncrementalQueryV2(
type: gql.GraphQLObjectType,
avoidCollisions = true,
resolvedPrimaryKeys: Dictionary<string> = {},
references: Dictionary<Reference> = {}
references: Dictionary<Reference> = {},
scalarsOnly = false
): Query {
const name = type.name;
// add fields and FKs
Expand All @@ -1500,7 +1501,7 @@ export function buildIncrementalQueryV2(
} else {
fieldsObj[field.name] = true; // arbitrary value here
}
} else if (isV2ModelType(field.type)) {
} else if (isV2ModelType(field.type) && !scalarsOnly) {
// this is foreign key to a top-level model.
// add nested fragment to select id of referenced model
const fk = resolvedPrimaryKeys[field.type.name] || ID_FLD;
Expand Down Expand Up @@ -1549,9 +1550,17 @@ export function createIncrementalReadersV2(
client: FarosClient,
graph: string,
pageSize: number,
graphQLSchema: gql.GraphQLSchema
graphQLSchema: gql.GraphQLSchema,
avoidCollisions = true,
scalarsOnly = false
): ReadonlyArray<Reader> {
const result: Reader[] = createIncrementalQueriesV2(graphQLSchema).map(
const result: Reader[] = createIncrementalQueriesV2(
graphQLSchema,
undefined,
undefined,
avoidCollisions,
scalarsOnly
).map(
(query) =>
readerFromQuery(
graph,
Expand All @@ -1574,7 +1583,8 @@ export function createIncrementalQueriesV2(
graphQLSchema: gql.GraphQLSchema,
primaryKeys?: Dictionary<ReadonlyArray<string>>,
references?: Dictionary<Dictionary<Reference>>,
avoidCollisions = true
avoidCollisions = true,
scalarsOnly = false
): ReadonlyArray<Query> {
const result: Query[] = [];
const resolvedPrimaryKeys = primaryKeys
Expand All @@ -1598,7 +1608,8 @@ export function createIncrementalQueriesV2(
type,
avoidCollisions,
resolvedPrimaryKeys,
typeReferences
typeReferences,
scalarsOnly
)
);
}
Expand Down
17 changes: 17 additions & 0 deletions test/__snapshots__/graphql.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,23 @@ exports[`graphql create incremental queries V2 2`] = `
]
`;

exports[`graphql create incremental queries V2 3`] = `
[
{
"gql": "query ($from: timestamptz!, $to: timestamptz!) { cicd_Build (where: {refreshedAt: {_gte: $from, _lt: $to}}) { id createdAt endedAt name number origin refreshedAt startedAt status statusCategory statusDetail uid url } }",
"name": "cicd_Build",
},
{
"gql": "query ($from: timestamptz!, $to: timestamptz!) { cicd_Pipeline (where: {refreshedAt: {_gte: $from, _lt: $to}}) { id tags } }",
"name": "cicd_Pipeline",
},
{
"gql": "query ($from: timestamptz!, $to: timestamptz!) { cicd_Artifact (where: {refreshedAt: {_gte: $from, _lt: $to}}) { id tags uid } }",
"name": "cicd_Artifact",
},
]
`;

exports[`graphql create incremental queries V2 when missing expected field 2`] = `"expected missing_field to be a reference field of cicd_Pipeline (foreign key to cicd_Organization)"`;

exports[`graphql create incremental queries V2 with primary keys/references 1`] = `
Expand Down
8 changes: 8 additions & 0 deletions test/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,14 @@ describe('graphql', () => {
expect(
sut.createIncrementalQueriesV2(graphSchemaV2, undefined, undefined, false)
).toMatchSnapshot();
expect(
sut.createIncrementalQueriesV2(graphSchemaV2,
undefined,
undefined,
false,
true,
),
).toMatchSnapshot();
});

test('create incremental queries V2 with primary keys/references', () => {
Expand Down

0 comments on commit c5eb845

Please sign in to comment.