{"_id":"graphql-union-input-type","_rev":"288416","name":"graphql-union-input-type","description":"Union Input Type for graphql-js","dist-tags":{"latest":"0.4.0"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"time":{"modified":"2021-06-03T18:44:32.000Z","created":"2016-04-25T16:55:58.187Z","0.4.0":"2018-03-08T12:37:20.065Z","0.3.1":"2017-12-26T13:29:17.927Z","0.3.0":"2017-06-14T22:05:00.244Z","0.2.3":"2017-02-26T05:42:52.406Z","0.2.2":"2016-12-22T07:01:42.349Z","0.2.1":"2016-09-06T00:29:30.337Z","0.2.0":"2016-04-27T11:01:15.140Z","0.1.0":"2016-04-25T16:55:58.187Z"},"users":{},"author":{"name":"Sergei Petrov"},"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"versions":{"0.4.0":{"name":"graphql-union-input-type","version":"0.4.0","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"jasmine","test:watch":"nodemon --exec jasmine"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"<0.13"},"keywords":["graphql","union","interface","input","type"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","devDependencies":{"jasmine":"^2.6.0","nodemon":"^1.11.0"},"gitHead":"09ca49abb8842f66c8e9c039649f18085009611a","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.4.0","_npmVersion":"5.7.1","_nodeVersion":"9.2.1","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"cf0a24ea705aa38be91bfe0e7cf87e99df609568","size":6953,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.4.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.4.0.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/graphql-union-input-type_0.4.0_1520512640021_0.18107813412658502"},"_hasShrinkwrap":false,"publish_time":1520512640065,"_cnpm_publish_time":1520512640065},"0.3.1":{"name":"graphql-union-input-type","version":"0.3.1","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"jasmine","test:watch":"nodemon --exec jasmine"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"<0.13"},"keywords":["graphql","union","interface","input","type"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","devDependencies":{"jasmine":"^2.6.0","nodemon":"^1.11.0"},"gitHead":"fb0bb2312785c506f2eb6292a086f0a456a38d7b","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.3.1","_npmVersion":"5.6.0","_nodeVersion":"9.2.1","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"e2b065bca7e4a9a65d0a14b5e54edadc7bce277a","size":6932,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.3.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.3.1.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/graphql-union-input-type-0.3.1.tgz_1514294956992_0.45790882548317313"},"directories":{},"publish_time":1514294957927,"_hasShrinkwrap":false,"_cnpm_publish_time":1514294957927},"0.3.0":{"name":"graphql-union-input-type","version":"0.3.0","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"jasmine","test:watch":"nodemon --exec jasmine"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"<=0.10.x"},"keywords":["graphql","union","interface","input","type"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","devDependencies":{"jasmine":"^2.6.0","nodemon":"^1.11.0"},"gitHead":"21024c2e147937d61ce851f64c53bea6572ba395","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.3.0","_npmVersion":"5.0.3","_nodeVersion":"7.7.4","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"63be344df4b68e504794116cc3595341f240cd8c","size":6987,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.3.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.3.0.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/graphql-union-input-type-0.3.0.tgz_1497477899205_0.6636611346621066"},"directories":{},"publish_time":1497477900244,"_hasShrinkwrap":false,"_cnpm_publish_time":1497477900244},"0.2.3":{"name":"graphql-union-input-type","version":"0.2.3","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"^0.7.0 || ^0.8.0 || ^0.9.0"},"keywords":["graphql","union","interface","input","type"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","gitHead":"a6703de0abb9230a955329e5e17e7700c85630d4","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.2.3","_shasum":"5d74fd32df65458cb03b45d1843e4ea43f214dbd","_from":".","_npmVersion":"3.10.7","_nodeVersion":"7.5.0","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"5d74fd32df65458cb03b45d1843e4ea43f214dbd","size":6476,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.2.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.2.3.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/graphql-union-input-type-0.2.3.tgz_1488087770495_0.635603946633637"},"directories":{},"publish_time":1488087772406,"_cnpm_publish_time":1488087772406,"_hasShrinkwrap":false},"0.2.2":{"name":"graphql-union-input-type","version":"0.2.2","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"^0.7.0 || ^0.8.0"},"keywords":["graphql","union","interface","input","type"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","gitHead":"ad3ae3953a761cbf85108d561d5dbfc1107cf5f0","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.2.2","_shasum":"b38c3982b1ce71a5594b46a9a8e49276f4251777","_from":".","_npmVersion":"3.10.7","_nodeVersion":"6.5.0","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"b38c3982b1ce71a5594b46a9a8e49276f4251777","size":6471,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.2.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.2.2.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/graphql-union-input-type-0.2.2.tgz_1482390100243_0.5698517833370715"},"directories":{},"publish_time":1482390102349,"_cnpm_publish_time":1482390102349,"_hasShrinkwrap":false},"0.2.1":{"name":"graphql-union-input-type","version":"0.2.1","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"^0.7.0"},"keywords":["graphql","union","interface","input","type"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","gitHead":"c9e632f4b9dd7ac2f2d5c8b40ef8dc8775cbd07e","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.2.1","_shasum":"c8fb2970b0778040fe87ae7f9642330a289ec718","_from":".","_npmVersion":"3.9.5","_nodeVersion":"6.2.2","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"c8fb2970b0778040fe87ae7f9642330a289ec718","size":6404,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.2.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.2.1.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/graphql-union-input-type-0.2.1.tgz_1473121768482_0.9380308822728693"},"directories":{},"publish_time":1473121770337,"_cnpm_publish_time":1473121770337,"_hasShrinkwrap":false},"0.2.0":{"name":"graphql-union-input-type","version":"0.2.0","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"^0.5.0"},"keywords":["graphql","union","interface","input","type"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","gitHead":"0a52640374dc9e75aed627d74be62ad938a5003d","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.2.0","_shasum":"742c1d606cab52d3be563500ece1287cfffbd5fd","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.4.0","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"742c1d606cab52d3be563500ece1287cfffbd5fd","size":6397,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.2.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.2.0.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/graphql-union-input-type-0.2.0.tgz_1461754872773_0.20286227599717677"},"directories":{},"publish_time":1461754875140,"_cnpm_publish_time":1461754875140,"_hasShrinkwrap":false},"0.1.0":{"name":"graphql-union-input-type","version":"0.1.0","description":"Union Input Type for graphql-js","main":"index.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":{"name":"Sergei Petrov"},"license":"MIT","peerDependencies":{"graphql":"^0.5.0"},"keywords":["graphql","union","interface","input"],"repository":{"type":"git","url":"git+https://github.com/Cardinal90/graphql-union-input-type.git"},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","gitHead":"d7bbfaf5198db44754fe3a803115a9d20584f008","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"_id":"graphql-union-input-type@0.1.0","_shasum":"097ecf0e71357cead6adb5973cee7021dc4d2406","_from":".","_npmVersion":"3.3.12","_nodeVersion":"5.4.0","_npmUser":{"name":"cardinal90","email":"petra_2003@list.ru"},"dist":{"shasum":"097ecf0e71357cead6adb5973cee7021dc4d2406","size":5868,"noattachment":false,"key":"/graphql-union-input-type/-/graphql-union-input-type-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/graphql-union-input-type/download/graphql-union-input-type-0.1.0.tgz"},"maintainers":[{"name":"cardinal90","email":"petra_2003@list.ru"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/graphql-union-input-type-0.1.0.tgz_1461603355825_0.8858680187258869"},"directories":{},"publish_time":1461603358187,"_cnpm_publish_time":1461603358187,"_hasShrinkwrap":false}},"readme":"# GraphQL Union Input Type\nUnion Input Type for GraphQL-js\n\n## Why\nI wanted to represent a group of an arbitrary number of related, but different items and be able to work with it through graphql. When I started to write mutations, I realised, that Interfaces and Unions are not allowed. I'm not the only one - discussion goes [here](https://github.com/facebook/graphql/issues/114) and [here](https://github.com/graphql/graphql-js/issues/207) for example. There is a chance, that something like this will be added to the core, but it is not certain.\n\nIt would be nice to have some syntax support to specify the type to be validated against. Otherwise the only way to have clean queries without some workarounds is to let developers manually traverse AST, which seems like a too low level detail to expose.\n\n## Installation\n```\nnpm install graphql-union-input-type\n```\n## Usage\n```\nvar UnionInputType = require('graphql-union-input-type');\n\nUnionInputType(options);\n```\n### Parameters\n#### Options(object)\n - `name`(`string`)\n Name for the UnionType itself. It has to be unique in your schema and it can be used to mutate a union nested inside of another union.\n - `inputTypes`(`array|object`):\n   - either array of `GraphQLInputObjectType` objects. Their `name` property will be referenced in mutations.\n   - or object with `name:GraphQLInputObjectType` pairs. This `name` will be used instead.\n\n   Objects returned by `UnionInputType` may also be used. This argument will be ignored if `resolveType` function is provided.\n - `typeKey`(`string)`: a key in a mutation argument object containing the `name` of a type to validate against. If omitted, another [strategy](#now-you-can-call-mutations-on-it) will be used instead.\n - `resolveType`(`function(name)` -> `GraphQLInputObjectType|null`): takes a name found in mutation argument and returns corresponding\n`GraphQLInputObjectType` or an object returned by `UnionInputType`. This strategy is not restricted by a predefined set of input types. It behaves as an interface in that `UnionInputType` does not know what input types implement it. If omitted, `inputTypes` is used.\n - `resolveTypeFromAst`(`function(ast)` -> `GraphQLInputObjectType|null`): provide this, if you absolutely do not want to explicitly specify the type in you mutation. The function will be called with full AST, which you can traverse manually to identify the input type and return it.\n - `resolveTypeFromValue`(`function(value)` -> `GraphQLInputObjectType|null`): same as `resolveTypeFromAst`, but for the case, where you use variables for your input types. The function is called with a variable value, and you need to return the input type\n\n### Examples\n#### Create normal input types\n```js\nvar JediInputType = new GraphQLInputObjectType({\n\tname: 'jedi',\n\tfields: function() {\n\t\treturn {\n\t\t\tside: {\n\t\t\t\ttype: GraphQLString\n\t\t\t},\n\t\t\tname: {\n\t\t\t\ttype: GraphQLString\n\t\t\t},\n\t\t}\n\t}\n});\n\nvar SithInputType = new GraphQLInputObjectType({\n\tname: 'sith',\n\tfields: function() {\n\t\treturn {\n\t\t\tside: {\n\t\t\t\ttype: GraphQLString\n\t\t\t},\n\t\t\tname: {\n\t\t\t\ttype: GraphQLString\n\t\t\t},\n\t\t\tdoubleBlade: {\n\t\t\t\ttype: GraphQLBoolean\n\t\t\t}\n\t\t};\n\t}\n});\n```\n#### Combine them together\n```js\nvar HeroInputType = UnionInputType({\n\tname: 'hero',\n\tinputTypes: [JediInputType, SithInputType], //an object can be used instead to query by names other than defined in these types\n\ttypeKey: 'side' //optional\n});\n```\nOR\n```js\nvar HeroInputType = UnionInputType({\n\tname: 'hero',\n\tresolveType: function resolveType(name) {\n\t\tif (name === 'jedi') {\n\t\t\treturn JediInputType;\n\t\t} else {\n\t\t\treturn SithInputType;\n\t\t}\n\t},\n\ttypeKey: 'side' //optional\n});\n```\nOR\n```js\nvar HeroInputType = UnionInputType({\n\tname: 'hero',\n\tresolveTypeFromAst: resolveTypeFromAst(ast) {\n\t\tif (ast.fields[2] && ast.fields[2].name.value === 'doubleBlade') {\n\t\t\treturn SithInputType;\n\t\t} else {\n\t\t\treturn JediInputType;\n\t\t}\n\t}\n});\n```\nNote, that in the last case as it is written `doubleBlade` field on `SithInputType` needs to be wrapped with `GraphQLNonNull` for the result to be accurate. Also you could loop through `ast.fields` instead of checking just position 2. For more information just dump AST into console and see what it contains.\n#### Create schema\n```js\nvar MutationType = new GraphQLObjectType({\n\tname: 'mutation',\n\tfields: function() {\n\t\treturn {\n\t\t\thero: {\n\t\t\t\ttype: GraphQLBoolean, //this is output type, normally it will correspond to some HeroType of type GraphQLUnionType or GraphQLInterfaceType\n\t\t\t\targs: {\n\t\t\t\t\tinput: {\n\t\t\t\t\t\ttype: HeroInputType //here is our Union\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tresolve: function(root, args) {\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t}\n\t\t};\n\t}\n});\n\nvar schema = new GraphQLSchema({\n\tquery: someQueryType,\n\tmutation: MutationType\n});\n```\n#### Now you can call mutations on it\n```js\nvar query = `mutation {\n\thero(input: {{kind: \"sith\", name: \"Maul\", saberColor: \"red\", doubleBlade: true})\n}`;\n\ngraphql(schema, query).then(function(res) {\n\texpect(res.data).toBeDefined();\n\tdone();\n});\n\nquery = `mutation {\n\thero(input: {{kind: \"jedi\", name: \"Maul\", saberColor: \"red\", doubleBlade: true})\n}`;\n\ngraphql(schema, query).then(function(res) {\n\texpect(res.errors).toBeDefined();\n\tdone();\n});\n```\nThe second query will fail to validate, as there is no `doubleBlade` field on `jedi` type. Of course you can also set the type of your mutation field to something other than `GraphQLBoolean` and specify the desired return schema.\n\nYou can also omit `typeKey` property and write the same mutation this way:\n```js\nvar query = `mutation {\n\thero(input: {_type_: \"sith\", _value_: {name: \"Maul\", saberColor: \"red\", doubleBlade: true}})\n}`;\n```\nYour `resolve` function for mutation arguments will get this `input` argument as is.\n\nFinally if you provided `resolveTypeFromAst` function, you may query with an input argument as it is:\n```js\nvar query = `mutation {\n\thero(input: {name: \"Maul\", saberColor: \"red\", doubleBlade: true})\n}`;\n```\n#### Using variables (since 0.3.0)\nIf you want to use variables, you may do it as you normally would. Please note, that the name of an input variable is a `name` property passed to the `UnionInputType` function. In terms of this readme it will be `hero`:\n```js\nvar query = `mutation($hero: hero!) {\n\thero(input: $hero)\n}`;\n```\nThere is a function `resolveTypeFromValue`, which is similar to `resolveTypeFromAst`, but used for a variable value.\n\n## Capabilities\nYou can use these unions as mutation arguments, nest them inside any input types and even create unions of unions. The only small problem is that objects returned by `UnionInputType` are really `GraphQLScalarType`, so I had to allow scalars to be passed to the function.\n\nYou may use variables since 0.3.0. [This issue](https://github.com/Cardinal90/graphql-union-input-type/issues/2) might have some relevant information.\n\n## Tests\nTest are written for `jasmine`. I use `nodemon` to run them. You can find more examples in the spec file. The last test is not written formally, I just used it to play around with nested structures.\n\nSince 0.3.0 just use `npm run test`\n\n`graphql-js` 0.10.1 breaks tests. Seems to be a bug. [Reported here](https://github.com/graphql/graphql-js/issues/910)\n\n## Contributing\nFeel free to make suggestions or pull requests.\n\n## License\n(The MIT License)\n\nCopyright (c) 2016 Sergei Petrov\n","_attachments":{},"homepage":"https://github.com/Cardinal90/graphql-union-input-type","bugs":{"url":"https://github.com/Cardinal90/graphql-union-input-type/issues"},"license":"MIT"}