-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add additional rule, NoDuplicateFieldName
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as gql from 'graphql' | ||
import { isFieldNode, isInlineFragmentNode } from '../../gql' | ||
|
||
export function NoDuplicateFieldName(document: gql.DocumentNode, schema: gql.GraphQLSchema) { | ||
const errors: gql.GraphQLError[] = [] | ||
const typeInfo = new gql.TypeInfo(schema) | ||
gql.visit( | ||
document, | ||
gql.visitWithTypeInfo(typeInfo, { | ||
SelectionSet(node) { | ||
const parent = typeInfo.getParentType() | ||
const type = parent?.name | ||
node.selections.reduce( | ||
(a, node) => { | ||
const name = isFieldNode(node) | ||
? node.name.value | ||
: isInlineFragmentNode(node) | ||
? node.typeCondition?.name.value | ||
: undefined | ||
if (!name) return a | ||
if (!!a[name]) { | ||
errors.push( | ||
new gql.GraphQLError(`Duplicate field "${name}" on type "${type}" `, { | ||
nodes: [node], | ||
}), | ||
) | ||
} | ||
return { ...a, [name]: true } | ||
}, | ||
{} as Record<string, boolean>, | ||
) | ||
}, | ||
}), | ||
) | ||
return errors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './NoDuplicateFieldName' |