Skip to content

Commit

Permalink
Merge pull request #34 from indigotech/feature/refactor-field
Browse files Browse the repository at this point in the history
Feature - Refactoring @field decorator
  • Loading branch information
felipesabino authored Nov 7, 2017
2 parents b11e01b + 331371f commit 20a0a3c
Show file tree
Hide file tree
Showing 73 changed files with 1,341 additions and 997 deletions.
2 changes: 1 addition & 1 deletion examples/apollo-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"express": "^4.14.0",
"express-graphql": "^0.5.3",
"graphql": "0.11.7",
"graphql-decorator": "file:../../",
"graphql-schema-decorator": "file:../../",
"graphql-subscriptions": "^0.5.4",
"hapi": "^16.6.2",
"nodemon": "^1.12.1",
Expand Down
8 changes: 4 additions & 4 deletions examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
"start": "node .built/main.js"
},
"dependencies": {
"graphql": "0.9.1",
"graphql-decorator": "file:../../",
"typescript": "2.3.4"
"graphql": "0.11.7",
"graphql-schema-decorator": "file:../../"
},
"keywords": [],
"author": "Quramy",
"repository": {
"type": "git",
"url": "https://github.com/Quramy/graphql-decorator.git"
"url": "https://github.com/indigotech/graphql-schema-decorator.git"
},
"license": "ISC",
"devDependencies": {
"typescript": "2.3.4",
"@types/node": "7.0.5"
}
}
13 changes: 9 additions & 4 deletions examples/hello-world/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
/* main.ts */

import { Field, ObjectType, Query, Schema, schemaFactory } from "graphql-schema-decorator";
const graphql = require("graphql").graphql;
import { Field, ObjectType, Query, Schema, schemaFactory } from 'graphql-schema-decorator';
declare var process;

process.on('unhandledRejection', up => { throw up; });

const graphql = require('graphql').graphql;
const printSchema = require('graphql').printSchema;

// @ObjectType creates GraphQLObjectType from a class
@ObjectType()
class QueryType {
@Field() greeting(): string {
return "Hello, world!";
return 'Hello, world!';
}
}

Expand All @@ -24,7 +29,7 @@ async function main() {
const schema = schemaFactory(SchemaType);

const result = await graphql(schema, `query { greeting } `);
console.log(result.data.greeting);
console.log(result.data);
}

main();
30 changes: 30 additions & 0 deletions examples/hello-world/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@types/node@7.0.5":
version "7.0.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-7.0.5.tgz#96a0f0a618b7b606f1ec547403c00650210bfbb7"

"graphql-schema-decorator@file:../../":
version "0.8.0"
dependencies:
reflect-metadata "0.1.9"

graphql@0.11.7:
version "0.11.7"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.11.7.tgz#e5abaa9cb7b7cccb84e9f0836bf4370d268750c6"
dependencies:
iterall "1.1.3"

iterall@1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.1.3.tgz#1cbbff96204056dde6656e2ed2e2226d0e6d72c9"

reflect-metadata@0.1.9:
version "0.1.9"
resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.9.tgz#987238dc87a516895fe457f130435ffbd763a4d4"

typescript@2.3.4:
version "2.3.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.3.4.tgz#3d38321828231e434f287514959c37a82b629f42"
2 changes: 1 addition & 1 deletion examples/simple-crud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"express": "^4.14.0",
"express-graphql": "^0.5.3",
"graphql": "0.9.1",
"graphql-decorator": "file:../../",
"graphql-schema-decorator": "file:../../",
"typescript": "2.3.4"
},
"keywords": [],
Expand Down
4 changes: 2 additions & 2 deletions examples/simple-crud/src/print.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { schema } from "./schema";
const printSchema = require("graphql/utilities").printSchema;
import { schema } from './schema';
const printSchema = require('graphql/utilities').printSchema;

function main() {
console.log(printSchema(schema));
Expand Down
58 changes: 23 additions & 35 deletions examples/simple-crud/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,91 +1,79 @@
import {
Arg,
Description,
Field,
InputObjectType,
List,
Mutation,
NonNull,
ObjectType,
Query,
Schema,
schemaFactory,
} from "graphql-schema-decorator";
} from 'graphql-schema-decorator';

import { createHash } from "crypto";
import {users as data} from "./data";
import { createHash } from 'crypto';
import {users as data} from './data';

const graphql = require("graphql");
const graphql = require('graphql');

let users = data.slice();

@ObjectType()
@Description("A user type.")
@ObjectType({description: 'A user type.'})
class User {
@NonNull() @Field({type: graphql.GraphQLID}) id: string;
@Field({type: graphql.GraphQLID, nonNull: true}) id: string;
@Field() name: string;
@NonNull() @Field() email: string;
@Field({nonNull: true}) email: string;
}

@ObjectType()
@Description("A root query.")
@ObjectType({description: 'A root query.'})
class QueryType {
@Description("return all users.")
@List() @Field({type: User})
@Field({description: 'return all users.', isList: true, type: User})
allUsers() {
return users;
}
}

@InputObjectType()
@Description("A input object to update a user.")
@InputObjectType({description: 'A input object to update a user.'})
class UserForUpdate {
@Field() name: string;
@Field() email: string;
}

@InputObjectType()
@Description("A input object to create a user.")
@InputObjectType({description: 'A input object to create a user.'})
class UserForCreate {
@Field() name: string;
@NonNull() @Field() email: string;
@Field({nonNull: true}) email: string;
}


@ObjectType()
@Description("Mutations.")
@ObjectType({description: 'Mutations.'})
class MutationType {

@Field({type: User})
@Description("Update a user and return the changed user.")
@Field({type: User, description: 'Update a user and return the changed user.'})
changeUser(
@NonNull() @Arg({name: "id"}) id: string,
@Arg({name: "input"}) input: UserForUpdate
@Arg({name: 'id', nonNull: true}) id: string,
@Arg({name: 'input'}) input: UserForUpdate,
) {
const user = users.find(u => u.id === id) as User;
if (!user) return null;
Object.assign(user, input);
return user;
}

@Field({type: User})
@Description("Create a user and return the created user.")
@Field({type: User, description: 'Create a user and return the created user.'})
addUser(
@NonNull() @Arg({name: "input"}) input: UserForCreate
@Arg({name: 'input', nonNull: true}) input: UserForCreate,
) {
const newUser = new User();
const shasum = createHash("sha1");
shasum.update("usr" + Date.now());
newUser.id = shasum.digest("hex");
const shasum = createHash('sha1');
shasum.update('usr' + Date.now());
newUser.id = shasum.digest('hex');
Object.assign(newUser, input);
users.push(newUser);
return newUser;
}

@Field({type: User})
@Description("Delete a user and return the removed user.")
@Field({type: User, description: 'Delete a user and return the removed user.'})
deleteUser(
@NonNull() @Arg({name: "id"}) id: string
@Arg({name: 'id', nonNull: true}) id: string,
) {
const user = users.find(u => u.id === id) as User;
if (!user) return null;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"build": "rimraf lib && tsc",
"lint": "tslint \"src/**/*.ts\"",
"test": "npm run build && npm run lint && mocha lib/*.spec.js lib/**/*.spec.js",
"test": "npm run build && npm run lint && mocha lib/**/*.spec.js",
"watch": "tsc -w",
"watch-test": "mocha lib/**/*.spec.js --watch"
},
Expand Down
33 changes: 15 additions & 18 deletions runtest.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
npm run test
#!/bin/sh

if [ "$?" -gt 0 ]; then
exit 1
fi
set -e

cd examples/hello-world/

npm install
npm start
yarn run test
./node_modules/.bin/rimraf node_modules
yarn install --production

if [ "$?" -gt 0 ]; then
exit 1
fi
# Hellow World
cd examples/hello-world/
yarn install
yarn start
cd ../../

# Simple CRUD
cd examples/simple-crud

npm install
npm run print

if [ "$?" -gt 0 ]; then
exit 1
fi
yarn install
yarn run print
cd ../../

# Restore lib environment
yarn install

exit 0
Loading

0 comments on commit 20a0a3c

Please sign in to comment.