Skip to content

Commit

Permalink
Merge 8fdbdc9 into 8a252c7
Browse files Browse the repository at this point in the history
  • Loading branch information
robak86 committed Dec 17, 2017
2 parents 8a252c7 + 8fdbdc9 commit 2581bab
Show file tree
Hide file tree
Showing 40 changed files with 592 additions and 637 deletions.
2 changes: 2 additions & 0 deletions ROADMAP.md
Expand Up @@ -29,6 +29,8 @@ type User {
}
```

### Reorganize, rewrite specs

### Infer basic types from ts metadata
- investigate if it makes sense, because this feature would be very limited - only for couple of types.

Expand Down
3 changes: 1 addition & 2 deletions lib/decorators/fields.ts
@@ -1,7 +1,6 @@
import {GraphQLFieldResolver, GraphQLID} from "graphql";
import {FieldType} from "../types-conversion/TypeProxy";
import {ArgsType} from "../types-conversion/ArgumentsTypeProxy";
import {createFieldDecorator} from "./helpers";
import {ArgsType, FieldType} from "../fields-metadata/FieldConfig";

export const id = ():PropertyDecorator => createFieldDecorator(fieldConfig => {
fieldConfig.setType(GraphQLID)
Expand Down
16 changes: 4 additions & 12 deletions lib/factories/createSchema.ts
@@ -1,19 +1,11 @@
import {GraphQLSchema} from "graphql";
import {someOrThrow} from "../utils/core";
import {TypeMetadata} from "../types-metadata/TypeMetadata";
import {GraphQLObjectType, GraphQLSchema} from "graphql";
import {typesResolver} from "../types-conversion/TypesResolver";

export function createSchema(annotatedRootClass, annotatedMutationClass):GraphQLSchema {
let query = someOrThrow(
TypeMetadata.getForClass(annotatedRootClass),
'Class provided as query root is not decorated with @type');


let mutation = someOrThrow(
TypeMetadata.getForClass(annotatedMutationClass),
'Class provided as query root is not decorated with @type');

return new GraphQLSchema({
query: query.toGraphQLObjectType(),
mutation: mutation.toGraphQLObjectType()
query: typesResolver.toGraphQLType(annotatedRootClass) as GraphQLObjectType,
mutation: typesResolver.toGraphQLType(annotatedMutationClass) as GraphQLObjectType
})
}
6 changes: 3 additions & 3 deletions lib/factories/createUnion.ts
@@ -1,13 +1,13 @@
import {GraphQLObjectType, GraphQLUnionType, Thunk} from "graphql";
import * as _ from 'lodash';
import {TypeProxy} from "../types-conversion/TypeProxy";
import {typesResolver} from "../types-conversion/TypesResolver";


//TODO: rename to - unionType
export function createUnion(name:string, types:Thunk<Array<GraphQLObjectType | Function>>, resolveType):GraphQLUnionType {
return new GraphQLUnionType({
name,
types: _.castArray(types).map(type => TypeProxy.inferType(type)) as any, //TODO: fix types
resolveType: (value, context) => TypeProxy.inferType(resolveType(value, context)) as any //TODO: fix types
types: _.castArray(types).map(type => typesResolver.toGraphQLType(type)) as any, //TODO: fix types
resolveType: (value, context) => typesResolver.toGraphQLType(resolveType(value, context)) as any //TODO: fix types
});
}
40 changes: 26 additions & 14 deletions lib/fields-metadata/FieldConfig.ts
@@ -1,18 +1,28 @@
import {FieldType, TypeProxy} from "../types-conversion/TypeProxy";
import {TypeWrapper} from "../types-conversion/TypeWrapper";
import {TypeValue} from "./TypeValue";
import {TypeWrapper} from "./TypeWrapper";
import {
GraphQLArgumentConfig, GraphQLFieldConfig, GraphQLFieldResolver, GraphQLInputFieldConfig,
GraphQLInputType, GraphQLOutputType
GraphQLArgumentConfig, GraphQLFieldConfig, GraphQLFieldConfigArgumentMap, GraphQLFieldResolver,
GraphQLInputFieldConfig,
GraphQLInputType, GraphQLOutputType, GraphQLType
} from "graphql";
import {ArgsType, ArgumentsTypeProxy} from "../types-conversion/ArgumentsTypeProxy";
import {ITypeResolver} from "../types-conversion/abstract/ITypeResolver";
import {ClassType} from "../utils/types";

export type FieldType =
GraphQLType |
ClassType<any> |
Object;

export type ArgsType =
GraphQLFieldConfigArgumentMap |
ClassType<any>

//TODO: throw errors from setters for wrong data
export class FieldConfig {
private type = new TypeProxy();
private type = new TypeValue<FieldType>();
private args:TypeValue<ArgsType>;
private compositeType:TypeWrapper = new TypeWrapper();
private description:string;
private args:ArgumentsTypeProxy;
private resolveFn:GraphQLFieldResolver<any, any>;
private defaultValue;

Expand All @@ -21,12 +31,12 @@ export class FieldConfig {
}

setParamsType(paramsFieldType:ArgsType) {
this.args = new ArgumentsTypeProxy();
this.args = new TypeValue();
this.args.setType(paramsFieldType);
}

setParamsThunk(paramsFieldTypeThunk:() => ArgsType) {
this.args = new ArgumentsTypeProxy();
this.args = new TypeValue();
this.args.setTypeThunk(paramsFieldTypeThunk);
}

Expand Down Expand Up @@ -64,8 +74,9 @@ export class FieldConfig {
this.description = name;
}

toGraphQLInputFieldConfig():GraphQLArgumentConfig | GraphQLInputFieldConfig {
let type = this.type.toGraphQLType() as GraphQLInputType;
//TODO: extract this to type resolver
toGraphQLInputFieldConfig(typeResolver:ITypeResolver):GraphQLArgumentConfig | GraphQLInputFieldConfig {
let type = typeResolver.toGraphQLType(this.type.inferType()) as GraphQLInputType;
type = this.compositeType.wrap(type) as GraphQLInputType;

return {
Expand All @@ -75,13 +86,14 @@ export class FieldConfig {
}
}

toGraphQLFieldConfig():GraphQLFieldConfig<any, any> {
let type = this.type.toGraphQLType();
//TODO: extract this to type resolver
toGraphQLFieldConfig(typeResolver:ITypeResolver, argsResolver:ITypeResolver):GraphQLFieldConfig<any, any> {
let type = typeResolver.toGraphQLType(this.type.inferType());
type = this.compositeType.wrap(type) as GraphQLOutputType;

return {
type,
args: this.args && this.args.toGraphQLType() as any,
args: this.args && argsResolver.toGraphQLType(this.args.inferType()) as any,
resolve: this.resolveFn,
description: this.description
}
Expand Down
15 changes: 0 additions & 15 deletions lib/fields-metadata/FieldsMetadata.ts
@@ -1,9 +1,6 @@
import 'reflect-metadata';
import {metadataGet, metadataGetOrSet} from "../utils/metadataFactories";
import {FieldConfig} from "./FieldConfig";
import {GraphQLFieldConfigMap, GraphQLInputFieldConfigMap} from "graphql";
import {propagateErrorWithContext} from "../utils/core";
import * as _ from "lodash";

const FIELDS_METADATA_KEY = '__FIELDS_METADATA_KEY';

Expand All @@ -21,16 +18,4 @@ export class FieldsMetadata {
getFields():{ [fieldName:string]:FieldConfig } {
return this._fields;
}

toGraphQLFieldConfigMap():GraphQLFieldConfigMap<any, any> {
return _.mapValues(this.getFields(), (fieldConfig, fieldName) => {
return propagateErrorWithContext(fieldName, () => fieldConfig.toGraphQLFieldConfig())
});
}

toGraphQLInputFieldConfigMap():GraphQLInputFieldConfigMap{
return _.mapValues(this.getFields(), (fieldConfig, fieldName) => {
return propagateErrorWithContext(`fieldName: ${fieldName}`, () => fieldConfig.toGraphQLInputFieldConfig())
});
}
}
19 changes: 19 additions & 0 deletions lib/fields-metadata/TypeValue.ts
@@ -0,0 +1,19 @@
import {Thunk} from "graphql";
import {resolveThunk} from "../utils/graphql";

export class TypeValue<T> {
private _type:T;
private _typeThunk:Thunk<T>;

setType(type:T) {
this._type = type;
}

setTypeThunk(typeThunk:Thunk<T>) {
this._typeThunk = typeThunk;
}

inferType() {
return this._type || resolveThunk(this._typeThunk);
}
}
File renamed without changes.
36 changes: 0 additions & 36 deletions lib/types-conversion/ArgumentsTypeProxy.ts

This file was deleted.

24 changes: 24 additions & 0 deletions lib/types-conversion/ArgumentsTypeResolver.ts
@@ -0,0 +1,24 @@
import {invariant} from "../utils/core";
import * as _ from "lodash";
import {ITypeResolver} from "./abstract/ITypeResolver";
import {ParamsMetadata} from "../types-metadata/ParamsMetadata";
import {ArgumentMapResolver} from "./input/ArgumentMapResolver";

export class ArgumentsTypeResolver implements ITypeResolver {
private argumentsMapBuilder:ArgumentMapResolver;

constructor(protected typesResolver:ITypeResolver) {
this.argumentsMapBuilder = new ArgumentMapResolver(this.typesResolver, this);
}

toGraphQLType(type):any {
if (_.isFunction(type)) {
let metadata:any = ParamsMetadata.getForClass(type);
invariant(!!metadata, `Missing ParamsMetadata for ${type}. Decorate class using @params decorator`);

return this.argumentsMapBuilder.toGraphQLType(metadata);
}

throw new Error(`Cannot infer type for ${JSON.stringify(type)}`)
}
}
77 changes: 0 additions & 77 deletions lib/types-conversion/TypeProxy.ts

This file was deleted.

0 comments on commit 2581bab

Please sign in to comment.