Skip to content

Instantly share code, notes, and snippets.

@voodooattack
Created August 24, 2016 05:55
Show Gist options
  • Save voodooattack/76bc617f279caf3dd59a8ed235207c7a to your computer and use it in GitHub Desktop.
Save voodooattack/76bc617f279caf3dd59a8ed235207c7a to your computer and use it in GitHub Desktop.
import { GraphQLSchema } from 'graphql/type';
import { graphql } from 'graphql';
import { type, query, mutation, field, iface, Schema } from '../src';
class MySchema extends Schema { }
/**
* Declare a basic interface
*/
@iface(MySchema)
class Node {
@field('ID!')
id = 1;
}
/**
* Declare the type 'User', it automatically inherits everything from Node.
*/
@type(MySchema)
class User extends Node
{
@field('String!')
username = 'Abdullah';
@field('String!')
password = 'Secret!';
@field('[User]')
friends;
}
@type(MySchema)
class Query {
@query('viewer : User')
async viewer() { return new User(); }
}
@type(MySchema)
class Mutation {
@mutation('signUp(username: String!, password: String!): ID')
async signUp({ username, password}) { return 1; }
}
const schema = new MySchema();
const theSchema = new GraphQLSchema({ query: schema.getType('Query'), mutation: schema.getMutationType() });
graphql(theSchema, `query { viewer { id } }`)
.then(r => console.log(r)).catch(console.error);
graphql(theSchema, `mutation { signUp(username: "test1", password: "test2") }`)
.then(r => console.log(r)).catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment