Skip to content

detect extra fields in fixtures#33

Merged
lopert merged 7 commits into
mainfrom
lopert.extra-fixtures
Nov 7, 2025
Merged

detect extra fields in fixtures#33
lopert merged 7 commits into
mainfrom
lopert.extra-fixtures

Conversation

@lopert
Copy link
Copy Markdown
Collaborator

@lopert lopert commented Nov 3, 2025

Redo of #27 on the latest.

Detects extra fields by keeping track of expectedFields (aka the ones we've visited) in a new stack, parallel to the existing valueStack. Then when leaving a selection set, compare what's available in the fixture to the visited field names to find extras.

Comment thread test/methods/validate-fixture-input.test.ts
Comment thread test/methods/validate-fixture-input.test.ts Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some tests for abstract types? I don't see any handling of that in the implementation so I would expect these tests to fail

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. I refactored to track expectedFields across different types and added tests

@lopert lopert marked this pull request as draft November 3, 2025 17:31
@lopert lopert requested a review from adampetro November 4, 2025 15:36
@lopert lopert marked this pull request as ready for review November 4, 2025 15:36
@lopert lopert force-pushed the lopert.extra-fixtures branch from 24d2e9d to d078685 Compare November 4, 2025 17:26
Comment thread src/methods/validate-fixture-input.ts Outdated
}
}
possibleTypesStack.push(possibleTypes);
typeConditionStack.push(namedType ?? null);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of an undefined vs null thing.

schema.getType can return undefined, but in selection sets I explicitly push null values to the typeConditionStack. I chose null to represent that I was consciously setting the value to "nothing", and we branch through the expectedFields setting logic with that.

... I could have just used undefined, but I felt null was a clearer representation of what I was doing.

This way we keep the graphql concept of "i tried to look up a type and it wasn't defined" and the stack concept of "am I in an inlinefragment" logic separate.

Comment thread src/methods/validate-fixture-input.ts Outdated
Comment thread src/methods/validate-fixture-input.ts Outdated
@lopert lopert force-pushed the lopert.extra-fixtures branch from c612396 to e1f1712 Compare November 6, 2025 17:24
@lopert lopert force-pushed the lopert.extra-fixtures branch from e1f1712 to a368c19 Compare November 6, 2025 17:32
@lopert lopert requested a review from adampetro November 6, 2025 17:37
Copy link
Copy Markdown
Contributor

@saga-dasgupta saga-dasgupta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was able to run through this code and ensure it works in detecting extra fields using some example fixtures and input queries.

for (const fixtureField of fixtureFields) {
if (!expectedForThisObject.has(fixtureField)) {
errors.push(
`Extra field "${fixtureField}" found in fixture data not in query`,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a path to the bad field here to make it easier for people to debug? This might when the field is something common like id or title and the fixture is huge and they don't where the extra field is.


// Check each field in the fixture object
for (const fixtureField of fixtureFields) {
if (!expectedForThisObject.has(fixtureField)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might wanna exclude __typename here.

Comment thread src/methods/validate-fixture-input.ts Outdated
Comment thread src/methods/validate-fixture-input.ts
Comment thread src/methods/validate-fixture-input.ts Outdated
Comment thread src/methods/validate-fixture-input.ts Outdated
@lopert lopert requested a review from adampetro November 7, 2025 19:08
@lopert lopert force-pushed the lopert.extra-fixtures branch from 0e07755 to edc262b Compare November 7, 2025 19:33
Copy link
Copy Markdown
Contributor

@adampetro adampetro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a few lint errors to fix but otherwise LGTM

@lopert lopert merged commit 149a9f7 into main Nov 7, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants