GraphQL frontend for Tyranid
This library adds a graphql query interpreter for the tyranid
mongodb ORM. The graphql schema is created from tyranid collection schemas using graphql-js
.
Quick Example
After calling graphqlize(Tyr)
on tyranid, use graphql queries for complex populations...
;; // ...; // (synchronous)// ... // template tag syntaxconst results = await Tyrgraphql` query userTeams { users { name teamIds { name, organizationId { name } } } }`; console; // => "Chipotle"
Interactive graphql server Example
To see an example of apollo-server
(http://dev.apollodata.com/core/)
being used with a generated schema from tyranid, clone the repo, install deps...
npm install
and run the example server
npm start
Detailed Code Example
Say we have the following tyranid collections...
; const User = id: 'u00' name: 'user' dbName: 'users' fields: _id: is: 'mongoid' name: is: 'string' teamIds: is: 'array' of: link: 'team' organizationId: link: 'organization' ; const Team = id: 't00' name: 'team' dbName: 'teams' fields: _id: is: 'mongoid' name: is: 'string' organizationId: link: 'organization' ; const Organization = id: 'o00' name: 'organization' dbName: 'organizations' fields: _id: is: 'mongoid' name: is: 'string' ;
We then can add graphql functionality to tyranid as follows
;; async const db = await mongodb MongoClient ; Tyr; ;;
Then, elsewhere in the app, we can use graphql queries for complex population of linked collections.
Additionally, if a Tyr.secure
plugin like tyranid-gracl
is being utilized,
we can provide auth
and perm
parameters to have
properties / documents filtered by user permissions.
const results = await Tyr; // result === {// 'data': {// 'users': [// {// 'name': 'ben',// 'teamIds': [// {// 'name': 'burritoMakers',// 'organizationId': {// 'name': 'Chipotle'// }// },// {// 'name': 'chipotleMarketing',// 'organizationId': {// 'name': 'Chipotle'// }// }// ]// },// {// 'name': 'ted',// 'teamIds': [// {// 'name': 'cavaEngineers',// 'organizationId': {// 'name': 'Cava'// }// }// ]// },// {// 'name': 'noTeams',// 'teamIds': []// }// ]// }// };
Installation
npm install tyranid-graphql
License
MIT