Skip to content

Commit

Permalink
more resilient componentField overlap detection
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Mar 6, 2024
1 parent f50b506 commit 3a6145c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test('adds internal documents to schema', async function () {
directive @required on FIELD
"""@componentField marks an inline fragment as the selection for a component field"""
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT | FIELD_DEFINITION
enum CachePolicy {
CacheAndNetwork
Expand Down Expand Up @@ -165,7 +165,7 @@ test('list operations are included', async function () {
directive @required on FIELD
"""@componentField marks an inline fragment as the selection for a component field"""
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT | FIELD_DEFINITION
enum CachePolicy {
CacheAndNetwork
Expand Down Expand Up @@ -273,7 +273,7 @@ test('list operations are included but delete directive should not be in when we
directive @required on FIELD
"""@componentField marks an inline fragment as the selection for a component field"""
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT | FIELD_DEFINITION
enum CachePolicy {
CacheAndNetwork
Expand Down Expand Up @@ -394,7 +394,7 @@ test("writing twice doesn't duplicate definitions", async function () {
directive @required on FIELD
"""@componentField marks an inline fragment as the selection for a component field"""
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT
directive @componentField(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT | FIELD_DEFINITION
enum CachePolicy {
CacheAndNetwork
Expand Down
4 changes: 2 additions & 2 deletions packages/houdini/src/codegen/transforms/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ ${
"""
@${config.componentFieldDirective} marks an inline fragment as the selection for a component field
"""
directive @${config.componentFieldDirective}(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT
directive @${config.componentFieldDirective}(field: String!, prop: String, export: String, raw: String) on FRAGMENT_DEFINITION | INLINE_FRAGMENT | FIELD_DEFINITION
`
: ''
Expand Down Expand Up @@ -180,7 +180,7 @@ directive @${config.componentFieldDirective}(field: String!, prop: String, expor
')'
}
return `${fieldName}${argString}: ${config.componentScalar}!`
return `${fieldName}${argString}: ${config.componentScalar}! @componentField(field: "${fieldName}")`
})
.join('\n')}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const table: Row[] = [
{
title: "componentFields can't overlap with type fields",
pass: false,
nb_of_fail: 1,
documents: [
`fragment MyFragmentOne on User @componentField(field: "firstName", prop: "user") {
firstName
Expand Down
33 changes: 17 additions & 16 deletions packages/houdini/src/codegen/validators/componentFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,23 @@ export default async function componentFields(config: Config, docs: Document[]):

// look up the type of the parent
const parentType = config.schema.getType(parent)
console.log(
parentType,
fieldValue,
graphql.isObjectType(parentType),
parentType &&
fieldValue &&
graphql.isObjectType(parentType) &&
parentType.getFields()[fieldValue],
existingField && existingField.filepath !== filepath
)
if (
parentType &&
fieldValue &&
((graphql.isObjectType(parentType) && parentType.getFields()[fieldValue]) ||
(existingField && existingField.filepath !== filepath))
) {
let conflict = false
if (existingField && existingField.filepath !== filepath) {
conflict = true
} else if (parentType && fieldValue) {
const fieldDef =
graphql.isObjectType(parentType) && parentType.getFields()[fieldValue]
if (
fieldDef &&
fieldDef.astNode?.directives?.find(
(dir) => dir.name.value === config.componentFieldDirective
)
) {
conflict = true
}
}

if (conflict) {
errors.push({
message:
`Duplicate component field definition for ${parent}.${fieldValue}.` +
Expand Down

0 comments on commit 3a6145c

Please sign in to comment.