-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support dynamic arguments (#146)
Co-authored-by: James Mortemore <[email protected]> fixes #145
- Loading branch information
Showing
22 changed files
with
283 additions
and
205 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const { strictEqual } = require('assert') | ||
const { GraphQLString } = require('graphql') | ||
const { mapSchema, getDirective, MapperKind } = require('@graphql-tools/utils') | ||
|
||
const dateDirectiveTypeDefs = 'directive @formatDate on FIELD_DEFINITION' | ||
|
||
const dateDirectiveTransformer = (schema) => | ||
mapSchema(schema, { | ||
[MapperKind.OBJECT_FIELD] (fieldConfig) { | ||
const dateDirective = getDirective(schema, fieldConfig, 'formatDate')?.[0] | ||
if (dateDirective) { | ||
fieldConfig.args.format = { | ||
type: GraphQLString | ||
} | ||
|
||
fieldConfig.type = GraphQLString | ||
return fieldConfig | ||
} | ||
} | ||
}) | ||
|
||
module.exports.test = function (setup, implType) { | ||
describe('Dynamic argument', function () { | ||
before(async function () { | ||
this.typeDefs = /* GraphQL */` | ||
type Query { | ||
getBook: String @formatDate | ||
getBooks: [String] @formatDate | ||
} | ||
` + '\n' + dateDirectiveTypeDefs | ||
|
||
this.request = await setup({ typeDefs: this.typeDefs, schemaCreatedCallback: (schema) => { return dateDirectiveTransformer(schema) } }) | ||
}) | ||
|
||
it('should pass', async function () { | ||
const query = /* GraphQL */` | ||
query Go { | ||
getBook(format: "aa") | ||
getBooks(format: "aa") | ||
} | ||
` | ||
const { body, statusCode } = await this.request | ||
.post('/graphql') | ||
.set('Accept', 'application/json') | ||
.send({ query }) | ||
|
||
if (statusCode !== 200) { console.log(body) } | ||
strictEqual(statusCode, 200) | ||
}) | ||
}) | ||
} |
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
Oops, something went wrong.