Skip to content

Commit

Permalink
Improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorDiaconu committed Mar 30, 2018
1 parent ea5a82a commit 26048f2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ A simple wrapper to easily add Apollo to your app, which contains:

## Install

First, let's get up and running with all the npm dependencies:
```
curl https://install.meteor.com/ | sh
meteor create --bare myFirstGraphQlProject
```

You can install Meteor on Windows too: https://www.meteor.com/install

Let's get up and running with all the npm dependencies:

```bash
meteor npm i -S graphql graphql-load subscriptions-transport-ws apollo-live-server apollo-live-client apollo-client apollo-cache-inmemory apollo-link apollo-link-http apollo-link-ws express apollo-server-express uuid graphql-subscriptions body-parser graphql-tools graphql-type-json
Expand All @@ -22,13 +29,13 @@ Phiew that was a lot! Any space left on your device? Now let's add our package:
meteor add cultofcoders:apollo
```

We recommend this package so you can store your types inside `.gql` files so you can benefit of reactivity:
We recommend this package so you can store your types inside `.gql` or `.graphql` files, so you can easily import them.

```bash
meteor add swydo:graphql
```

Now, if you start your Meteor app it will complain because you don't have any Query set up yet, just set up an easy one:
Now, if you start your Meteor app it will complain because you don't have any `Query` set up yet, just set up an easy one:

```js
// file: server/index.js
Expand All @@ -48,7 +55,13 @@ load({
});
```

Now get on your browser and go to: http://localhost:3000/graphiql and give it a query for testing:
Now you can safely run your project:

```
meteor run
```

Now get on your browser and go to: http://localhost:3000/graphiql and give it a spin:

```js
query {
Expand All @@ -61,14 +74,20 @@ query {
### Table of Contents

* [Simple Usage](docs/sample.md)
* [Database](docs/db.md)
* [Client](docs/client.md)
* [Work with Database](docs/db.md)
* [Accounts](docs/accounts.md)
* [Scalars](docs/scalars.md)
* [Live Queries](docs/live_queries.md)
* [Accounts](docs/accounts.md)
* [Settings](docs/settings.md)
* [Client](docs/client.md)
* [DDP](docs/ddp.md)
* [Visualising](docs/visualising.md)
* [Settings](docs/settings.md)

### Useful packages

* [graphql-load](https://www.npmjs.com/package/graphql-load?activeTab=readme)
* [disable-introspection](https://github.com/helfer/graphql-disable-introspection)
*

## Premium Support

Expand Down
63 changes: 51 additions & 12 deletions docs/db.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ The advantage is that you can start using your database and related links in jus
```typescript
type Post @mongo(name: "posts")
{
text: String!
title: String!
author: Author @link(field: "authorId")
comments: [Comment] @link(to: "post")
}

type Comment @mongo(name: "comments")
{
text: String!
post: Post @link(field: "postId")
}

type Author @mongo(name: "authors")
Expand All @@ -32,10 +39,12 @@ type Group @mongo(name: "groups") {

Above we have the following relationships:

* Post has one author and it's stored in `authorId`
* Author has many posts
* Author belongs to many groups and it's stored in `groupIds`
* Groups have many authors
* Post has one Author and it's stored in `authorId`
* Post has many Comments and is linked to the `post` link
* Comment is linked to a post, and that is stored in `postId`
* Author has many Posts and is linked to the `author` link
* Author belongs to many Groups and it's stored in `groupIds`
* Groups have many Authors and is linked to `groups` link

And the beautiful part is that for prototyping this is so fast, because we inject the db inside our context:

Expand All @@ -45,28 +54,55 @@ https://github.com/cult-of-coders/grapher-schema-directives
```js
export default {
Query: {
posts(_, args, ctx, ast) {
posts(_, args, { db }, ast) {
// Performantly fetch the query using Grapher
// You don't need to implement resolvers for your links, it's all done automatically

return ctx.db.posts.astToQuery(ast).fetch();
// Grapher will only fetch the fields you require
return db.posts.astToQuery(ast).fetch();
// but you can do whatever you want here since ctx.db.posts is a Mongo.Collection
// https://docs.meteor.com/api/collections.html
},
},
Mutation: {
addPost(_, { title }, ctx) {
ctx.db.posts.insert({
addPost(_, { title }, { db }) {
db.posts.insert({
title,
});
},
addCommentToPost(_, { postId, text }, { db }) {
// You can do this manually, but with Linker Engine from Grapher is nicer because
// it makes you not care at all about the stored fields, you only care about the linked name
// it takes care of the rest
// https://github.com/cult-of-coders/grapher/blob/master/docs/linker_engine.md
db.posts.getLink('comments').add({
text,
});

// or if you need the comment id:
const commentId = db.comments.insert({ text });
const commentLink = db.posts.getLink(postId, 'comments');
commentLink.add(commentId);

return commentId;

// alternatively, from the other side:
const commentId = db.comments.insert({ text });
db.comments.getLink(commentId, 'post').set(postId);

// Or just avoid Linker Engine
const comment = {
text,
postId,
};
return db.comments.insert(comment);
},
},
Subscription: {
posts(_, args, ctx) {
posts(_, args, { db }) {
// You can also use astToBody from Grapher, to only follow the requested fields
// But since that is a rare case, we won't cover it here so we keep it simple:
// But note that reactivity only works at a single level.
ctx.db.posts.find(
db.posts.find(
{},
{
fields: { status: 1 },
Expand All @@ -79,3 +115,6 @@ export default {

Read more about Grapher's GraphQL bridge:
https://github.com/cult-of-coders/grapher/blob/master/docs/graphql.md

Read more about advanced functionalities of Collections:
http://www.meteor-tuts.com/chapters/3/persistence-layer.html

0 comments on commit 26048f2

Please sign in to comment.