Skip to content

TouchBistro/typiql

Repository files navigation

typiql

CircleCI

typiql is a micro-helper for graphql-js that lets you refer to your GraphQL types more succinctly.

typiql is not for building entire schemas. It's only for use with the type property of your GraphQL fields. You still define GraphQLObjectType and GraphQLInputObjectTypes with normal graphql-js syntax, but when declaring your fields' types, use tql shorthand to refer to scalars, custom objects, and wrapping types.

Examples

Built in scalars

tql`String`     // => GraphQLString
tql`Int`        // => GraphQLInt

Non-null scalar

tql`ID!`        // => new GraphQLNonNull(GraphQLID)

Non-null list of non-null floats

tql`[Float!]!`  // => new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLFloat)))

List of custom object types

tql`[${BeerType}]` // => new GraphQLList(BeerType)

Detailed example

npm install --save typiql
import tql from 'typiql'
import {
  GraphQLObjectType
} from 'graphql'

const CommentType = new GraphQLObjectType({
  name: 'Comment',
  fields: () => {
    id: { type: tql`ID!` },          // => new GraphQLNonNull(GraphQLID)
    text: { type: tql`String!` },    // => new GraphQLNonNull(GraphQLString)
    post: {
      type: tql`${PostType}!`,       // => new GraphQLNonNull(PostType)
      resolve: () => { /* ... */ }
    }
  }
})

const PostType = new GraphQLObjectType({
  name: 'Post',
  fields: () => {
    id: { type: tql`ID!` },           // => new GraphQLNonNull(GraphQLID)
    title: { type: tql`String!` },    // => new GraphQLNonNull(GraphQLString)
    comments: {
      type: tql`[${CommentType}!]`,   // => new GraphQLList(new GraphQLNonNull(CommentType))
      resolve: () => { /* ... */ }
    }
  }
})

Notes

  • typiql is not intended for constructing your entire schema using the GraphQL schema IDL. For this, consider one of the other tools listed here.

  • typiql does not require/implement a custom type registry. Import your actual types, and interpolate them.

  • typiql gives you a slightly better experience than stock graphql-js, while still allowing you to build your schema programatically, as well as keep your resolvers next to your fields. (typiql does not require your types and resolvers in separate parallel structures, like some other schema building tools).