From 51c7503dae736ae112859498ebbb3e8e09c2d7a6 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Tue, 27 Jun 2017 19:10:07 -0700 Subject: [PATCH] GraphQL: Implement DirectiveDefinition (#2309) --- src/printer-graphql.js | 37 +++++++++++++++++++ .../__snapshots__/jsfmt.spec.js.snap | 20 ++++++++++ .../directive_decl.graphql | 5 +++ tests/graphql_directive_decl/jsfmt.spec.js | 1 + 4 files changed, 63 insertions(+) create mode 100644 tests/graphql_directive_decl/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/graphql_directive_decl/directive_decl.graphql create mode 100644 tests/graphql_directive_decl/jsfmt.spec.js diff --git a/src/printer-graphql.js b/src/printer-graphql.js index a43f0c72..80ee4b29 100644 --- a/src/printer-graphql.js +++ b/src/printer-graphql.js @@ -236,6 +236,43 @@ function genericPrint(path, options, print) { return concat([path.call(print, "name"), ": ", path.call(print, "type")]); } + case "DirectiveDefinition": { + return concat([ + "directive ", + "@", + path.call(print, "name"), + n.arguments.length > 0 + ? group( + concat([ + "(", + indent( + concat([ + softline, + join( + concat([ifBreak("", ", "), softline]), + path.map(print, "arguments") + ) + ]) + ), + softline, + ")" + ]) + ) + : "", + concat([" on ", join(", ", path.map(print, "locations"))]) + ]); + } + + case "InputValueDefinition": { + return concat([ + path.call(print, "name"), + ": ", + path.call(print, "type"), + n.defaultValue ? concat([" = ", path.call(print, "defaultValue")]) : "", + printDirectives(path, print, n) + ]); + } + case "FragmentSpread": { return concat([ "...", diff --git a/tests/graphql_directive_decl/__snapshots__/jsfmt.spec.js.snap b/tests/graphql_directive_decl/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..da5ed2cb --- /dev/null +++ b/tests/graphql_directive_decl/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,20 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`directive_decl.graphql 1`] = ` +directive @a on FIELD1 +directive @a(as: String) on FIELD1 +directive @a(as: String = 1) on FIELD1 +directive @a(as: String, b: Int!) on FIELD1 +directive @a(as: String! = 1 @deprecated) on FIELD1 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +directive @a on FIELD1 + +directive @a(as: String) on FIELD1 + +directive @a(as: String = 1) on FIELD1 + +directive @a(as: String, b: Int!) on FIELD1 + +directive @a(as: String! = 1 @deprecated) on FIELD1 + +`; diff --git a/tests/graphql_directive_decl/directive_decl.graphql b/tests/graphql_directive_decl/directive_decl.graphql new file mode 100644 index 00000000..cf4e48fb --- /dev/null +++ b/tests/graphql_directive_decl/directive_decl.graphql @@ -0,0 +1,5 @@ +directive @a on FIELD1 +directive @a(as: String) on FIELD1 +directive @a(as: String = 1) on FIELD1 +directive @a(as: String, b: Int!) on FIELD1 +directive @a(as: String! = 1 @deprecated) on FIELD1 diff --git a/tests/graphql_directive_decl/jsfmt.spec.js b/tests/graphql_directive_decl/jsfmt.spec.js new file mode 100644 index 00000000..335646fb --- /dev/null +++ b/tests/graphql_directive_decl/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "graphql" });