bicycle
A data synchronisation library for JavaScript
Installation
npm install bicycle
Usage
Client
; const client = ; const subscription = client; // to dispose of the subscription:subscription; // Use `update` to trigger mutations on the server. Any relevant subscriptions are updated automaticallyclient;
Queries can also take parameters and have aliases, e.g.
const subscription = client;
Server
;; const app = ; // other routes etc. here // define the schema.// in a real app you'd want to split schema definition across multiple filesconst schema = objects: name: 'Root' fields: todoById: type: 'Todo' args: id: 'string' { return ; } todos: type: 'Todo[]' { return ; } name: 'Todo' fields: id: 'id' title: 'string' completed: 'boolean' mutations: addTodo: args: id: 'id' title: 'string' completed: 'boolean' { return ; } toggleAll: args: checked: 'boolean' { return ; } toggle: args: id: 'id' checked: 'boolean' { return ; } destroy: args: id: 'id' { return ; } save: args: id: 'id' title: 'string' { return ; } clearCompleted: { return ; } ;; const bicycle = schema; // createMiddleware takes a function that returns the context given a request// this allows you to only expose information the user is allowed to seeapp; app;
schema
Your schema consists of a collection of type definitions. Type definitions can be:
- objects (a collection of fields, with an ID)
- scalars (there are built in values for
'string'
,'number'
and'boolean'
, but you may wish to add your own) - enums (these take a value from a predetermined set)
Root Object
You must always define an ObjectType called 'Root'
. This type is a singleton and is the entry point for all queries.
e.g.
name: 'Root' fields: todoById: type: 'Todo' args: id: 'string' { return ; } todos: type: 'Todo[]' { return ; } ;
Object types
Object types have the following properties:
- id (
Function
) - A function that takes an object of this type and returns a globally unique id, defaults toobj => TypeName + obj.id
- name (
string
, required) - The name of your Object Type - description (
string
) - An optional string that may be useful for generating automated documentation - fields (
Map<string, Field>
) - An object mapping field names onto field definitions. - mutations (
Map<string, Mutation>
) - An object mapping field names onto mutation definitions.
Fields can have:
- type (
typeString
, required) - The type of the field - args (
Map<string, typeString>
) - The type of any arguments the field takes - description (
string
) - An optional string that may be useful for generating automated documentation - resolve (
Function
) - A function that takes the object, the args (that have been type checked) and the context and returns the value of the field. Defaults toobj => obj.fieldName
License
MIT