grapher/docs/api.md

242 lines
5.5 KiB
Markdown
Raw Normal View History

2017-11-30 22:11:43 +02:00
# API
Use this as a cheatsheet after you have read the full documentation.
2018-03-30 07:18:27 +03:00
* [Adding Links](#adding-links)
* [Adding Reducers](#adding-reducers)
* [Creating GraphQL Queries](#creating-graphql-queries)
* [Creating Named Queries](#creating-named-queries)
* [Exposing Named Queries](#exposing-named-queries)
* [Using Queries](#using-queries)
* [Caching Named Queries](#caching-named-queries)
* [Creating Global Queries](#creating-global-queries)
* [Exposing Global Queries](#exposing-global-queries)
2017-12-01 12:58:36 +02:00
2017-11-30 22:11:43 +02:00
### Adding Links
```js
Collection.addLinks({
2018-03-30 07:18:27 +03:00
linkName: {
collection, // Mongo.Collection
type, // 'one' or 'many'
metadata, // Boolean
field, // String
index, // Boolean, whether to index your collections
denormalize: {
field, // String
body, // Body from related collection
},
2018-03-29 19:04:43 +03:00
},
});
2017-11-30 22:11:43 +02:00
Collection.addLinks({
2018-03-30 07:18:27 +03:00
linkName: {
collection, // Mongo.Collection
inversedBy, // The link name from the other side
denormalize: {
field, // String
body, // Body from related collection
},
2018-03-29 19:04:43 +03:00
},
});
2017-11-30 22:11:43 +02:00
```
### Adding Reducers
```js
Collection.addReducers({
2018-03-30 07:18:27 +03:00
reducerName: {
body, // Object, dependency graph
compute(object) {
// anything
},
2018-03-29 19:04:43 +03:00
},
});
2017-11-30 22:11:43 +02:00
```
2017-12-01 12:58:36 +02:00
### Creating Named Queries
2017-11-30 22:11:43 +02:00
```js
2018-03-29 19:04:43 +03:00
Collection.createQuery(
2018-03-30 07:18:27 +03:00
'queryName',
{
$options, // Mongo Options {sort, limit, skip}
$filters, // Mongo Filters
$filter({ filters, options, params }) {}, // Function or [Function]
$postOptions, // {limit, sort, skip}
$postFilters, // any sift() available filters
$postFilter(results, params) {}, // Function => results, or [Function] => results
body, // The query body
},
{
params, // Default parameters
validateParams, // Object or Function
}
2018-03-29 19:04:43 +03:00
);
```
### Creating GraphQL Queries
```js
const Query = {
users(_, args, context, ast) {
const query = Users.astToQuery(ast, {
// Manipulate the transformed body
embody({body, getArgs}) {}
$filters, // Mongo Filters/Selector
$options, // Mongo Options
// It will only allow you to query against this body graph
// Meaning it won't allow fields outside, links outside, or deeper nested than the ones you specify
intersect: Body,
// Useful when you don't have an intersection body, to restrict the limit of depth, to avoid a nested GraphQL attack
maxDepth,
// Automatically enforces a maximum number of results
maxLimit, // Integer
// Simply removes from the graph what fields it won't allow
// Can work with deep strings like 'comments.author'
deny, // String[]
})
return query.fetch();
}
}
```
Setting global defaults for all `astToQuery` queries:
```js
import { setAstToQueryDefaults } from 'meteor/cultofcoders:grapher';
setAstToQueryDefaults({
2018-03-30 07:18:27 +03:00
maxLimit: 100,
maxDepth: 5,
2018-03-29 19:04:43 +03:00
});
```
Getting the db context to inject it:
```js
import { db } from 'meteor/cultofcoders:grapher';
// db.users
// db.posts
// db.${collectionName}
2017-11-30 22:11:43 +02:00
```
2018-03-30 07:18:27 +03:00
Checkout [https://github.com/cult-of-coders/grapher-schema-directives](https://github.com/cult-of-coders/grapher-schema-directives) for some Grapher directives.
2017-12-01 12:58:36 +02:00
### Exposing Named Queries
2017-11-30 22:11:43 +02:00
```js
query.expose({
2018-03-30 07:18:27 +03:00
firewall(userId, params) {}, // Function or [Function]
method, // Boolean
publication, // Boolean
unblock, // Boolean
validateParams, // Function or Object
embody, // Object which extends the body server-side securely, or Function(body, params)
2018-03-29 19:04:43 +03:00
});
2017-11-30 22:11:43 +02:00
```
2017-12-01 12:58:36 +02:00
### Creating and Exposing Resolvers
2017-11-30 22:11:43 +02:00
```js
// both
const query = createQuery('queryName', () => {});
// server
query.expose({
2018-03-30 07:18:27 +03:00
firewall, // Function or [Function]
2017-11-30 22:11:43 +02:00
});
2018-03-29 19:04:43 +03:00
query.resolve(function(params) {
2018-03-30 07:18:27 +03:00
// this.userId
return [];
2017-11-30 22:11:43 +02:00
});
```
2017-12-01 12:58:36 +02:00
### Using Queries
2017-11-30 22:11:43 +02:00
```js
2018-03-29 19:04:43 +03:00
query.setParams({}); // extends current params
2017-11-30 22:11:43 +02:00
```
#### Server-Side
2018-03-29 19:04:43 +03:00
2017-11-30 22:11:43 +02:00
```js
2018-03-29 19:04:43 +03:00
query.clone({ params }).fetch();
query.clone({ params }).fetchOne();
query.clone({ params }).getCount();
2017-11-30 22:11:43 +02:00
```
#### Client-Side
Static:
2018-03-29 19:04:43 +03:00
2017-11-30 22:11:43 +02:00
```js
2018-03-29 19:04:43 +03:00
query.clone({ params }).fetch((err, res) => {});
query.clone({ params }).fetchOne((err, res) => {});
query.clone({ params }).getCount((err, res) => {});
2017-11-30 22:11:43 +02:00
```
Reactive:
2018-03-29 19:04:43 +03:00
2017-11-30 22:11:43 +02:00
```js
2018-03-29 19:04:43 +03:00
const query = userListQuery.clone({ params });
2017-11-30 22:11:43 +02:00
const handle = query.subscribe(); // handle.ready()
const data = query.fetch();
const oneData = query.fetchOne();
const handleCount = query.subscribeCount();
const count = query.getCount();
```
#### Caching Named Queries
2018-03-29 19:04:43 +03:00
2017-11-30 22:11:43 +02:00
```js
2018-03-29 19:04:43 +03:00
import { MemoryResultCacher } from 'meteor/cultofcoders:grapher';
2017-11-30 22:11:43 +02:00
// server-side
2018-03-29 19:04:43 +03:00
query.cacheResults(
2018-03-30 07:18:27 +03:00
new MemoryResultCacher({
ttl: 60 * 1000, // 60 seconds
})
2018-03-29 19:04:43 +03:00
);
2017-11-30 22:11:43 +02:00
```
2017-12-01 12:58:36 +02:00
#### Creating Global Queries
2017-11-30 22:11:43 +02:00
```js
Collection.createQuery({
2018-03-30 07:18:27 +03:00
$options, // Mongo Options {sort, limit, skip}
$filters, // Mongo Filters
$filter({ filters, options, params }) {}, // Function or [Function]
$postOptions, // {limit, sort, skip}
$postFilters, // any sift() available filters
$postFilter, // Function => results, or [Function] => results
body, // the rest of the object
2018-03-29 19:04:43 +03:00
});
2017-11-30 22:11:43 +02:00
```
2017-12-01 12:58:36 +02:00
#### Exposing Global Queries
2017-11-30 22:11:43 +02:00
```js
Collection.expose({
2018-03-30 07:18:27 +03:00
firewall(filters, options, userId) {}, // Function or [Function]
publication, // Boolean
method, // Boolean
blocking, // Boolean
maxLimit, // Number
maxDepth, // Number
restrictedFields, // [String]
restrictLinks, // [String] or Function,
body, // Object or Function(userId) => Object
2017-11-30 22:11:43 +02:00
});
2018-03-29 19:04:43 +03:00
```