{"_id":"5844478a94dafe19003dc2d8","order":0,"parentDoc":null,"sync_unique":"","updates":[],"excerpt":"","category":"5844478994dafe19003dc2d6","version":"5844478994dafe19003dc2d5","__v":0,"body":"[![Build Status](https://travis-ci.org/fenos/graphql-thinky.svg?branch=master)](https://travis-ci.org/fenos/graphql-thinky) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/fenos/graphql-thinky/master/LICENSE) [![npm](https://img.shields.io/badge/npm-0.4.0--rc--3-blue.svg)](https://www.npmjs.com/package/graphql-thinky) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)\n\nGraphql-thinky helps you to construct your GraphQL schema and the communication layer to a RethinkDB backend. It will perform **batched** and **optimised** queries, during graphql requests. the library is powered by the fantastic thinky ORM and Graphql / RelayJS which rely on.\n\nInspired by the great [graphql-sequelize](https://github.com/mickhansen/graphql-sequelize). If you have a SQLs application i suggest to look at it.  \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Milestone\"\n}\n[/block]\nThis library has been just backed with :heartpulse:  and with in it's early version can already provide pleasure to build your own GraphQL schema; although it needs the **community support** to grow and evolve to achieve the **v1.0**.\n\nHere is the features not yet implemented, that i'm willing to add over the time.\n\n- **Subscription** helpers for graphql, so that we can fully use RethinkDB amazing **change feed**\n- **Mutation** helpers, to allow create simple mutation in few lines\n- Custom query overwrites - to allow the developer to extend the default query behaviour of **graphql-thinky** on every Node.\n- Increase test case coverage\n- and much more when new feature are requested from the folks...","githubsync":"","link_url":"","project":"57462673130acb0e0029b07e","type":"basic","api":{"settings":"","auth":"required","params":[],"url":"","results":{"codes":[]}},"createdAt":"2016-05-30T11:24:06.180Z","link_external":false,"next":{"description":"","pages":[]},"slug":"overview","title":"Overview","user":"563bb91c2eecbf0d003c5b46","hidden":false,"isReference":false,"childrenPages":[]}

Overview


[![Build Status](https://travis-ci.org/fenos/graphql-thinky.svg?branch=master)](https://travis-ci.org/fenos/graphql-thinky) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/fenos/graphql-thinky/master/LICENSE) [![npm](https://img.shields.io/badge/npm-0.4.0--rc--3-blue.svg)](https://www.npmjs.com/package/graphql-thinky) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) Graphql-thinky helps you to construct your GraphQL schema and the communication layer to a RethinkDB backend. It will perform **batched** and **optimised** queries, during graphql requests. the library is powered by the fantastic thinky ORM and Graphql / RelayJS which rely on. Inspired by the great [graphql-sequelize](https://github.com/mickhansen/graphql-sequelize). If you have a SQLs application i suggest to look at it. [block:api-header] { "type": "basic", "title": "Milestone" } [/block] This library has been just backed with :heartpulse: and with in it's early version can already provide pleasure to build your own GraphQL schema; although it needs the **community support** to grow and evolve to achieve the **v1.0**. Here is the features not yet implemented, that i'm willing to add over the time. - **Subscription** helpers for graphql, so that we can fully use RethinkDB amazing **change feed** - **Mutation** helpers, to allow create simple mutation in few lines - Custom query overwrites - to allow the developer to extend the default query behaviour of **graphql-thinky** on every Node. - Increase test case coverage - and much more when new feature are requested from the folks...
[![Build Status](https://travis-ci.org/fenos/graphql-thinky.svg?branch=master)](https://travis-ci.org/fenos/graphql-thinky) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/fenos/graphql-thinky/master/LICENSE) [![npm](https://img.shields.io/badge/npm-0.4.0--rc--3-blue.svg)](https://www.npmjs.com/package/graphql-thinky) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo) Graphql-thinky helps you to construct your GraphQL schema and the communication layer to a RethinkDB backend. It will perform **batched** and **optimised** queries, during graphql requests. the library is powered by the fantastic thinky ORM and Graphql / RelayJS which rely on. Inspired by the great [graphql-sequelize](https://github.com/mickhansen/graphql-sequelize). If you have a SQLs application i suggest to look at it. [block:api-header] { "type": "basic", "title": "Milestone" } [/block] This library has been just backed with :heartpulse: and with in it's early version can already provide pleasure to build your own GraphQL schema; although it needs the **community support** to grow and evolve to achieve the **v1.0**. Here is the features not yet implemented, that i'm willing to add over the time. - **Subscription** helpers for graphql, so that we can fully use RethinkDB amazing **change feed** - **Mutation** helpers, to allow create simple mutation in few lines - Custom query overwrites - to allow the developer to extend the default query behaviour of **graphql-thinky** on every Node. - Increase test case coverage - and much more when new feature are requested from the folks...
{"_id":"5844478a94dafe19003dc2d9","updates":[],"githubsync":"","order":1,"excerpt":"","title":"Installation","__v":0,"isReference":false,"parentDoc":null,"project":"57462673130acb0e0029b07e","version":"5844478994dafe19003dc2d5","createdAt":"2016-05-25T22:25:55.667Z","link_external":false,"sync_unique":"","hidden":false,"body":"*Dependencies*\n- [graphql](https://github.com/graphql/graphql-js)\n- [thinky](https://github.com/neumino/thinky)\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"npm install graphql-thinky --save\",\n      \"language\": \"text\",\n      \"name\": \"\"\n    }\n  ]\n}\n[/block]\nTo initialize the the package, simply create a file ex: **graphql-thiky.js**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// graphql-thinky.js\\nimport GraphQLThinky from 'graphql-thinky';\\nimport models from './../models'; // import your models so they can be bound to thinky\\nimport thinky from '../thinky'; // thinky instance\\n\\n\\nexport default new GraphQLThinky(thinky);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nHere is the only place where you will import the package. In all other files you will import this **local file** so that you can work on the **GraphqlThinky** instance.\n\n**Next**: We need to make sure that we pass to the **graphql context ** the ready to use **Loaders** created by the library. Here as follow:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// server.js\\nimport GT from './graphql-thinky';\\n\\nconst app = express();\\n\\napp.use(bodyParser.json());\\n// We are going to create new instances of the Loaders\\n// for every request\\napp.use('/graphql',expressGraphql(request => ({ \\n  schema,\\n  context: {\\n  \\tloaders: GT.getModelLoaders() \\n  },\\n  graphiql: true,\\n  pretty: true\\n})));\\n\\napp.listen(7000, function() {\\n  console.log(\\\"Server started at port: 7000\\\");\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","slug":"getting-started","category":"5844478994dafe19003dc2d6","user":"563bb91c2eecbf0d003c5b46","api":{"auth":"required","params":[],"url":"","results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"code":"{}","language":"json","status":400,"name":""}]},"settings":""},"next":{"pages":[],"description":""},"link_url":"","type":"basic","childrenPages":[]}

Installation


*Dependencies* - [graphql](https://github.com/graphql/graphql-js) - [thinky](https://github.com/neumino/thinky) [block:code] { "codes": [ { "code": "npm install graphql-thinky --save", "language": "text", "name": "" } ] } [/block] To initialize the the package, simply create a file ex: **graphql-thiky.js** [block:code] { "codes": [ { "code": "// graphql-thinky.js\nimport GraphQLThinky from 'graphql-thinky';\nimport models from './../models'; // import your models so they can be bound to thinky\nimport thinky from '../thinky'; // thinky instance\n\n\nexport default new GraphQLThinky(thinky);", "language": "javascript" } ] } [/block] Here is the only place where you will import the package. In all other files you will import this **local file** so that you can work on the **GraphqlThinky** instance. **Next**: We need to make sure that we pass to the **graphql context ** the ready to use **Loaders** created by the library. Here as follow: [block:code] { "codes": [ { "code": "// server.js\nimport GT from './graphql-thinky';\n\nconst app = express();\n\napp.use(bodyParser.json());\n// We are going to create new instances of the Loaders\n// for every request\napp.use('/graphql',expressGraphql(request => ({ \n schema,\n context: {\n \tloaders: GT.getModelLoaders() \n },\n graphiql: true,\n pretty: true\n})));\n\napp.listen(7000, function() {\n console.log(\"Server started at port: 7000\");\n});", "language": "javascript" } ] } [/block]
*Dependencies* - [graphql](https://github.com/graphql/graphql-js) - [thinky](https://github.com/neumino/thinky) [block:code] { "codes": [ { "code": "npm install graphql-thinky --save", "language": "text", "name": "" } ] } [/block] To initialize the the package, simply create a file ex: **graphql-thiky.js** [block:code] { "codes": [ { "code": "// graphql-thinky.js\nimport GraphQLThinky from 'graphql-thinky';\nimport models from './../models'; // import your models so they can be bound to thinky\nimport thinky from '../thinky'; // thinky instance\n\n\nexport default new GraphQLThinky(thinky);", "language": "javascript" } ] } [/block] Here is the only place where you will import the package. In all other files you will import this **local file** so that you can work on the **GraphqlThinky** instance. **Next**: We need to make sure that we pass to the **graphql context ** the ready to use **Loaders** created by the library. Here as follow: [block:code] { "codes": [ { "code": "// server.js\nimport GT from './graphql-thinky';\n\nconst app = express();\n\napp.use(bodyParser.json());\n// We are going to create new instances of the Loaders\n// for every request\napp.use('/graphql',expressGraphql(request => ({ \n schema,\n context: {\n \tloaders: GT.getModelLoaders() \n },\n graphiql: true,\n pretty: true\n})));\n\napp.listen(7000, function() {\n console.log(\"Server started at port: 7000\");\n});", "language": "javascript" } ] } [/block]
{"_id":"5844478a94dafe19003dc2da","project":"57462673130acb0e0029b07e","user":"563bb91c2eecbf0d003c5b46","next":{"pages":[],"description":""},"isReference":false,"slug":"graphqltypes","hidden":false,"body":"To get started with **graphql-thinky** we have todo 4 steps to start retrieving data from our **GraphQL** end point.\n\n- Create Model\n- Create GraphQLObjectType\n- Create Query\n- Create Schema\n\nDependencies of the example:\n```\nnpm i thinky graphql graphql-thinky body-parser express express-graphql node-babel --save\n```\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Create Model\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// UserModel.js\\nimport thinky from 'thinky';\\nconst type = thiky.type;\\n\\nexport default thinky.createModel('user', {\\n  name: type.string(),\\n  surname: type.string(),\\n  age: type.number()\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Create GraphQLObjectType\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// UserType.js\\nimport GT from './graphql-thinky';\\n\\n// @returns GraphQLObjectType\\n//{\\n// name: {\\n//  type: GraphQLString \\n// },\\n// surname: {\\n//  type: GraphQLString \\n// },\\n// age: {\\n//  type: GraphQLInt\\n// },\\n//}\\nexport default GT.createModelType('user');\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nWe have now our brand new `UserType`.\n\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Create Query\"\n}\n[/block]\nWe need to create our `GraphQLQuery` so that we can inform the schema on which entry point are available\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Query.js\\nimport { GraphQLObjectType } from 'graphql';\\nimport userType from './user/userType';\\nimport GT from './graphql-thinky';\\n\\nconst { resolve } = GT;\\n\\n// User query definition\\nconst userQuery = {\\n\\n   users: {\\n   \\ttype: new GraphQLList(userType),\\n    resolve: resolve('user')\\n   },\\n   user: {\\n    type: userType,\\n   \\targs: {\\n    \\tid: {\\n      \\ttype: new GraphQLNotNull(GraphQLString)\\n      }\\n    },\\n    resolve: resolve('user')\\n   }\\n};\\n\\n\\n// Export query\\nexport default new GraphQLObjectType({\\n  name: 'Query',\\n  fields: () => ({\\n    ...userQuery\\n  })\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Create Schema\"\n}\n[/block]\nFinally let's create our schema\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Schema.js\\nimport { GraphQLSchema } from 'graphql';\\n\\nimport Query from './query';\\n\\nconst Schema = new GraphQLSchema({\\n  query: Query,\\n});\\n\\nexport default Schema;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Testing end point\"\n}\n[/block]\nHere is a tiny server that we can run to test our GraphQL end point\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// server.js\\nimport express from 'express';\\nimport bodyParser from 'body-parser';\\nimport expressGraphql from 'express-graphql';\\nimport schema from './Schema.js';\\nimport GT from './graphql-thinky';\\n\\nconst app = express();\\n\\napp.use(bodyParser.json());\\napp.use('/graphql',expressGraphql(request => ({ \\n  schema,\\n  context: {\\n  \\tloaders: GT.getModelLoaders() \\n  },\\n  graphiql: true,\\n  pretty: true\\n})));\\n\\napp.listen(7000, function() {\\n\\n  console.log(\\\"Server started at port: 7000\\\");\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n- Start RethinkDB: `rethinkdb --deamon`\n- Start the server `babel-node server.js`\n- Go to `http://localhost:7000/graphql`\n\n\n####Here are the possible queries to run:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"{\\n   users {\\n    id\\n    name,\\n    surname,\\n    age\\n\\t }\\n   \\n   user(id: \\\"someuserid\\\") {\\n     name,\\n     surname\\n   }\\n}\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nCheckout the example into the repo: [graphql-thinky-example](https://github.com/fenos/graphql-thinky/tree/master/example)","category":"5844478994dafe19003dc2d6","version":"5844478994dafe19003dc2d5","updates":[],"link_external":false,"githubsync":"","type":"basic","parentDoc":null,"createdAt":"2016-05-25T22:42:15.341Z","link_url":"","sync_unique":"","excerpt":"Here how to get started","api":{"results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"name":"","code":"{}","language":"json","status":400}]},"settings":"","auth":"required","params":[],"url":""},"order":2,"title":"Getting Started","__v":0,"childrenPages":[]}

Getting Started

Here how to get started

To get started with **graphql-thinky** we have todo 4 steps to start retrieving data from our **GraphQL** end point. - Create Model - Create GraphQLObjectType - Create Query - Create Schema Dependencies of the example: ``` npm i thinky graphql graphql-thinky body-parser express express-graphql node-babel --save ``` [block:api-header] { "type": "basic", "title": "Create Model" } [/block] [block:code] { "codes": [ { "code": "// UserModel.js\nimport thinky from 'thinky';\nconst type = thiky.type;\n\nexport default thinky.createModel('user', {\n name: type.string(),\n surname: type.string(),\n age: type.number()\n});", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Create GraphQLObjectType" } [/block] [block:code] { "codes": [ { "code": "// UserType.js\nimport GT from './graphql-thinky';\n\n// @returns GraphQLObjectType\n//{\n// name: {\n// type: GraphQLString \n// },\n// surname: {\n// type: GraphQLString \n// },\n// age: {\n// type: GraphQLInt\n// },\n//}\nexport default GT.createModelType('user');", "language": "javascript" } ] } [/block] We have now our brand new `UserType`. [block:api-header] { "type": "basic", "title": "Create Query" } [/block] We need to create our `GraphQLQuery` so that we can inform the schema on which entry point are available [block:code] { "codes": [ { "code": "// Query.js\nimport { GraphQLObjectType } from 'graphql';\nimport userType from './user/userType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\n// User query definition\nconst userQuery = {\n\n users: {\n \ttype: new GraphQLList(userType),\n resolve: resolve('user')\n },\n user: {\n type: userType,\n \targs: {\n \tid: {\n \ttype: new GraphQLNotNull(GraphQLString)\n }\n },\n resolve: resolve('user')\n }\n};\n\n\n// Export query\nexport default new GraphQLObjectType({\n name: 'Query',\n fields: () => ({\n ...userQuery\n })\n});", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Create Schema" } [/block] Finally let's create our schema [block:code] { "codes": [ { "code": "// Schema.js\nimport { GraphQLSchema } from 'graphql';\n\nimport Query from './query';\n\nconst Schema = new GraphQLSchema({\n query: Query,\n});\n\nexport default Schema;", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Testing end point" } [/block] Here is a tiny server that we can run to test our GraphQL end point [block:code] { "codes": [ { "code": "// server.js\nimport express from 'express';\nimport bodyParser from 'body-parser';\nimport expressGraphql from 'express-graphql';\nimport schema from './Schema.js';\nimport GT from './graphql-thinky';\n\nconst app = express();\n\napp.use(bodyParser.json());\napp.use('/graphql',expressGraphql(request => ({ \n schema,\n context: {\n \tloaders: GT.getModelLoaders() \n },\n graphiql: true,\n pretty: true\n})));\n\napp.listen(7000, function() {\n\n console.log(\"Server started at port: 7000\");\n});", "language": "javascript" } ] } [/block] - Start RethinkDB: `rethinkdb --deamon` - Start the server `babel-node server.js` - Go to `http://localhost:7000/graphql` ####Here are the possible queries to run: [block:code] { "codes": [ { "code": "{\n users {\n id\n name,\n surname,\n age\n\t }\n \n user(id: \"someuserid\") {\n name,\n surname\n }\n}", "language": "text" } ] } [/block] Checkout the example into the repo: [graphql-thinky-example](https://github.com/fenos/graphql-thinky/tree/master/example)
To get started with **graphql-thinky** we have todo 4 steps to start retrieving data from our **GraphQL** end point. - Create Model - Create GraphQLObjectType - Create Query - Create Schema Dependencies of the example: ``` npm i thinky graphql graphql-thinky body-parser express express-graphql node-babel --save ``` [block:api-header] { "type": "basic", "title": "Create Model" } [/block] [block:code] { "codes": [ { "code": "// UserModel.js\nimport thinky from 'thinky';\nconst type = thiky.type;\n\nexport default thinky.createModel('user', {\n name: type.string(),\n surname: type.string(),\n age: type.number()\n});", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Create GraphQLObjectType" } [/block] [block:code] { "codes": [ { "code": "// UserType.js\nimport GT from './graphql-thinky';\n\n// @returns GraphQLObjectType\n//{\n// name: {\n// type: GraphQLString \n// },\n// surname: {\n// type: GraphQLString \n// },\n// age: {\n// type: GraphQLInt\n// },\n//}\nexport default GT.createModelType('user');", "language": "javascript" } ] } [/block] We have now our brand new `UserType`. [block:api-header] { "type": "basic", "title": "Create Query" } [/block] We need to create our `GraphQLQuery` so that we can inform the schema on which entry point are available [block:code] { "codes": [ { "code": "// Query.js\nimport { GraphQLObjectType } from 'graphql';\nimport userType from './user/userType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\n// User query definition\nconst userQuery = {\n\n users: {\n \ttype: new GraphQLList(userType),\n resolve: resolve('user')\n },\n user: {\n type: userType,\n \targs: {\n \tid: {\n \ttype: new GraphQLNotNull(GraphQLString)\n }\n },\n resolve: resolve('user')\n }\n};\n\n\n// Export query\nexport default new GraphQLObjectType({\n name: 'Query',\n fields: () => ({\n ...userQuery\n })\n});", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Create Schema" } [/block] Finally let's create our schema [block:code] { "codes": [ { "code": "// Schema.js\nimport { GraphQLSchema } from 'graphql';\n\nimport Query from './query';\n\nconst Schema = new GraphQLSchema({\n query: Query,\n});\n\nexport default Schema;", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Testing end point" } [/block] Here is a tiny server that we can run to test our GraphQL end point [block:code] { "codes": [ { "code": "// server.js\nimport express from 'express';\nimport bodyParser from 'body-parser';\nimport expressGraphql from 'express-graphql';\nimport schema from './Schema.js';\nimport GT from './graphql-thinky';\n\nconst app = express();\n\napp.use(bodyParser.json());\napp.use('/graphql',expressGraphql(request => ({ \n schema,\n context: {\n \tloaders: GT.getModelLoaders() \n },\n graphiql: true,\n pretty: true\n})));\n\napp.listen(7000, function() {\n\n console.log(\"Server started at port: 7000\");\n});", "language": "javascript" } ] } [/block] - Start RethinkDB: `rethinkdb --deamon` - Start the server `babel-node server.js` - Go to `http://localhost:7000/graphql` ####Here are the possible queries to run: [block:code] { "codes": [ { "code": "{\n users {\n id\n name,\n surname,\n age\n\t }\n \n user(id: \"someuserid\") {\n name,\n surname\n }\n}", "language": "text" } ] } [/block] Checkout the example into the repo: [graphql-thinky-example](https://github.com/fenos/graphql-thinky/tree/master/example)
{"_id":"5844478a94dafe19003dc2db","next":{"pages":[],"description":""},"link_external":false,"link_url":"","api":{"url":"","results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"name":"","code":"{}","language":"json","status":400}]},"settings":"","auth":"required","params":[]},"slug":"defining-types","type":"basic","__v":0,"category":"5844478994dafe19003dc2d6","createdAt":"2016-05-29T15:06:42.391Z","title":"Defining types","parentDoc":null,"project":"57462673130acb0e0029b07e","githubsync":"","isReference":false,"excerpt":"Thinky model to Graphql","user":"563bb91c2eecbf0d003c5b46","version":"5844478994dafe19003dc2d5","updates":[],"sync_unique":"","hidden":false,"order":3,"body":"Some people think that writing the schema twice might consume time during the development, when i say twice means 1 schema definition for **model** and one schema definition for our **GraphQL**.\n **graphql-thinky** come at the rescue with few helpers function that will allow flexibility during this conversion.\nIt is also important to try to **strongly type** as much as possible the **model definition**, so that the library can create correct definition on the GraphQL side.\n\n**Note**: You can create your GraphQLObjectTypes manually if you wish, without graphql-thinky help.\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"GT.modelToGraphQLDefinition(model,opts = {})\"\n}\n[/block]\nThis function accept a model name or a model instance. When invoked it returns the Graphql definition `Object`, created from Model definition. (Not a GraphQLObjectType).\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// User Model\\nimport thinky from 'thinky';\\nconst {type} = thinky;\\n  \\nconst userModel = thinky.createModel('user', {\\n  name: type.string(),\\n  surname: type.string(),\\n  email: type.string(),\\n  images: type.object().schema({\\n\\t\\tprofile: type.string(),\\n    cover: type.string()\\n  })\\n});\\n\\nexport default userModel;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//userType.js\\nimport GT from './graphql-thinky';\\n\\nconst userTypeDef = GT.modelToGraphQLDefinition('user');\\n\\nconsole.log(userTypeDef);\\n\\n//Object {\\n// name: {\\n//  type: GraphQLString \\n// },\\n// surname: {\\n//  type: GraphQLString \\n// },\\n// email: {\\n//  type: GraphQLString\\n// },\\n// images: GraphqlObjectType('UserImages', {\\n//   profile: {\\n//  \\ttype: GraphQLString \\n// \\t },\\n//   cover: {\\n//    type: GraphQLString\\n//   }\\n// })}\\n//}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n###Options\n\n- **exclude**: `Array` - Exclude fields from the definitnion output\n- **only**: `Array|nil` - Limit the definition with the given fields\n- **globalId**: `Bool` - Create a relay global id, it also create modelID field\n- **allowNull**: `Bool` overwrite if use GraphQLNotNull when requested \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// UserType.js\\nimport GT from './graphql-thinky';\\nimport {GraphQLInt} from 'graphql;\\n\\nconst userTypeDef = GT.modelToGraphQLDefinition('user', {\\n\\texclude: ['name'],\\n  only: null,\\n  globalId: false,\\n  map: {\\n\\t\\temail: 'EMAIL' // or function (email) => emial.toLowerCase()\\n\\t}\\n});\\n\\nconsole.log(userTypeDef);\\n\\n//Object {\\n// surname: {\\n//  type: GraphQLString \\n// },\\n// EMAIL: { // map\\n//  type: GraphQLString\\n// },\\n// images: GraphqlObjectType('UserImages', {\\n//   profile: {\\n//  \\ttype: GraphQLString \\n// \\t },\\n//   cover: {\\n//    type: GraphQLString\\n//   }\\n// })}\\n//}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"GT.createModelType(model, opts = {})\"\n}\n[/block]\nThis function allow to create a **GraphQLObjectType** instance, it's used to export a **GraphQLObjectType**. The benefit to use this function when create your **types** is that \n\n- Will create customisable ObjectType from your model definition\n- Will register the instance to relay automatically. So you don't have to bind it to fetch the global id.\n\nThere is no restriction if you create your type with your definition.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import GT from './graphql-thinky';\\nimport {GraphQLInt} from 'graphql;\\n\\nconst userTypeDef = GT.createModelType('user', {\\n\\texclude: ['name'],\\n  only: null,\\n  globalId: false,\\n  fields: () => ({ // additional fields\\n  \\tage: {\\n      type: GraphQLInt\\n    }\\n  }),\\n  map: {\\n\\t\\temail: 'EMAIL' // of function (email) => emial.toLowerCase()\\n\\t}\\n});\\n\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n###Options\n\n- **exclude**: `Array` - Exclude fields from the definitnion output\n- **only**: `Array|nil` - Limit the definition with the given fields\n- **globalId**: `Bool` - Create a relay global id, it also create modelID field\n- **allowNull**: `Bool` overwrite if use GraphQLNotNull when requested \n- **map**: `Object` - decorate schema structure\n- **fields**: `Function` - Add graphql fields to the definition","childrenPages":[]}

Defining types

Thinky model to Graphql

Some people think that writing the schema twice might consume time during the development, when i say twice means 1 schema definition for **model** and one schema definition for our **GraphQL**. **graphql-thinky** come at the rescue with few helpers function that will allow flexibility during this conversion. It is also important to try to **strongly type** as much as possible the **model definition**, so that the library can create correct definition on the GraphQL side. **Note**: You can create your GraphQLObjectTypes manually if you wish, without graphql-thinky help. [block:api-header] { "type": "basic", "title": "GT.modelToGraphQLDefinition(model,opts = {})" } [/block] This function accept a model name or a model instance. When invoked it returns the Graphql definition `Object`, created from Model definition. (Not a GraphQLObjectType). [block:code] { "codes": [ { "code": "// User Model\nimport thinky from 'thinky';\nconst {type} = thinky;\n \nconst userModel = thinky.createModel('user', {\n name: type.string(),\n surname: type.string(),\n email: type.string(),\n images: type.object().schema({\n\t\tprofile: type.string(),\n cover: type.string()\n })\n});\n\nexport default userModel;", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "//userType.js\nimport GT from './graphql-thinky';\n\nconst userTypeDef = GT.modelToGraphQLDefinition('user');\n\nconsole.log(userTypeDef);\n\n//Object {\n// name: {\n// type: GraphQLString \n// },\n// surname: {\n// type: GraphQLString \n// },\n// email: {\n// type: GraphQLString\n// },\n// images: GraphqlObjectType('UserImages', {\n// profile: {\n// \ttype: GraphQLString \n// \t },\n// cover: {\n// type: GraphQLString\n// }\n// })}\n//}", "language": "javascript" } ] } [/block] ###Options - **exclude**: `Array` - Exclude fields from the definitnion output - **only**: `Array|nil` - Limit the definition with the given fields - **globalId**: `Bool` - Create a relay global id, it also create modelID field - **allowNull**: `Bool` overwrite if use GraphQLNotNull when requested [block:code] { "codes": [ { "code": "// UserType.js\nimport GT from './graphql-thinky';\nimport {GraphQLInt} from 'graphql;\n\nconst userTypeDef = GT.modelToGraphQLDefinition('user', {\n\texclude: ['name'],\n only: null,\n globalId: false,\n map: {\n\t\temail: 'EMAIL' // or function (email) => emial.toLowerCase()\n\t}\n});\n\nconsole.log(userTypeDef);\n\n//Object {\n// surname: {\n// type: GraphQLString \n// },\n// EMAIL: { // map\n// type: GraphQLString\n// },\n// images: GraphqlObjectType('UserImages', {\n// profile: {\n// \ttype: GraphQLString \n// \t },\n// cover: {\n// type: GraphQLString\n// }\n// })}\n//}", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "GT.createModelType(model, opts = {})" } [/block] This function allow to create a **GraphQLObjectType** instance, it's used to export a **GraphQLObjectType**. The benefit to use this function when create your **types** is that - Will create customisable ObjectType from your model definition - Will register the instance to relay automatically. So you don't have to bind it to fetch the global id. There is no restriction if you create your type with your definition. [block:code] { "codes": [ { "code": "import GT from './graphql-thinky';\nimport {GraphQLInt} from 'graphql;\n\nconst userTypeDef = GT.createModelType('user', {\n\texclude: ['name'],\n only: null,\n globalId: false,\n fields: () => ({ // additional fields\n \tage: {\n type: GraphQLInt\n }\n }),\n map: {\n\t\temail: 'EMAIL' // of function (email) => emial.toLowerCase()\n\t}\n});\n", "language": "javascript" } ] } [/block] ###Options - **exclude**: `Array` - Exclude fields from the definitnion output - **only**: `Array|nil` - Limit the definition with the given fields - **globalId**: `Bool` - Create a relay global id, it also create modelID field - **allowNull**: `Bool` overwrite if use GraphQLNotNull when requested - **map**: `Object` - decorate schema structure - **fields**: `Function` - Add graphql fields to the definition
Some people think that writing the schema twice might consume time during the development, when i say twice means 1 schema definition for **model** and one schema definition for our **GraphQL**. **graphql-thinky** come at the rescue with few helpers function that will allow flexibility during this conversion. It is also important to try to **strongly type** as much as possible the **model definition**, so that the library can create correct definition on the GraphQL side. **Note**: You can create your GraphQLObjectTypes manually if you wish, without graphql-thinky help. [block:api-header] { "type": "basic", "title": "GT.modelToGraphQLDefinition(model,opts = {})" } [/block] This function accept a model name or a model instance. When invoked it returns the Graphql definition `Object`, created from Model definition. (Not a GraphQLObjectType). [block:code] { "codes": [ { "code": "// User Model\nimport thinky from 'thinky';\nconst {type} = thinky;\n \nconst userModel = thinky.createModel('user', {\n name: type.string(),\n surname: type.string(),\n email: type.string(),\n images: type.object().schema({\n\t\tprofile: type.string(),\n cover: type.string()\n })\n});\n\nexport default userModel;", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "//userType.js\nimport GT from './graphql-thinky';\n\nconst userTypeDef = GT.modelToGraphQLDefinition('user');\n\nconsole.log(userTypeDef);\n\n//Object {\n// name: {\n// type: GraphQLString \n// },\n// surname: {\n// type: GraphQLString \n// },\n// email: {\n// type: GraphQLString\n// },\n// images: GraphqlObjectType('UserImages', {\n// profile: {\n// \ttype: GraphQLString \n// \t },\n// cover: {\n// type: GraphQLString\n// }\n// })}\n//}", "language": "javascript" } ] } [/block] ###Options - **exclude**: `Array` - Exclude fields from the definitnion output - **only**: `Array|nil` - Limit the definition with the given fields - **globalId**: `Bool` - Create a relay global id, it also create modelID field - **allowNull**: `Bool` overwrite if use GraphQLNotNull when requested [block:code] { "codes": [ { "code": "// UserType.js\nimport GT from './graphql-thinky';\nimport {GraphQLInt} from 'graphql;\n\nconst userTypeDef = GT.modelToGraphQLDefinition('user', {\n\texclude: ['name'],\n only: null,\n globalId: false,\n map: {\n\t\temail: 'EMAIL' // or function (email) => emial.toLowerCase()\n\t}\n});\n\nconsole.log(userTypeDef);\n\n//Object {\n// surname: {\n// type: GraphQLString \n// },\n// EMAIL: { // map\n// type: GraphQLString\n// },\n// images: GraphqlObjectType('UserImages', {\n// profile: {\n// \ttype: GraphQLString \n// \t },\n// cover: {\n// type: GraphQLString\n// }\n// })}\n//}", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "GT.createModelType(model, opts = {})" } [/block] This function allow to create a **GraphQLObjectType** instance, it's used to export a **GraphQLObjectType**. The benefit to use this function when create your **types** is that - Will create customisable ObjectType from your model definition - Will register the instance to relay automatically. So you don't have to bind it to fetch the global id. There is no restriction if you create your type with your definition. [block:code] { "codes": [ { "code": "import GT from './graphql-thinky';\nimport {GraphQLInt} from 'graphql;\n\nconst userTypeDef = GT.createModelType('user', {\n\texclude: ['name'],\n only: null,\n globalId: false,\n fields: () => ({ // additional fields\n \tage: {\n type: GraphQLInt\n }\n }),\n map: {\n\t\temail: 'EMAIL' // of function (email) => emial.toLowerCase()\n\t}\n});\n", "language": "javascript" } ] } [/block] ###Options - **exclude**: `Array` - Exclude fields from the definitnion output - **only**: `Array|nil` - Limit the definition with the given fields - **globalId**: `Bool` - Create a relay global id, it also create modelID field - **allowNull**: `Bool` overwrite if use GraphQLNotNull when requested - **map**: `Object` - decorate schema structure - **fields**: `Function` - Add graphql fields to the definition
{"_id":"5844478a94dafe19003dc2dc","githubsync":"","hidden":false,"project":"57462673130acb0e0029b07e","user":"563bb91c2eecbf0d003c5b46","version":"5844478994dafe19003dc2d5","createdAt":"2016-05-29T18:34:10.642Z","order":4,"body":"Will now see how **graphql-thinky** can resolve your definition to an optimised query to run in RethinkDB. The big help is given from **thinky** the powerful ORM.\n\nThis function is the one that allow to request **thinky** todo certain moves depending the request.\n\n- It automatically bind all the arguments to a **filter** argument. \n- it can automatically limit result if a **limit** argument has been requested\n- it can automatically resolve a order if provided \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Resolving data\"\n}\n[/block]\nFore example, we are going to force the limit of the result to 10. \nWe also forcing the order by `name:desc`.\nWe use the **before** and **after** hooks that allow us to run custom logic, before and after the Database query resolution. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// UserQuery.js\\nimport userType from './userType';\\nimport GT from './graphql-thinky';\\n\\nconst { resolve } = GT;\\n\\nexport default {\\n\\n   users: {\\n     type: userType,\\n     // Run before query is instructed to thinky\\n     resolve: resolve('user',null,{\\n\\t\\t\\t\\tbefore: (opts,  args, context, info) => {\\n\\t\\t\\t\\t\\topts.limit = 10;\\n          opts.orderBy = 'name:desc';\\n          \\n          // opts.attributes.concat(['otherfield']);\\n          // opt.filter.name = 'jhon';\\n       \\t\\treturn opts;\\n     \\t\\t},\\n       // After the result set has been fetched\\n       // allows you to decorate\\n       after: (result) => {\\n\\t\\t\\t\\treturn result.map((item) => item.name.toUpperCase());\\n       }\\n     })\\n   }\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","slug":"resolve","updates":[],"next":{"pages":[],"description":""},"link_external":false,"link_url":"","parentDoc":null,"isReference":false,"excerpt":"Resolving data","type":"basic","title":"Resolve","__v":0,"category":"5844478994dafe19003dc2d6","sync_unique":"","api":{"url":"","results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"name":"","code":"{}","language":"json","status":400}]},"settings":"","auth":"required","params":[]},"childrenPages":[]}

Resolve

Resolving data

Will now see how **graphql-thinky** can resolve your definition to an optimised query to run in RethinkDB. The big help is given from **thinky** the powerful ORM. This function is the one that allow to request **thinky** todo certain moves depending the request. - It automatically bind all the arguments to a **filter** argument. - it can automatically limit result if a **limit** argument has been requested - it can automatically resolve a order if provided [block:api-header] { "type": "basic", "title": "Resolving data" } [/block] Fore example, we are going to force the limit of the result to 10. We also forcing the order by `name:desc`. We use the **before** and **after** hooks that allow us to run custom logic, before and after the Database query resolution. [block:code] { "codes": [ { "code": "// UserQuery.js\nimport userType from './userType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\nexport default {\n\n users: {\n type: userType,\n // Run before query is instructed to thinky\n resolve: resolve('user',null,{\n\t\t\t\tbefore: (opts, args, context, info) => {\n\t\t\t\t\topts.limit = 10;\n opts.orderBy = 'name:desc';\n \n // opts.attributes.concat(['otherfield']);\n // opt.filter.name = 'jhon';\n \t\treturn opts;\n \t\t},\n // After the result set has been fetched\n // allows you to decorate\n after: (result) => {\n\t\t\t\treturn result.map((item) => item.name.toUpperCase());\n }\n })\n }\n}", "language": "javascript" } ] } [/block]
Will now see how **graphql-thinky** can resolve your definition to an optimised query to run in RethinkDB. The big help is given from **thinky** the powerful ORM. This function is the one that allow to request **thinky** todo certain moves depending the request. - It automatically bind all the arguments to a **filter** argument. - it can automatically limit result if a **limit** argument has been requested - it can automatically resolve a order if provided [block:api-header] { "type": "basic", "title": "Resolving data" } [/block] Fore example, we are going to force the limit of the result to 10. We also forcing the order by `name:desc`. We use the **before** and **after** hooks that allow us to run custom logic, before and after the Database query resolution. [block:code] { "codes": [ { "code": "// UserQuery.js\nimport userType from './userType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\nexport default {\n\n users: {\n type: userType,\n // Run before query is instructed to thinky\n resolve: resolve('user',null,{\n\t\t\t\tbefore: (opts, args, context, info) => {\n\t\t\t\t\topts.limit = 10;\n opts.orderBy = 'name:desc';\n \n // opts.attributes.concat(['otherfield']);\n // opt.filter.name = 'jhon';\n \t\treturn opts;\n \t\t},\n // After the result set has been fetched\n // allows you to decorate\n after: (result) => {\n\t\t\t\treturn result.map((item) => item.name.toUpperCase());\n }\n })\n }\n}", "language": "javascript" } ] } [/block]
{"_id":"5844478a94dafe19003dc2dd","excerpt":"","title":"Resolve relations","project":"57462673130acb0e0029b07e","updates":[],"next":{"description":"","pages":[]},"sync_unique":"","body":"Let's see how **graphql-thinky** allow us to query related types:\nex: Users** has many** tasks\n\n- You must to set the relations on the targeted models.\n- You must reference the relation name on the `resolve` function\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Models\"\n}\n[/block]\nLet's create our 2 models, **User** - **Task**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Models.js\\nimport thinky from '../thinky';\\nconst type = thinky.type;\\n\\n// User Model\\nconst User = thinky.createModel('user', {\\n  name: type.string()\\n});\\n\\nUser.relations = () => {\\n  User.hasMany(thinky.models.task,'tasks','id','user_id');\\n}\\n\\nexport User;\\n\\n// Task Model\\nconst Task = thinky.createModel('task', {\\n  text: type.string(),\\n  completed: type.boolean(),\\n  user_id: type.string().allowNull(false)\\n});\\n\\nTask.relations = () => {\\n  Task.belongsTo(thinky.models.user,'user','user_id','id');\\n}\\n\\nexport Task;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"GQL Object Types\"\n}\n[/block]\nWhen creating object type we can pass the **fields** option to extend the shape of the final type.\nWe do as such to include our new **tasks** node to the **UserType**.\n\nThe `resolve` function will now accept a second argument which is the relation name of the **user** model. It will automatically resolve as a **join** instruction to thinky.\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//UserType.js\\nimport taskType from './taskType';\\nimport GT from './graphql-thinky';\\n\\nconst { resolve } = GT;\\n\\nconst UserType = GT.createModelType('user', {\\n\\tfields: () => ({\\n  \\ttasks: {\\n    \\ttype: taskType,\\n      resolve: resolve('user','tasks')\\n  })\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// TaskType.js\\nimport userType from './userType';\\nimport GT from './graphql-thinky';\\n\\nconst { resolve } = GT;\\n\\nexport default GT.createModelType('task', {\\n\\tfields: () => ({\\n  \\ttasks: {\\n    \\ttype: userType,\\n      resolve: resolve('task','user') // task belongs to user\\n  })\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nYou can still use the 3rd argument to provide custom filtering.","version":"5844478994dafe19003dc2d5","createdAt":"2016-05-28T17:48:17.601Z","link_external":false,"githubsync":"","hidden":false,"slug":"related-types","__v":0,"category":"5844478994dafe19003dc2d6","user":"563bb91c2eecbf0d003c5b46","link_url":"","isReference":false,"order":5,"parentDoc":null,"api":{"auth":"required","params":[],"url":"","results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"name":"","code":"{}","language":"json","status":400}]},"settings":""},"type":"basic","childrenPages":[]}

Resolve relations


Let's see how **graphql-thinky** allow us to query related types: ex: Users** has many** tasks - You must to set the relations on the targeted models. - You must reference the relation name on the `resolve` function [block:api-header] { "type": "basic", "title": "Models" } [/block] Let's create our 2 models, **User** - **Task** [block:code] { "codes": [ { "code": "// Models.js\nimport thinky from '../thinky';\nconst type = thinky.type;\n\n// User Model\nconst User = thinky.createModel('user', {\n name: type.string()\n});\n\nUser.relations = () => {\n User.hasMany(thinky.models.task,'tasks','id','user_id');\n}\n\nexport User;\n\n// Task Model\nconst Task = thinky.createModel('task', {\n text: type.string(),\n completed: type.boolean(),\n user_id: type.string().allowNull(false)\n});\n\nTask.relations = () => {\n Task.belongsTo(thinky.models.user,'user','user_id','id');\n}\n\nexport Task;", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "GQL Object Types" } [/block] When creating object type we can pass the **fields** option to extend the shape of the final type. We do as such to include our new **tasks** node to the **UserType**. The `resolve` function will now accept a second argument which is the relation name of the **user** model. It will automatically resolve as a **join** instruction to thinky. [block:code] { "codes": [ { "code": "//UserType.js\nimport taskType from './taskType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\nconst UserType = GT.createModelType('user', {\n\tfields: () => ({\n \ttasks: {\n \ttype: taskType,\n resolve: resolve('user','tasks')\n })\n});", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "// TaskType.js\nimport userType from './userType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\nexport default GT.createModelType('task', {\n\tfields: () => ({\n \ttasks: {\n \ttype: userType,\n resolve: resolve('task','user') // task belongs to user\n })\n});", "language": "javascript" } ] } [/block] You can still use the 3rd argument to provide custom filtering.
Let's see how **graphql-thinky** allow us to query related types: ex: Users** has many** tasks - You must to set the relations on the targeted models. - You must reference the relation name on the `resolve` function [block:api-header] { "type": "basic", "title": "Models" } [/block] Let's create our 2 models, **User** - **Task** [block:code] { "codes": [ { "code": "// Models.js\nimport thinky from '../thinky';\nconst type = thinky.type;\n\n// User Model\nconst User = thinky.createModel('user', {\n name: type.string()\n});\n\nUser.relations = () => {\n User.hasMany(thinky.models.task,'tasks','id','user_id');\n}\n\nexport User;\n\n// Task Model\nconst Task = thinky.createModel('task', {\n text: type.string(),\n completed: type.boolean(),\n user_id: type.string().allowNull(false)\n});\n\nTask.relations = () => {\n Task.belongsTo(thinky.models.user,'user','user_id','id');\n}\n\nexport Task;", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "GQL Object Types" } [/block] When creating object type we can pass the **fields** option to extend the shape of the final type. We do as such to include our new **tasks** node to the **UserType**. The `resolve` function will now accept a second argument which is the relation name of the **user** model. It will automatically resolve as a **join** instruction to thinky. [block:code] { "codes": [ { "code": "//UserType.js\nimport taskType from './taskType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\nconst UserType = GT.createModelType('user', {\n\tfields: () => ({\n \ttasks: {\n \ttype: taskType,\n resolve: resolve('user','tasks')\n })\n});", "language": "javascript" } ] } [/block] [block:code] { "codes": [ { "code": "// TaskType.js\nimport userType from './userType';\nimport GT from './graphql-thinky';\n\nconst { resolve } = GT;\n\nexport default GT.createModelType('task', {\n\tfields: () => ({\n \ttasks: {\n \ttype: userType,\n resolve: resolve('task','user') // task belongs to user\n })\n});", "language": "javascript" } ] } [/block] You can still use the 3rd argument to provide custom filtering.
{"_id":"5844478a94dafe19003dc2de","sync_unique":"","isReference":false,"body":"**graphql-thinky** has built in support for RelayJs. There few things to see to be ready and use Relay complaint entry points. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Map Types\"\n}\n[/block]\nAn important piece is to bind the models with they're own GraphQLTypes so that the library can figure out how to retrieve Types based to a relay globalD.\n\n**Note**: If you use the function [createModelType](doc:defining-types#gtcreatemodeltype--model-opts--) this is done internally for you. So you don't have to manually bind your models. If not then as following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//graphql-thinky.js\\nimport graphqlThinky from 'graphql-thikny';\\nimport thinky from './thinky';\\n\\nconst GraphQLThinky = new graphqlThinky(thinky);\\n\\nGraphQLThinky.nodeTypeMapper.mapTypes({\\n  user: userType,\\n\\tcustom: {\\n    type: customType,\\n    resolve(globalId, context) {\\n      const { id } = fromGlobalId(globalId);\\n      return resolveIdFromSomewhere(id);\\n    }\\n  }\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nSecond we need to assign the **globalID** + **nodeField** interface to our Type.\nAgain if you use [createModelType](doc:defining-types#gtcreatemodeltype--model-opts--)  function will be as easy as set the option **globalId: true**. If instead you create your types manually: \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import graphqlThinky from './graphql-thikny';\\nimport { GraphQLObjectType } from 'graphql;\\nimport { globalIdField } from 'graphql-relay';\\n\\nexport default new GraphQLObjectType({\\n\\tname: 'user',\\n  fields: () => ({\\n    id: globalIdField('user')\\n  }),\\n  interfaces: () => [graphqlThinky.nodeInterface]\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Resolving Connection\"\n}\n[/block]\nLet's see now how to resolve connections based on the relay definition.\nHere is our **userQuery.js**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//userQuery.js\\nimport GT from './graphql-thinky';\\nimport userType from './userType';\\n\\nconst { connect } = GT;\\n\\nexport default {\\n\\tusersConnection: {\\n\\t\\t...connect('user',null, {\\n\\t\\t\\tconnection: {\\n\\t\\t\\t\\tname: ' UserConnection',\\n        type: userType\\n\\t\\t\\t}\\n\\t\\t})\\n\\t}\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nWe are now ready to test our connection with the following GraphQL query:\n\n```\n{\n  usersConnection {\n     edges {\n        node {\n           id\n           userID\n           name\n        }\n     }\n  }\n}\n```\n\nYou can also use all the relay arguments, such as: **first, last, after, before**\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Connect = (modelName, related, opts = {})\"\n}\n[/block]\nThe connection function is similar to the **resolve** function. Let's look it's arguments\n\n- modelName: `string` - Thinky model name - **Required**\n- related: `string|nil` - Relation name of the model name provided\n- opts: `Object`: \n   - args: `Object` - provide additional arguments\n   - connection: `Object` - connection info  - **Required**\n      - name: `string` - name of the connection - **Required**\n      - type: `GraphQLObjectType` type of the connection - **Required**\n      - before: `Function` - before hook\n      - after: `Function` - after hook","excerpt":"Relay support","version":"5844478994dafe19003dc2d5","link_external":false,"link_url":"","title":"Relay","next":{"pages":[],"description":""},"hidden":false,"parentDoc":null,"project":"57462673130acb0e0029b07e","user":"563bb91c2eecbf0d003c5b46","updates":[],"createdAt":"2016-05-29T21:27:29.428Z","githubsync":"","type":"basic","__v":0,"category":"5844478994dafe19003dc2d6","api":{"url":"","results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"status":400,"name":"","code":"{}","language":"json"}]},"settings":"","auth":"required","params":[]},"order":6,"slug":"relay","childrenPages":[]}

Relay

Relay support

**graphql-thinky** has built in support for RelayJs. There few things to see to be ready and use Relay complaint entry points. [block:api-header] { "type": "basic", "title": "Map Types" } [/block] An important piece is to bind the models with they're own GraphQLTypes so that the library can figure out how to retrieve Types based to a relay globalD. **Note**: If you use the function [createModelType](doc:defining-types#gtcreatemodeltype--model-opts--) this is done internally for you. So you don't have to manually bind your models. If not then as following: [block:code] { "codes": [ { "code": "//graphql-thinky.js\nimport graphqlThinky from 'graphql-thikny';\nimport thinky from './thinky';\n\nconst GraphQLThinky = new graphqlThinky(thinky);\n\nGraphQLThinky.nodeTypeMapper.mapTypes({\n user: userType,\n\tcustom: {\n type: customType,\n resolve(globalId, context) {\n const { id } = fromGlobalId(globalId);\n return resolveIdFromSomewhere(id);\n }\n }\n});", "language": "javascript" } ] } [/block] Second we need to assign the **globalID** + **nodeField** interface to our Type. Again if you use [createModelType](doc:defining-types#gtcreatemodeltype--model-opts--) function will be as easy as set the option **globalId: true**. If instead you create your types manually: [block:code] { "codes": [ { "code": "import graphqlThinky from './graphql-thikny';\nimport { GraphQLObjectType } from 'graphql;\nimport { globalIdField } from 'graphql-relay';\n\nexport default new GraphQLObjectType({\n\tname: 'user',\n fields: () => ({\n id: globalIdField('user')\n }),\n interfaces: () => [graphqlThinky.nodeInterface]\n});", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Resolving Connection" } [/block] Let's see now how to resolve connections based on the relay definition. Here is our **userQuery.js** [block:code] { "codes": [ { "code": "//userQuery.js\nimport GT from './graphql-thinky';\nimport userType from './userType';\n\nconst { connect } = GT;\n\nexport default {\n\tusersConnection: {\n\t\t...connect('user',null, {\n\t\t\tconnection: {\n\t\t\t\tname: ' UserConnection',\n type: userType\n\t\t\t}\n\t\t})\n\t}\n}", "language": "javascript" } ] } [/block] We are now ready to test our connection with the following GraphQL query: ``` { usersConnection { edges { node { id userID name } } } } ``` You can also use all the relay arguments, such as: **first, last, after, before** [block:api-header] { "type": "basic", "title": "Connect = (modelName, related, opts = {})" } [/block] The connection function is similar to the **resolve** function. Let's look it's arguments - modelName: `string` - Thinky model name - **Required** - related: `string|nil` - Relation name of the model name provided - opts: `Object`: - args: `Object` - provide additional arguments - connection: `Object` - connection info - **Required** - name: `string` - name of the connection - **Required** - type: `GraphQLObjectType` type of the connection - **Required** - before: `Function` - before hook - after: `Function` - after hook
**graphql-thinky** has built in support for RelayJs. There few things to see to be ready and use Relay complaint entry points. [block:api-header] { "type": "basic", "title": "Map Types" } [/block] An important piece is to bind the models with they're own GraphQLTypes so that the library can figure out how to retrieve Types based to a relay globalD. **Note**: If you use the function [createModelType](doc:defining-types#gtcreatemodeltype--model-opts--) this is done internally for you. So you don't have to manually bind your models. If not then as following: [block:code] { "codes": [ { "code": "//graphql-thinky.js\nimport graphqlThinky from 'graphql-thikny';\nimport thinky from './thinky';\n\nconst GraphQLThinky = new graphqlThinky(thinky);\n\nGraphQLThinky.nodeTypeMapper.mapTypes({\n user: userType,\n\tcustom: {\n type: customType,\n resolve(globalId, context) {\n const { id } = fromGlobalId(globalId);\n return resolveIdFromSomewhere(id);\n }\n }\n});", "language": "javascript" } ] } [/block] Second we need to assign the **globalID** + **nodeField** interface to our Type. Again if you use [createModelType](doc:defining-types#gtcreatemodeltype--model-opts--) function will be as easy as set the option **globalId: true**. If instead you create your types manually: [block:code] { "codes": [ { "code": "import graphqlThinky from './graphql-thikny';\nimport { GraphQLObjectType } from 'graphql;\nimport { globalIdField } from 'graphql-relay';\n\nexport default new GraphQLObjectType({\n\tname: 'user',\n fields: () => ({\n id: globalIdField('user')\n }),\n interfaces: () => [graphqlThinky.nodeInterface]\n});", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "Resolving Connection" } [/block] Let's see now how to resolve connections based on the relay definition. Here is our **userQuery.js** [block:code] { "codes": [ { "code": "//userQuery.js\nimport GT from './graphql-thinky';\nimport userType from './userType';\n\nconst { connect } = GT;\n\nexport default {\n\tusersConnection: {\n\t\t...connect('user',null, {\n\t\t\tconnection: {\n\t\t\t\tname: ' UserConnection',\n type: userType\n\t\t\t}\n\t\t})\n\t}\n}", "language": "javascript" } ] } [/block] We are now ready to test our connection with the following GraphQL query: ``` { usersConnection { edges { node { id userID name } } } } ``` You can also use all the relay arguments, such as: **first, last, after, before** [block:api-header] { "type": "basic", "title": "Connect = (modelName, related, opts = {})" } [/block] The connection function is similar to the **resolve** function. Let's look it's arguments - modelName: `string` - Thinky model name - **Required** - related: `string|nil` - Relation name of the model name provided - opts: `Object`: - args: `Object` - provide additional arguments - connection: `Object` - connection info - **Required** - name: `string` - name of the connection - **Required** - type: `GraphQLObjectType` type of the connection - **Required** - before: `Function` - before hook - after: `Function` - after hook
{"_id":"5844478a94dafe19003dc2df","link_url":"","sync_unique":"","hidden":false,"excerpt":"Contribution guideline","type":"basic","__v":0,"version":"5844478994dafe19003dc2d5","next":{"pages":[],"description":""},"slug":"contribution","user":"563bb91c2eecbf0d003c5b46","order":7,"githubsync":"","api":{"auth":"required","params":[],"url":"","results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"code":"{}","language":"json","status":400,"name":""}]},"settings":""},"title":"Contribution","project":"57462673130acb0e0029b07e","link_external":false,"updates":[],"createdAt":"2016-05-31T12:22:54.235Z","isReference":false,"body":"To contribute to the repo, you can do it in few ways:\n\n- **Bugs**: Open a issue into github, add the test case to reproduce the bug (if possible)\n- **New features**: Open an issue into github, explain the needs of the feature once the feature is agreed i will happy to receive Pull request with related tests cases, if you can't do that I'll try to help for the implementation.\n\n**Note**: As the repository grow we will add more strict guidelines for contribution","category":"5844478994dafe19003dc2d6","parentDoc":null,"childrenPages":[]}

Contribution

Contribution guideline

To contribute to the repo, you can do it in few ways: - **Bugs**: Open a issue into github, add the test case to reproduce the bug (if possible) - **New features**: Open an issue into github, explain the needs of the feature once the feature is agreed i will happy to receive Pull request with related tests cases, if you can't do that I'll try to help for the implementation. **Note**: As the repository grow we will add more strict guidelines for contribution
To contribute to the repo, you can do it in few ways: - **Bugs**: Open a issue into github, add the test case to reproduce the bug (if possible) - **New features**: Open an issue into github, explain the needs of the feature once the feature is agreed i will happy to receive Pull request with related tests cases, if you can't do that I'll try to help for the implementation. **Note**: As the repository grow we will add more strict guidelines for contribution
{"_id":"5844478a94dafe19003dc2e0","updates":[],"link_url":"","body":"##### I want to thanks the technologies that allowed **graphql-thinky** to be built. Here the links\n\n- [Thinky](http://thinky.io)\n- [GraphQL](http://graphql.org)\n- [RelayJs](https://facebook.github.io/relay)\n- [RethinkDB](http://www.rethinkdb.com/)\n- [graphql-sequelize](https://github.com/mickhansen/graphql-sequelize)\n- [ava](https://github.com/avajs/ava)\n- [babel](https://babeljs.io)\n- And all the entire MIT projects communities \n\n\n##### Folks & contributors of the repo\n\n**Developers**\n- Me: [fenos](https://github.com/fenos)\n\n\n**Doc Design and Logo**:\n- [Tan](https://github.com/slow-motion)\n- [Fatos](https://www.behance.net/roza)","excerpt":"","__v":0,"category":"5844478994dafe19003dc2d6","project":"57462673130acb0e0029b07e","user":"563bb91c2eecbf0d003c5b46","next":{"pages":[],"description":""},"sync_unique":"","api":{"results":{"codes":[{"name":"","code":"{}","language":"json","status":200},{"status":400,"name":"","code":"{}","language":"json"}]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":8,"parentDoc":null,"version":"5844478994dafe19003dc2d5","githubsync":"","type":"basic","title":"Credits","createdAt":"2016-05-30T12:37:43.555Z","link_external":false,"hidden":false,"slug":"credits","childrenPages":[]}

Credits


##### I want to thanks the technologies that allowed **graphql-thinky** to be built. Here the links - [Thinky](http://thinky.io) - [GraphQL](http://graphql.org) - [RelayJs](https://facebook.github.io/relay) - [RethinkDB](http://www.rethinkdb.com/) - [graphql-sequelize](https://github.com/mickhansen/graphql-sequelize) - [ava](https://github.com/avajs/ava) - [babel](https://babeljs.io) - And all the entire MIT projects communities ##### Folks & contributors of the repo **Developers** - Me: [fenos](https://github.com/fenos) **Doc Design and Logo**: - [Tan](https://github.com/slow-motion) - [Fatos](https://www.behance.net/roza)
##### I want to thanks the technologies that allowed **graphql-thinky** to be built. Here the links - [Thinky](http://thinky.io) - [GraphQL](http://graphql.org) - [RelayJs](https://facebook.github.io/relay) - [RethinkDB](http://www.rethinkdb.com/) - [graphql-sequelize](https://github.com/mickhansen/graphql-sequelize) - [ava](https://github.com/avajs/ava) - [babel](https://babeljs.io) - And all the entire MIT projects communities ##### Folks & contributors of the repo **Developers** - Me: [fenos](https://github.com/fenos) **Doc Design and Logo**: - [Tan](https://github.com/slow-motion) - [Fatos](https://www.behance.net/roza)