npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tyranid-graphql

v0.5.25

Published

GraphQL plugin for tyranid

Downloads

11

Readme

GraphQL frontend for Tyranid

npm version codecov

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...

import { Tyr } from 'tyranid';
import { graphqlize } from 'tyranid-graphql';

// ...
graphqlize(Tyr); // (synchronous)
// ...

// template tag syntax
const results = await Tyr.graphql`
  query userTeams {
    users {
      name
      teamIds {
        name,
        organizationId {
          name
        }
      }
    }
  }
`;

console.log(users[1].teamIds[2].organizationId.name); // => "Chipotle"

Interactive graphql server Example

Apollo 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...

import { Tyr } from 'tyranid';

export const User = new Tyr.Collection({
  id: 'u00',
  name: 'user',
  dbName: 'users',
  fields: {
    _id: { is: 'mongoid' },
    name: { is: 'string' },
    teamIds: {
      is: 'array',
      of: {
        link: 'team'
      }
    },
    organizationId: { link: 'organization' }
  }
});

export const Team = new Tyr.Collection({
  id: 't00',
  name: 'team',
  dbName: 'teams',
  fields: {
    _id: { is: 'mongoid' },
    name: { is: 'string' },
    organizationId: { link: 'organization' }
  }
});

export const Organization = new Tyr.Collection({
  id: 'o00',
  name: 'organization',
  dbName: 'organizations',
  fields: {
    _id: { is: 'mongoid' },
    name: { is: 'string' }
  }
});

We then can add graphql functionality to tyranid as follows

import { Tyr } from 'tyranid';
import { graphqlize } from 'tyranid-graphql';


(async() => {
  const db = await mongodb
    .MongoClient
    .connect('mongodb://127.0.0.1:27017/tyranid_gracl_test');

  Tyr.config({
    db: db,
    validate: [
      { dir: __dirname,
        fileMatch: 'models.js' }
    ]
  });

  graphqlize(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.graphql({
  query: `
    query userTeams {
      users {
        name
        teamIds {
          name,
          organizationId {
            name
          }
        }
      }
    }
  `
  auth: req.user,
  perm: 'view'
});


// 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