Skip to content

Commit

Permalink
Initial implementation / documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
aldahick committed Oct 2, 2018
0 parents commit bbc5aac
Show file tree
Hide file tree
Showing 19 changed files with 794 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .atomignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.js
*.js.map
*.d.ts
10 changes: 10 additions & 0 deletions .editorconfig
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.js
*.js.map
*.d.ts
*.log
/node_modules
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.ts
!*.js
7 changes: 7 additions & 0 deletions LICENSE.md
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.
48 changes: 48 additions & 0 deletions README.md
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)
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./lib";
27 changes: 27 additions & 0 deletions lib/EntityManager.ts
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();
}
}
7 changes: 7 additions & 0 deletions lib/decorators/entity.ts
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);
};
2 changes: 2 additions & 0 deletions lib/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./entity";
export * from "./property";
17 changes: 17 additions & 0 deletions lib/decorators/property.ts
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);
};
5 changes: 5 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import "reflect-metadata";

export * from "./EntityManager";

export * from "./decorators";
18 changes: 18 additions & 0 deletions lib/util/graphql.ts
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;
}
1 change: 1 addition & 0 deletions lib/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./graphql";
Loading

0 comments on commit bbc5aac

Please sign in to comment.