From 4b680d4874d4faa70147a35e3abcaf02269654a8 Mon Sep 17 00:00:00 2001 From: Pavel Lang Date: Sat, 8 Dec 2018 06:16:10 +0100 Subject: [PATCH] feat(graphql): add graphql code sample --- test/samples-all.generated.js | 4 + test/samples/sample.graphql.txt | 263 ++++++++++++++++++++++++++++++++ 2 files changed, 267 insertions(+) create mode 100644 test/samples/sample.graphql.txt diff --git a/test/samples-all.generated.js b/test/samples-all.generated.js index 21723e6b..678bd267 100644 --- a/test/samples-all.generated.js +++ b/test/samples-all.generated.js @@ -88,6 +88,10 @@ define([], function() { return[ "name": "sample.go.txt", "content": "// We often need our programs to perform operations on\r\n// collections of data, like selecting all items that\r\n// satisfy a given predicate or mapping all items to a new\r\n// collection with a custom function.\r\n\r\n// In some languages it's idiomatic to use [generic](http://en.wikipedia.org/wiki/Generic_programming)\r\n// data structures and algorithms. Go does not support\r\n// generics; in Go it's common to provide collection\r\n// functions if and when they are specifically needed for\r\n// your program and data types.\r\n\r\n// Here are some example collection functions for slices\r\n// of `strings`. You can use these examples to build your\r\n// own functions. Note that in some cases it may be\r\n// clearest to just inline the collection-manipulating\r\n// code directly, instead of creating and calling a\r\n// helper function.\r\n\r\npackage main\r\n\r\nimport \"strings\"\r\nimport \"fmt\"\r\n\r\n// Returns the first index of the target string `t`, or\r\n// -1 if no match is found.\r\nfunc Index(vs []string, t string) int {\r\n for i, v := range vs {\r\n if v == t {\r\n return i\r\n }\r\n }\r\n return -1\r\n}\r\n\r\n// Returns `true` if the target string t is in the\r\n// slice.\r\nfunc Include(vs []string, t string) bool {\r\n return Index(vs, t) >= 0\r\n}\r\n\r\n// Returns `true` if one of the strings in the slice\r\n// satisfies the predicate `f`.\r\nfunc Any(vs []string, f func(string) bool) bool {\r\n for _, v := range vs {\r\n if f(v) {\r\n return true\r\n }\r\n }\r\n return false\r\n}\r\n\r\n// Returns `true` if all of the strings in the slice\r\n// satisfy the predicate `f`.\r\nfunc All(vs []string, f func(string) bool) bool {\r\n for _, v := range vs {\r\n if !f(v) {\r\n return false\r\n }\r\n }\r\n return true\r\n}\r\n\r\n// Returns a new slice containing all strings in the\r\n// slice that satisfy the predicate `f`.\r\nfunc Filter(vs []string, f func(string) bool) []string {\r\n vsf := make([]string, 0)\r\n for _, v := range vs {\r\n if f(v) {\r\n vsf = append(vsf, v)\r\n }\r\n }\r\n return vsf\r\n}\r\n\r\n// Returns a new slice containing the results of applying\r\n// the function `f` to each string in the original slice.\r\nfunc Map(vs []string, f func(string) string) []string {\r\n vsm := make([]string, len(vs))\r\n for i, v := range vs {\r\n vsm[i] = f(v)\r\n }\r\n return vsm\r\n}\r\n\r\nfunc main() {\r\n\r\n // Here we try out our various collection functions.\r\n var strs = []string{\"peach\", \"apple\", \"pear\", \"plum\"}\r\n\r\n fmt.Println(Index(strs, \"pear\"))\r\n\r\n fmt.Println(Include(strs, \"grape\"))\r\n\r\n fmt.Println(Any(strs, func(v string) bool {\r\n return strings.HasPrefix(v, \"p\")\r\n }))\r\n\r\n fmt.Println(All(strs, func(v string) bool {\r\n return strings.HasPrefix(v, \"p\")\r\n }))\r\n\r\n fmt.Println(Filter(strs, func(v string) bool {\r\n return strings.Contains(v, \"e\")\r\n }))\r\n\r\n // The above examples all used anonymous functions,\r\n // but you can also use named functions of the correct\r\n // type.\r\n fmt.Println(Map(strs, strings.ToUpper))\r\n\r\n}\r\n" }, + { + "name": "sample.graphql.txt", + "content": "# GraphQL Schema Definition Language\n\n\"\"\"\nColor value\n\"\"\"\nscalar Color\n\n\"\"\"\nNode interface\n\n- allows (re)fetch arbitrary entity only by ID\n- allows client side cache normalization\n\nSee [Relay Global Object Identification Specification](https://facebook.github.io/relay/graphql/objectidentification.htm)\n\"\"\"\ninterface Node {\n \"\"\"\n Globally unique identifier,\n typically `${__typename}:${dbId}`\n may be encoded in *base64*\n \"\"\"\n id: ID!\n}\n\n\"\"\"\nA character in the Star Wars Trilogy\n\"\"\"\ninterface Character {\n \"\"\"\n The id of the character.\n \"\"\"\n id: ID!\n\n \"\"\"\n The name of the character.\n \"\"\"\n name: String\n\n \"\"\"\n The friends of the character, or an empty list if they have none.\n \"\"\"\n friends: [Character]\n\n \"\"\"\n Which movies they appear in\n \"\"\"\n appearsIn: [Episode]\n\n \"\"\"\n All secrets about their past\n \"\"\"\n secretBackstory: String\n}\n\n\"\"\"\nA mechanical creature in the Star Wars universe.\n\"\"\"\ntype Droid implements Character {\n \"\"\"\n The id of the droid.\n \"\"\"\n id: ID!\n\n \"\"\"\n The name of the droid.\n \"\"\"\n name: String\n\n \"\"\"\n The friends of the droid, or an empty list if they have none.\n \"\"\"\n friends: [Character]\n\n \"\"\"\n Which movies they appear in.\n \"\"\"\n appearsIn: [Episode]\n\n \"\"\"\n Construction date and the name of the designer.\n \"\"\"\n secretBackstory: String\n\n \"\"\"\n The primary function of the droid.\n \"\"\"\n primaryFunction: String\n\n \"\"\"\n Chase color of the droid.\n \"\"\"\n color: Color\n}\n\n# One of the films in the Star Wars Trilogy\nenum Episode {\n \"\"\"\n Released in 1977.\n \"\"\"\n NEWHOPE\n\n \"\"\"\n Released in 1980.\n \"\"\"\n EMPIRE\n\n \"\"\"\n Released in 1983.\n \"\"\"\n JEDI\n}\n\n\"\"\"\nA humanoid creature in the Star Wars universe.\n\"\"\"\ntype Human implements Character {\n \"\"\"\n The id of the human.\n \"\"\"\n id: ID!\n\n \"\"\"\n The name of the human.\n \"\"\"\n name: String\n\n \"\"\"\n The friends of the human, or an empty list if they have none.\n \"\"\"\n friends: [Character]\n\n \"\"\"\n Which movies they appear in.\n \"\"\"\n appearsIn: [Episode]\n\n \"\"\"\n The home planet of the human, or null if unknown.\n \"\"\"\n homePlanet: String\n\n \"\"\"\n Where are they from and how they came to be who they are.\n \"\"\"\n secretBackstory: String\n}\n\nenum LengthUnit {\n METER\n FEET\n}\n\ntype Starship {\n id: ID!\n name: String!\n length(unit: LengthUnit = METER): Float\n}\n\nunion SearchResult = Human | Droid | Starship\n\ninput SearchInput {\n name: String\n episode: Episode\n}\n\n\"\"\"\nRoot Query\n\"\"\"\ntype Query {\n \"\"\"\n Return the hero by episode.\n \"\"\"\n hero(\n \"\"\"\n If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.\n \"\"\"\n episode: Episode\n ): Character\n\n \"\"\"\n Return the Human by ID.\n \"\"\"\n human(\n \"\"\"\n id of the human\n \"\"\"\n id: ID!\n ): Human\n\n \"\"\"\n Return the Droid by ID.\n \"\"\"\n droid(\n \"\"\"\n id of the droid\n \"\"\"\n id: ID!\n ): Droid\n\n \"\"\"\n Search everything by name\n\n __NOTE__: You should use Relay pagination\n \"\"\"\n search(search: SearchInput!): [SearchResult]\n @deprecated(reason: \"`search` will be replaced.\")\n}\n\n\"\"\"\nRoot Mutation\n\"\"\"\ntype Mutation {\n \"\"\"\n Save the favorite episode.\n \"\"\"\n favorite(\n \"\"\"\n Favorite episode.\n \"\"\"\n episode: Episode!\n ): Episode\n}\n\n\"\"\"\nSubscriptions — live events\n\"\"\"\ntype Subscription {\n \"\"\"\n Message\n \"\"\"\n message: String\n}\n\nextend type Query {\n \"\"\"\n Dummy query for highlighting test\n \"\"\"\n dummy(\n int: Int = 123\n float: Float = 123.456\n str: String = \"Hello World!\"\n boolDefaultTrue: Boolean = true\n boolDefaultFalse: Boolean = false\n id: ID\n search: SearchInput = null\n ): Boolean\n}\n\nschema {\n query: Query\n mutation: Mutation\n subscription: Subscription\n}\n\n# GraphQL Query Language\n\nquery dummyQuery($int: Int) {\n dummy(int: $int)\n}\n\nmutation favoriteEpisode($episode: Episode) {\n favorite(episode: $episode)\n}\n" + }, { "name": "sample.handlebars.txt", "content": "\n
\n\t

{{title}}

\n\t{{#if author}}\n\t

{{author.firstName}} {{author.lastName}}

\n\t{{else}}\n\t

Unknown Author

\n\t{{/if}}\n\t{{contentBody}}\n
\n\n{{#unless license}}\n

WARNING: This entry does not have a license!

\n{{/unless}}\n\n
\n\t\n
\n\n

Comments

\n\n
\n\t{{#each comments}}\n\t

{{title}}

\n\t
{{body}}
\n\t{{/each}}\n
\n" diff --git a/test/samples/sample.graphql.txt b/test/samples/sample.graphql.txt new file mode 100644 index 00000000..2f783785 --- /dev/null +++ b/test/samples/sample.graphql.txt @@ -0,0 +1,263 @@ +# GraphQL Schema Definition Language + +""" +Color value +""" +scalar Color + +""" +Node interface + +- allows (re)fetch arbitrary entity only by ID +- allows client side cache normalization + +See [Relay Global Object Identification Specification](https://facebook.github.io/relay/graphql/objectidentification.htm) +""" +interface Node { + """ + Globally unique identifier, + typically `${__typename}:${dbId}` + may be encoded in *base64* + """ + id: ID! +} + +""" +A character in the Star Wars Trilogy +""" +interface Character { + """ + The id of the character. + """ + id: ID! + + """ + The name of the character. + """ + name: String + + """ + The friends of the character, or an empty list if they have none. + """ + friends: [Character] + + """ + Which movies they appear in + """ + appearsIn: [Episode] + + """ + All secrets about their past + """ + secretBackstory: String +} + +""" +A mechanical creature in the Star Wars universe. +""" +type Droid implements Character { + """ + The id of the droid. + """ + id: ID! + + """ + The name of the droid. + """ + name: String + + """ + The friends of the droid, or an empty list if they have none. + """ + friends: [Character] + + """ + Which movies they appear in. + """ + appearsIn: [Episode] + + """ + Construction date and the name of the designer. + """ + secretBackstory: String + + """ + The primary function of the droid. + """ + primaryFunction: String + + """ + Chase color of the droid. + """ + color: Color +} + +# One of the films in the Star Wars Trilogy +enum Episode { + """ + Released in 1977. + """ + NEWHOPE + + """ + Released in 1980. + """ + EMPIRE + + """ + Released in 1983. + """ + JEDI +} + +""" +A humanoid creature in the Star Wars universe. +""" +type Human implements Character { + """ + The id of the human. + """ + id: ID! + + """ + The name of the human. + """ + name: String + + """ + The friends of the human, or an empty list if they have none. + """ + friends: [Character] + + """ + Which movies they appear in. + """ + appearsIn: [Episode] + + """ + The home planet of the human, or null if unknown. + """ + homePlanet: String + + """ + Where are they from and how they came to be who they are. + """ + secretBackstory: String +} + +enum LengthUnit { + METER + FEET +} + +type Starship { + id: ID! + name: String! + length(unit: LengthUnit = METER): Float +} + +union SearchResult = Human | Droid | Starship + +input SearchInput { + name: String + episode: Episode +} + +""" +Root Query +""" +type Query { + """ + Return the hero by episode. + """ + hero( + """ + If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode. + """ + episode: Episode + ): Character + + """ + Return the Human by ID. + """ + human( + """ + id of the human + """ + id: ID! + ): Human + + """ + Return the Droid by ID. + """ + droid( + """ + id of the droid + """ + id: ID! + ): Droid + + """ + Search everything by name + + __NOTE__: You should use Relay pagination + """ + search(search: SearchInput!): [SearchResult] + @deprecated(reason: "`search` will be replaced.") +} + +""" +Root Mutation +""" +type Mutation { + """ + Save the favorite episode. + """ + favorite( + """ + Favorite episode. + """ + episode: Episode! + ): Episode +} + +""" +Subscriptions — live events +""" +type Subscription { + """ + Message + """ + message: String +} + +extend type Query { + """ + Dummy query for highlighting test + """ + dummy( + int: Int = 123 + float: Float = 123.456 + str: String = "Hello World!" + boolDefaultTrue: Boolean = true + boolDefaultFalse: Boolean = false + id: ID + search: SearchInput = null + ): Boolean +} + +schema { + query: Query + mutation: Mutation + subscription: Subscription +} + +# GraphQL Query Language + +query dummyQuery($int: Int) { + dummy(int: $int) +} + +mutation favoriteEpisode($episode: Episode) { + favorite(episode: $episode) +}