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

Create Model

// UserModel.js import thinky from 'thinky'; const type = thiky.type; export default thinky.createModel('user', { name: type.string(), surname: type.string(), age: type.number() });

Create GraphQLObjectType

// UserType.js import GT from './graphql-thinky'; // @returns GraphQLObjectType //{ // name: { // type: GraphQLString // }, // surname: { // type: GraphQLString // }, // age: { // type: GraphQLInt // }, //} export default GT.createModelType('user');

We have now our brand new UserType.

Create Query

We need to create our GraphQLQuery so that we can inform the schema on which entry point are available

// Query.js import { GraphQLObjectType } from 'graphql'; import userType from './user/userType'; import GT from './graphql-thinky'; const { resolve } = GT; // User query definition const userQuery = { users: { type: new GraphQLList(userType), resolve: resolve('user') }, user: { type: userType, args: { id: { type: new GraphQLNotNull(GraphQLString) } }, resolve: resolve('user') } }; // Export query export default new GraphQLObjectType({ name: 'Query', fields: () => ({ ...userQuery }) });

Create Schema

Finally let's create our schema

// Schema.js import { GraphQLSchema } from 'graphql'; import Query from './query'; const Schema = new GraphQLSchema({ query: Query, }); export default Schema;

Testing end point

Here is a tiny server that we can run to test our GraphQL end point

// server.js import express from 'express'; import bodyParser from 'body-parser'; import expressGraphql from 'express-graphql'; import schema from './Schema.js'; import GT from './graphql-thinky'; const app = express(); app.use(bodyParser.json()); app.use('/graphql',expressGraphql(request => ({ schema, context: { loaders: GT.getModelLoaders() }, graphiql: true, pretty: true }))); app.listen(7000, function() { console.log("Server started at port: 7000"); });
  • Start RethinkDB: rethinkdb --deamon
  • Start the server babel-node server.js
  • Go to http://localhost:7000/graphql

Here are the possible queries to run:

{ users { id name, surname, age } user(id: "someuserid") { name, surname } }

Checkout the example into the repo: graphql-thinky-example


Did this page help you?