Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make it independent from graphql-tools
- Loading branch information
1 parent
f20a46b
commit 6d021cc
Showing
103 changed files
with
5,855 additions
and
417 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
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
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
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
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
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
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,87 @@ | ||
import { getNamedType, GraphQLSchema, isObjectType, isInputObjectType, GraphQLField, GraphQLInputType } from 'graphql'; | ||
|
||
export type IndexedObject<V> = { [key: string]: V } | ReadonlyArray<V>; | ||
export type IFieldIteratorFn = (fieldDef: GraphQLField<any, any>, typeName: string, fieldName: string) => void; | ||
export type IDefaultValueIteratorFn = (type: GraphQLInputType, value: any) => void; | ||
|
||
export function forEachField(schema: GraphQLSchema, fn: IFieldIteratorFn): void { | ||
const typeMap = schema.getTypeMap(); | ||
Object.keys(typeMap).forEach((typeName) => { | ||
const type = typeMap[typeName]; | ||
|
||
// TODO: maybe have an option to include these? | ||
if (!getNamedType(type).name.startsWith('__') && isObjectType(type)) { | ||
const fields = type.getFields(); | ||
Object.keys(fields).forEach((fieldName) => { | ||
const field = fields[fieldName]; | ||
fn(field, typeName, fieldName); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
export function forEachDefaultValue(schema: GraphQLSchema, fn: IDefaultValueIteratorFn): void { | ||
const typeMap = schema.getTypeMap(); | ||
Object.keys(typeMap).forEach((typeName) => { | ||
const type = typeMap[typeName]; | ||
|
||
if (!getNamedType(type).name.startsWith('__')) { | ||
if (isObjectType(type)) { | ||
const fields = type.getFields(); | ||
Object.keys(fields).forEach((fieldName) => { | ||
const field = fields[fieldName]; | ||
|
||
field.args.forEach((arg) => { | ||
arg.defaultValue = fn(arg.type, arg.defaultValue); | ||
}); | ||
}); | ||
} else if (isInputObjectType(type)) { | ||
const fields = type.getFields(); | ||
Object.keys(fields).forEach((fieldName) => { | ||
const field = fields[fieldName]; | ||
field.defaultValue = fn(field.type, field.defaultValue); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
// A more powerful version of each that has the ability to replace or remove | ||
// array or object keys. | ||
export function updateEachKey<V>( | ||
arrayOrObject: IndexedObject<V>, | ||
// The callback can return nothing to leave the key untouched, null to remove | ||
// the key from the array or object, or a non-null V to replace the value. | ||
updater: (value: V, key: string) => void | null | V | ||
) { | ||
let deletedCount = 0; | ||
|
||
Object.keys(arrayOrObject).forEach((key) => { | ||
const result = updater(arrayOrObject[key], key); | ||
|
||
if (typeof result === 'undefined') { | ||
return; | ||
} | ||
|
||
if (result === null) { | ||
delete arrayOrObject[key]; | ||
deletedCount++; | ||
return; | ||
} | ||
|
||
arrayOrObject[key] = result; | ||
}); | ||
|
||
if (deletedCount > 0 && Array.isArray(arrayOrObject)) { | ||
// Remove any holes from the array due to deleted elements. | ||
arrayOrObject.splice(0).forEach((elem) => { | ||
arrayOrObject.push(elem); | ||
}); | ||
} | ||
} | ||
|
||
export function each<V>(arrayOrObject: IndexedObject<V>, callback: (value: V, key: string) => void) { | ||
Object.keys(arrayOrObject).forEach((key) => { | ||
callback(arrayOrObject[key], key); | ||
}); | ||
} |
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,25 @@ | ||
export function mergeDeep(target: any, ...sources: any): any { | ||
const output = { | ||
...target, | ||
}; | ||
sources.forEach((source: any) => { | ||
if (isObject(target) && isObject(source)) { | ||
Object.keys(source).forEach((key) => { | ||
if (isObject(source[key])) { | ||
if (!(key in target)) { | ||
Object.assign(output, { [key]: source[key] }); | ||
} else { | ||
output[key] = mergeDeep(target[key], source[key]); | ||
} | ||
} else { | ||
Object.assign(output, { [key]: source[key] }); | ||
} | ||
}); | ||
} | ||
}); | ||
return output; | ||
} | ||
|
||
function isObject(item: any): boolean { | ||
return item && typeof item === 'object' && !Array.isArray(item); | ||
} |
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
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,11 @@ | ||
// @schemaDefinition: A GraphQL type schema in shorthand | ||
// @resolvers: Definitions for resolvers to be merged with schema | ||
export class SchemaError extends Error { | ||
public message: string; | ||
|
||
constructor(message: string) { | ||
super(message); | ||
this.message = message; | ||
Error.captureStackTrace(this, this.constructor); | ||
} | ||
} |
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,64 @@ | ||
import { | ||
GraphQLObjectType, | ||
GraphQLInterfaceType, | ||
GraphQLInputObjectType, | ||
GraphQLString, | ||
GraphQLNamedType, | ||
GraphQLInt, | ||
GraphQLFloat, | ||
GraphQLBoolean, | ||
GraphQLID, | ||
isObjectType, | ||
isInterfaceType, | ||
isInputObjectType, | ||
} from 'graphql'; | ||
|
||
export function createNamedStub( | ||
name: string, | ||
type: 'object' | 'interface' | 'input' | ||
): GraphQLObjectType | GraphQLInputObjectType | GraphQLInterfaceType { | ||
let constructor: any; | ||
if (type === 'object') { | ||
constructor = GraphQLObjectType; | ||
} else if (type === 'interface') { | ||
constructor = GraphQLInterfaceType; | ||
} else { | ||
constructor = GraphQLInputObjectType; | ||
} | ||
|
||
return new constructor({ | ||
name, | ||
fields: { | ||
__fake: { | ||
type: GraphQLString, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
export function isStub(type: GraphQLNamedType): boolean { | ||
if (isObjectType(type) || isInterfaceType(type) || isInputObjectType(type)) { | ||
const fields = type.getFields(); | ||
const fieldNames = Object.keys(fields); | ||
return fieldNames.length === 1 && fields[fieldNames[0]].name === '__fake'; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export function getBuiltInForStub(type: GraphQLNamedType): GraphQLNamedType { | ||
switch (type.name) { | ||
case GraphQLInt.name: | ||
return GraphQLInt; | ||
case GraphQLFloat.name: | ||
return GraphQLFloat; | ||
case GraphQLString.name: | ||
return GraphQLString; | ||
case GraphQLBoolean.name: | ||
return GraphQLBoolean; | ||
case GraphQLID.name: | ||
return GraphQLID; | ||
default: | ||
return type; | ||
} | ||
} |
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,46 @@ | ||
import { | ||
GraphQLEnumType, | ||
GraphQLInputType, | ||
GraphQLScalarType, | ||
getNullableType, | ||
isLeafType, | ||
isListType, | ||
isInputObjectType, | ||
} from 'graphql'; | ||
|
||
type InputValueTransformer = (type: GraphQLEnumType | GraphQLScalarType, originalValue: any) => any; | ||
|
||
export function transformInputValue(type: GraphQLInputType, value: any, transformer: InputValueTransformer) { | ||
if (value == null) { | ||
return value; | ||
} | ||
|
||
const nullableType = getNullableType(type); | ||
|
||
if (isLeafType(nullableType)) { | ||
return transformer(nullableType, value); | ||
} else if (isListType(nullableType)) { | ||
return value.map((listMember: any) => transformInputValue(nullableType.ofType, listMember, transformer)); | ||
} else if (isInputObjectType(nullableType)) { | ||
const fields = nullableType.getFields(); | ||
const newValue = {}; | ||
Object.keys(value).forEach((key) => { | ||
newValue[key] = transformInputValue(fields[key].type, value[key], transformer); | ||
}); | ||
return newValue; | ||
} | ||
|
||
// unreachable, no other possible return value | ||
} | ||
|
||
export function serializeInputValue(type: GraphQLInputType, value: any) { | ||
return transformInputValue(type, value, (t, v) => t.serialize(v)); | ||
} | ||
|
||
export function parseInputValue(type: GraphQLInputType, value: any) { | ||
return transformInputValue(type, value, (t, v) => t.parseValue(v)); | ||
} | ||
|
||
export function parseInputValueLiteral(type: GraphQLInputType, value: any) { | ||
return transformInputValue(type, value, (t, v) => t.parseLiteral(v, {})); | ||
} |
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
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
Oops, something went wrong.