Skip to content

Commit

Permalink
fix: small changes for supporting graphql@0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Feb 1, 2018
1 parent e851a65 commit b973df1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
18 changes: 12 additions & 6 deletions src/__tests__/github_issues/107-test.js
Expand Up @@ -47,7 +47,8 @@ beforeEach(() => {

describe('github issue #107 merge Schema types on GQL', () => {
it('get QueryTC from remote schema', () => {
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = TypeComposer.create(RemoteQueryType);
expect(RemoteQueryTC).toBeInstanceOf(TypeComposer);
expect(RemoteQueryTC.getTypeName()).toBe('Query');
expect(RemoteQueryTC.getFieldNames()).toEqual(['users']);
Expand All @@ -57,7 +58,8 @@ describe('github issue #107 merge Schema types on GQL', () => {
});

it('get nested TC from remote schema', () => {
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = TypeComposer.create(RemoteQueryType);
const RemoteUserTC = RemoteQueryTC.get('users');
expect(RemoteUserTC.getTypeName()).toEqual('User');

Expand All @@ -66,7 +68,8 @@ describe('github issue #107 merge Schema types on GQL', () => {
});

it('schema stiching on Query', async () => {
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = TypeComposer.create(RemoteQueryType);

GQC.rootQuery().addFields({
tag: {
Expand Down Expand Up @@ -100,7 +103,8 @@ describe('github issue #107 merge Schema types on GQL', () => {
});

it('schema stiching on Query.remote', async () => {
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = TypeComposer.create(RemoteQueryType);

GQC.rootQuery().addFields({
tag: {
Expand Down Expand Up @@ -145,7 +149,8 @@ describe('github issue #107 merge Schema types on GQL', () => {
});

it('using remote type in local schema', async () => {
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = TypeComposer.create(RemoteQueryType);
const RemoteUserTC = RemoteQueryTC.getFieldTC('users');
const remoteUsersFC = RemoteQueryTC.getField('users');

Expand Down Expand Up @@ -204,7 +209,8 @@ describe('github issue #107 merge Schema types on GQL', () => {
});

it('adding remote type to GQC and check reference by name', () => {
const RemoteQueryTC = TypeComposer.create(remoteSchema._queryType);
const RemoteQueryType: any = remoteSchema._queryType;
const RemoteQueryTC = TypeComposer.create(RemoteQueryType);
const UserTC = RemoteQueryTC.getFieldTC('users');
GQC.add(UserTC);

Expand Down
4 changes: 0 additions & 4 deletions src/__tests__/typeMapper-test.js
Expand Up @@ -48,11 +48,8 @@ describe('TypeMapper', () => {
const type: GraphQLObjectType = (typeMapper.createType(
`
type IntRange {
# Max value
max: Int,
# Min value
min: Int!
# Array of Strings
arr: [String]
}
`
Expand All @@ -64,7 +61,6 @@ describe('TypeMapper', () => {
const IntRangeTC = new TypeComposer(type);
expect(IntRangeTC.getTypeName()).toBe('IntRange');
expect(IntRangeTC.getFieldNames()).toEqual(expect.arrayContaining(['max', 'min', 'arr']));
expect(IntRangeTC.getField('max').description).toBe('Max value');
expect(IntRangeTC.getField('max').type).toBe(GraphQLInt);
expect(IntRangeTC.getField('min').type).toBeInstanceOf(GraphQLNonNull);
expect(IntRangeTC.getField('arr').type).toBeInstanceOf(GraphQLList);
Expand Down
73 changes: 40 additions & 33 deletions src/typeMapper.js
Expand Up @@ -3,20 +3,7 @@

import objectPath from 'object-path';
import { parse, parseType } from 'graphql/language/parser';
import {
LIST_TYPE,
NON_NULL_TYPE,
DOCUMENT,
SCHEMA_DEFINITION,
SCALAR_TYPE_DEFINITION,
OBJECT_TYPE_DEFINITION,
INTERFACE_TYPE_DEFINITION,
ENUM_TYPE_DEFINITION,
UNION_TYPE_DEFINITION,
INPUT_OBJECT_TYPE_DEFINITION,
DIRECTIVE_DEFINITION,
NAMED_TYPE,
} from 'graphql/language/kinds';
import { Kind } from 'graphql/language';

import { getDescription } from 'graphql/utilities/buildASTSchema';
import keyValMap from 'graphql/jsutils/keyValMap';
Expand Down Expand Up @@ -254,15 +241,19 @@ class TypeMapper {

if (composeType instanceof InputTypeComposer) {
throw new Error(
`You cannot provide InputTypeComposer to the field '${typeName}.${fieldName}'. It should be OutputType.`
`You cannot provide InputTypeComposer to the field '${typeName}.${
fieldName
}'. It should be OutputType.`
);
}

const fieldConfig: GraphQLFieldConfig<TSource, TContext> = ({}: any);
if (typeof composeType === 'string') {
if (RegexpInputTypeDefinition.test(composeType)) {
throw new Error(
`${typeName}.${fieldName} should be OutputType, but got input type definition '${composeType}'`
`${typeName}.${fieldName} should be OutputType, but got input type definition '${
composeType
}'`
);
}

Expand All @@ -276,7 +267,9 @@ class TypeMapper {

if (!type) {
throw new Error(
`${typeName}.${fieldName} cannot convert to OutputType the following string: '${composeType}'`
`${typeName}.${fieldName} cannot convert to OutputType the following string: '${
composeType
}'`
);
}
fieldConfig.type = (type: any);
Expand Down Expand Up @@ -372,7 +365,9 @@ class TypeMapper {
while (Array.isArray(composeType)) {
if (composeType.length !== 1) {
throw new Error(
`${typeName}.${fieldName}@${argName} can accept Array exact with one input type definition`
`${typeName}.${fieldName}@${
argName
} can accept Array exact with one input type definition`
);
}
wrapWithList += 1;
Expand All @@ -381,15 +376,19 @@ class TypeMapper {

if (composeType instanceof TypeComposer) {
throw new Error(
`You cannot provide TypeComposer to the arg '${typeName}.${fieldName}.@${argName}'. It should be InputType.`
`You cannot provide TypeComposer to the arg '${typeName}.${fieldName}.@${
argName
}'. It should be InputType.`
);
}

const argConfig: GraphQLArgumentConfig = ({}: any);
if (typeof composeType === 'string') {
if (RegexpOutputTypeDefinition.test(composeType)) {
throw new Error(
`${typeName}.${fieldName}@${argName} should be InputType, but got output type definition '${composeType}'`
`${typeName}.${fieldName}@${
argName
} should be InputType, but got output type definition '${composeType}'`
);
}

Expand All @@ -403,7 +402,9 @@ class TypeMapper {

if (!type) {
throw new Error(
`${typeName}.${fieldName}@${argName} cannot convert to InputType the following string: '${composeType}'`
`${typeName}.${fieldName}@${
argName
} cannot convert to InputType the following string: '${composeType}'`
);
}

Expand Down Expand Up @@ -507,15 +508,19 @@ class TypeMapper {

if (composeType instanceof TypeComposer) {
throw new Error(
`You cannot provide TypeComposer to the field '${typeName}.${fieldName}'. It should be InputType.`
`You cannot provide TypeComposer to the field '${typeName}.${
fieldName
}'. It should be InputType.`
);
}

const fieldConfig: GraphQLInputFieldConfig = ({}: any);
if (typeof composeType === 'string') {
if (RegexpOutputTypeDefinition.test(composeType)) {
throw new Error(
`${typeName}.${fieldName} should be InputType, but got output type definition '${composeType}'`
`${typeName}.${fieldName} should be InputType, but got output type definition '${
composeType
}'`
);
}

Expand All @@ -529,7 +534,9 @@ class TypeMapper {

if (!type) {
throw new Error(
`${typeName}.${fieldName} cannot convert to InputType the following string: '${composeType}'`
`${typeName}.${fieldName} cannot convert to InputType the following string: '${
composeType
}'`
);
}

Expand Down Expand Up @@ -598,15 +605,15 @@ function parseTypes(astDocument: DocumentNode): Array<GraphQLNamedType> {

function typeFromAST(inputTypeAST: TypeNode): ?GraphQLType {
let innerType;
if (inputTypeAST.kind === LIST_TYPE) {
if (inputTypeAST.kind === Kind.LIST_TYPE) {
innerType = typeFromAST(inputTypeAST.type);
return innerType && new GraphQLList(innerType);
}
if (inputTypeAST.kind === NON_NULL_TYPE) {
if (inputTypeAST.kind === Kind.NON_NULL_TYPE) {
innerType = typeFromAST(inputTypeAST.type);
return innerType && new GraphQLNonNull(((innerType: any): GraphQLNullableType));
}
invariant(inputTypeAST.kind === NAMED_TYPE, 'Must be a named type.');
invariant(inputTypeAST.kind === Kind.NAMED_TYPE, 'Must be a named type.');
return typeMapper.get(inputTypeAST.name.value);
}

Expand All @@ -624,17 +631,17 @@ function makeSchemaDef(def) {
}

switch (def.kind) {
case OBJECT_TYPE_DEFINITION:
case Kind.OBJECT_TYPE_DEFINITION:
return makeTypeDef(def);
// case INTERFACE_TYPE_DEFINITION:
// return makeInterfaceDef(def);
case ENUM_TYPE_DEFINITION:
case Kind.ENUM_TYPE_DEFINITION:
return makeEnumDef(def);
// case UNION_TYPE_DEFINITION:
// return makeUnionDef(def);
// case SCALAR_TYPE_DEFINITION:
// return makeScalarDef(def);
case INPUT_OBJECT_TYPE_DEFINITION:
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
return makeInputObjectDef(def);
default:
throw new Error(`Type kind "${def.kind}" not supported.`);
Expand Down Expand Up @@ -696,17 +703,17 @@ function makeInputObjectDef(def: InputObjectTypeDefinitionNode) {

function getNamedTypeAST(typeAST: TypeNode): NamedTypeNode {
let namedType = typeAST;
while (namedType.kind === LIST_TYPE || namedType.kind === NON_NULL_TYPE) {
while (namedType.kind === Kind.LIST_TYPE || namedType.kind === Kind.NON_NULL_TYPE) {
namedType = namedType.type;
}
return namedType;
}

function buildWrappedType(innerType: GraphQLType, inputTypeAST: TypeNode): GraphQLType {
if (inputTypeAST.kind === LIST_TYPE) {
if (inputTypeAST.kind === Kind.LIST_TYPE) {
return new GraphQLList(buildWrappedType(innerType, inputTypeAST.type));
}
if (inputTypeAST.kind === NON_NULL_TYPE) {
if (inputTypeAST.kind === Kind.NON_NULL_TYPE) {
const wrappedType = buildWrappedType(innerType, inputTypeAST.type);
invariant(!(wrappedType instanceof GraphQLNonNull), 'No nesting nonnull.');
return new GraphQLNonNull(wrappedType);
Expand Down

0 comments on commit b973df1

Please sign in to comment.