-
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 deprecated arg detection and tests (#5)
- Loading branch information
Showing
8 changed files
with
172 additions
and
13 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,97 @@ | ||
import { buildSchema, parse, type DocumentNode } from 'graphql' | ||
import test from 'node:test' | ||
import { validateOperationsAndReportDeprecatedFields } from '../src/utils' | ||
import assert from 'node:assert' | ||
|
||
function makeDocumentMap(str: string) { | ||
const documentsMap = new Map<string, DocumentNode>() | ||
documentsMap.set('test.graphql', parse(str)) | ||
return documentsMap | ||
} | ||
|
||
function buildSchemaWithRootQuery(str: string) { | ||
// GraphQL validation rules imply that a schema must have a Query type | ||
if (str.match(/type\sQuery[^a-zA-Z0-9_]/)) return buildSchema(str) | ||
return buildSchema('type Query { ok: String } ' + str) | ||
} | ||
|
||
function assertValid(schemaString: string, queryString: string, expectedResult: string) { | ||
const schema = buildSchemaWithRootQuery(schemaString) | ||
const documentsMap = makeDocumentMap(queryString) | ||
const deprecatedFields = validateOperationsAndReportDeprecatedFields( | ||
schema, | ||
documentsMap, | ||
new Map(), | ||
new Map(), | ||
false, | ||
) | ||
assert.equal(deprecatedFields.size, 1) | ||
assert.equal(deprecatedFields.values().next().value, expectedResult) | ||
} | ||
|
||
test('find deprecated query', () => { | ||
assertValid( | ||
` | ||
type Query { | ||
someQuery: String @deprecated(reason: "Use nothing instead") | ||
} | ||
`, | ||
`query Test { someQuery }`, | ||
'Query "someQuery" is deprecated', | ||
) | ||
}) | ||
|
||
test('find deprecated mutation', () => { | ||
assertValid( | ||
` | ||
type Mutation { | ||
someMutation(input: String!): String! @deprecated(reason: "Use nothing instead") | ||
} | ||
`, | ||
`mutation Test { someMutation(input:"hi") }`, | ||
'Mutation "someMutation" is deprecated', | ||
) | ||
}) | ||
|
||
test('find deprecated subscription', () => { | ||
assertValid( | ||
` | ||
type Subscription { | ||
someSubscription(input: String!): String! @deprecated(reason: "Use nothing instead") | ||
} | ||
`, | ||
`subscription Test { someSubscription(input:"hi") }`, | ||
'Subscription "someSubscription" is deprecated', | ||
) | ||
}) | ||
|
||
test('find deprecated field', () => { | ||
assertValid( | ||
` | ||
type SomePayload { | ||
ok: Boolean | ||
notOk: String @deprecated(reason: "Use ok instead") | ||
} | ||
type Query { | ||
someQuery: SomePayload | ||
} | ||
`, | ||
`query Test { someQuery { ok notOk } }`, | ||
'Field "SomePayload.notOk" is deprecated', | ||
) | ||
}) | ||
|
||
test('find deprecated arg', () => { | ||
assertValid( | ||
` | ||
type SomePayload { | ||
ok: Boolean | ||
} | ||
type Query { | ||
someQuery(arg: String @deprecated(reason: "Stop using it")): SomePayload | ||
} | ||
`, | ||
`query Test { someQuery(arg: "hi") { ok } }`, | ||
'Argument "arg" from "someQuery" is deprecated', | ||
) | ||
}) |