Last active
May 2, 2017 02:11
-
-
Save jbaxleyiii/20d1e4a779d04cd2f28cdf424bbe06f9 to your computer and use it in GitHub Desktop.
Heighliner (Functional GraphQL Server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from "express"; | |
import bodyParser from "body-parser"; | |
import { graphqlExpress } from "graphql-server-express"; | |
const PORT = 3000; | |
import schema from "./schema"; | |
express() | |
.use( | |
"/graphql", | |
bodyParser.json(), | |
graphqlExpress(request => ({ | |
context: { models: { User: new User() }, // from whatever ORM you want | |
schema | |
})) | |
) | |
.listen(PORT); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
interface Node { | |
id: ID! | |
} | |
*/ | |
import { Interface, IdField, Required } from "heighliner"; | |
// id: ID! | |
export const id = Required(IdField(({ id }) => id)); | |
export default Interface("Node", { id }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
type Person { | |
user: User | |
firstName: String | |
id: ID! | |
} | |
*/ | |
import { Type, StringField, Field } from "heighliner"; | |
import User from "./user"; | |
import { id } from "./node"; | |
// firstName: String | |
export const firstName = StringField(({ firstName }) => firstName); | |
// user: User | |
export const user = Field(User, person => person.user); | |
// export the type | |
export default Type("Person", { firstName, user, id }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
type Query { | |
user(id: Id!): User | |
} | |
*/ | |
import { Query, Schema } from "heighliner"; | |
import User from "./user"; | |
export const query = Query({ | |
user: (rootValue, { id }, { models }) => models.User.get(id) | |
}); | |
export default Schema(query, /* mutation, subscription */) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
type User implements Node { | |
person: Person | |
email(format: String!): String | |
id: ID! | |
} | |
*/ | |
import { Field, Types, Required } from "heighliner"; | |
import Node, { id } from "./node"; | |
import Person from "./person"; | |
// (format: Bool!) | |
export const format = Required(Types.Bool); | |
// shareable field primitives | |
// person: Person | |
export const person = Field(Person, user => user.person); | |
// Field is curryable so you can share field types and customize resolvers on a per type basis | |
// email(format: Bool!): String | |
export const EmailField = Field(Types.String, { format }); | |
export const email = EmailField( | |
({ email }, { format }) => (format ? email.toLowerCase() : email) | |
); | |
// export the type (it inherits the fields from the interface) | |
export default Node("User", { email, person }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment