-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation / documentation
- Loading branch information
0 parents
commit bbc5aac
Showing
19 changed files
with
794 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.js | ||
*.js.map | ||
*.d.ts |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*.{cson,json,yml}] | ||
indent_size = 2 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
*.js | ||
*.js.map | ||
*.d.ts | ||
*.log | ||
/node_modules |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.ts | ||
!*.js |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright 2018 Alex Hicks | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# barnacle | ||
|
||
Barnacle is a very simple GraphQL helper for Typescript. It exposes several | ||
decorators which allow you to automatically build GraphQL type definitions from | ||
Typescript classes. | ||
|
||
## Usage | ||
|
||
```typescript | ||
import * as barnacle from "barnaclejs"; | ||
|
||
const manager = new barnacle.EntityManager(); | ||
|
||
@barnacle.entity() | ||
class User { | ||
// nullable: false by default | ||
@barnacle.property() | ||
id!: number; | ||
|
||
@barnacle.property({ nullable: true }) | ||
lastIP?: string; | ||
|
||
// manually override type inference (because Typescript doesn't automagically emit all types :( ) | ||
@barnacle.property({ type: "[String]" }) | ||
phoneNumbers!: string[]; | ||
|
||
// not a GraphQL property, no decorator | ||
getName() { | ||
return "Bob"; | ||
} | ||
} | ||
|
||
console.log(manager.toString(User)); /* should output: | ||
type User { | ||
id: Int! | ||
lastIP: String | ||
phoneNumbers: [String]! | ||
} | ||
*/ | ||
``` | ||
|
||
## To-do | ||
|
||
- Implement proper unit tests | ||
- Remove manual instantiation of EntityManager (allow still, but use a singleton instance if not specified) | ||
- Better type inference (if possible?) | ||
- Support methods | ||
- Expand scope to handling requests? (generating resolvers, etc) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./lib"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Entity { | ||
constructor(readonly target: Function) { | ||
} | ||
|
||
toString(): string { | ||
const propertyNames: string[] = Reflect.getMetadata("barnacle:propertyNames", this.target.prototype) || []; | ||
return `type ${this.target.name} { | ||
${propertyNames.map(property => `${property}: ${Reflect.getMetadata("barnacle:type", this.target.prototype, property)}`).join("\n\t")} | ||
}`.replace(/[ ]{4}/g, "\t"); | ||
} | ||
} | ||
|
||
export class EntityManager { | ||
readonly entities: Entity[] = []; | ||
constructor() { } | ||
|
||
addEntity(target: Function) { | ||
this.entities.push(new Entity(target)); | ||
} | ||
|
||
toString(target?: Function): string { | ||
if (!target) return ""; | ||
const entity = this.entities.find(e => e.target === target); | ||
if (!entity) throw new Error(`Entity ${target.name} was never registered!`); | ||
return entity.toString(); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { EntityManager } from "../EntityManager"; | ||
|
||
export const entity = (manager: EntityManager): ClassDecorator => target => { | ||
console.log("entity()"); | ||
Reflect.defineMetadata("barnacle:manager", manager, target); | ||
manager.addEntity(target); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./entity"; | ||
export * from "./property"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getFullGraphQLType } from "../util"; | ||
|
||
type PropertyOptions = { | ||
type?: Function | string; | ||
nullable?: boolean; | ||
}; | ||
|
||
export const property = (options?: PropertyOptions) => <T, K extends keyof T & string>(target: T, key: K) => { | ||
options = options || {}; | ||
options.nullable = options.nullable === undefined ? false : options.nullable; | ||
options.type = options.type || Reflect.getMetadata("design:type", target, key) as Function; | ||
const type = getFullGraphQLType(options.type, options.nullable); | ||
Reflect.defineMetadata("barnacle:type", type, target, key); | ||
Reflect.defineMetadata("barnacle:propertyNames", (Reflect.hasMetadata("barnacle:propertyNames", target) | ||
? Reflect.getMetadata("barnacle:propertyNames", target) as string[] | ||
: []).concat([key]), target); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import "reflect-metadata"; | ||
|
||
export * from "./EntityManager"; | ||
|
||
export * from "./decorators"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function getGraphQLType(type: Function): string { | ||
switch (type.name) { | ||
case "Number": | ||
return "Int"; | ||
case "Array": | ||
case "Object": | ||
case "Symbol": | ||
throw new Error("Can't infer type from " + type.name); | ||
} | ||
// includes Boolean and String | ||
return type.name; | ||
} | ||
|
||
export function getFullGraphQLType(type: Function | string, nullable: boolean): string { | ||
let rawType = typeof(type) === "string" ? type : getGraphQLType(type); | ||
if (!nullable) rawType += "!"; | ||
return rawType; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./graphql"; |
Oops, something went wrong.