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.

GT.modelToGraphQLDefinition(model,opts = {})

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

// User Model
import thinky from 'thinky';
const {type} = thinky;
  
const userModel = thinky.createModel('user', {
  name: type.string(),
  surname: type.string(),
  email: type.string(),
  images: type.object().schema({
		profile: type.string(),
    cover: type.string()
  })
});

export default userModel;
//userType.js
import GT from './graphql-thinky';

const userTypeDef = GT.modelToGraphQLDefinition('user');

console.log(userTypeDef);

//Object {
// name: {
//  type: GraphQLString 
// },
// surname: {
//  type: GraphQLString 
// },
// email: {
//  type: GraphQLString
// },
// images: GraphqlObjectType('UserImages', {
//   profile: {
//  	type: GraphQLString 
// 	 },
//   cover: {
//    type: GraphQLString
//   }
// })}
//}

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
// UserType.js
import GT from './graphql-thinky';
import {GraphQLInt} from 'graphql;

const userTypeDef = GT.modelToGraphQLDefinition('user', {
	exclude: ['name'],
  only: null,
  globalId: false,
  map: {
		email: 'EMAIL' // or function (email) => emial.toLowerCase()
	}
});

console.log(userTypeDef);

//Object {
// surname: {
//  type: GraphQLString 
// },
// EMAIL: { // map
//  type: GraphQLString
// },
// images: GraphqlObjectType('UserImages', {
//   profile: {
//  	type: GraphQLString 
// 	 },
//   cover: {
//    type: GraphQLString
//   }
// })}
//}

GT.createModelType(model, opts = {})

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.

import GT from './graphql-thinky';
import {GraphQLInt} from 'graphql;

const userTypeDef = GT.createModelType('user', {
	exclude: ['name'],
  only: null,
  globalId: false,
  fields: () => ({ // additional fields
  	age: {
      type: GraphQLInt
    }
  }),
  map: {
		email: 'EMAIL' // of function (email) => emial.toLowerCase()
	}
});

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