graphql-tools
Advanced tools
Comparing version 0.7.2 to 0.8.0
@@ -1,8 +0,13 @@ | ||
# Changelog | ||
### v0.8.0 | ||
### vNEXT | ||
* Update default resolve function to match the one from GraphQL.js ([@stubailo](https://github.com/stubailo) in [#183](https://github.com/apollostack/graphql-tools/pull/183)) | ||
* Move `typed-graphql` to `optionalDependencies` ([@stubailo](https://github.com/stubailo) in [#183](https://github.com/apollostack/graphql-tools/pull/183)) | ||
* Set new defaults for resolver validation to match GraphQL.js so that developers need to opt-in to advanced validation ([@stubailo](https://github.com/stubailo) in [#183](https://github.com/apollostack/graphql-tools/pull/183)): | ||
* `requireResolversForArgs = false` | ||
* `requireResolversForNonScalar = false` | ||
### v0.7.2 | ||
* Eliminated babel and moved to native ES5 compilation. ([@DxCx](https://github.com/DxCx) in [#147](https://github.com/apollostack/graphql-tools/pull/147)) | ||
* Eliminated babel and moved to native ES5 compilation. ([@DxCx](https://github.com/DxCx) in [#147](https://github.com/apollostack/graphql-tools/pull/147)) | ||
### v0.7.1 | ||
@@ -9,0 +14,0 @@ |
export * from './schemaGenerator'; | ||
export * from './mock'; | ||
export * from './autopublish'; |
@@ -7,2 +7,3 @@ "use strict"; | ||
__export(require('./mock')); | ||
__export(require('./autopublish')); | ||
//# sourceMappingURL=index.js.map |
/// <reference types="typed-graphql" /> | ||
/// <reference types="@types/node" /> | ||
import { GraphQLSchema, GraphQLFieldDefinition, GraphQLResult, GraphQLType, GraphQLFieldResolveFn, GraphQLIsTypeOfFn, GraphQLTypeResolveFn } from 'graphql'; | ||
@@ -34,3 +35,3 @@ export interface IResolverValidationOptions { | ||
typeDefs: ITypeDefinitions; | ||
resolvers: IResolvers; | ||
resolvers?: IResolvers; | ||
connectors?: IConnectors; | ||
@@ -37,0 +38,0 @@ logger?: ILogger; |
@@ -0,1 +1,2 @@ | ||
/// <reference types="@types/node" /> | ||
import { ILogger } from './Interfaces'; | ||
@@ -2,0 +3,0 @@ export declare class Logger implements ILogger { |
@@ -183,3 +183,4 @@ "use strict"; | ||
// we could return undefined, but that would be hard to debug, so we throw instead. | ||
throw new Error("No mock defined for type \"" + fieldType.name + "\""); | ||
// however, we returning it instead of throwing it, so preserveResolvers can handle the failures. | ||
return Error("No mock defined for type \"" + fieldType.name + "\""); | ||
}; | ||
@@ -227,2 +228,10 @@ }; | ||
var mockedValue = values[0], resolvedValue = values[1]; | ||
// In case we couldn't mock | ||
if (mockedValue instanceof Error) { | ||
// only if value was not resolved, populate the error. | ||
if (undefined === resolvedValue) { | ||
throw mockedValue; | ||
} | ||
return resolvedValue; | ||
} | ||
if (isObject(mockedValue) && isObject(resolvedValue)) { | ||
@@ -229,0 +238,0 @@ // Object.assign() won't do here, as we need to all properties, including |
/// <reference types="typed-graphql" /> | ||
import { GraphQLSchema, GraphQLFieldResolveFn } from 'graphql'; | ||
/// <reference types="@types/node" /> | ||
import { GraphQLSchema, GraphQLResolveInfo, GraphQLFieldResolveFn } from 'graphql'; | ||
import { IExecutableSchemaDefinition, ILogger, IResolvers, ITypeDefinitions, IFieldIteratorFn, IResolverValidationOptions } from './Interfaces'; | ||
@@ -16,3 +17,6 @@ declare class SchemaError extends Error { | ||
declare function addErrorLoggingToSchema(schema: GraphQLSchema, logger: ILogger): void; | ||
declare function chainResolvers(resolvers: GraphQLFieldResolveFn[]): (root: any, args: { | ||
[argName: string]: any; | ||
}, ctx: any, info: GraphQLResolveInfo) => any; | ||
declare function addCatchUndefinedToSchema(schema: GraphQLSchema): void; | ||
export { makeExecutableSchema, SchemaError, forEachField, addErrorLoggingToSchema, addResolveFunctionsToSchema, addCatchUndefinedToSchema, assertResolveFunctionsPresent, buildSchemaFromTypeDefinitions, addSchemaLevelResolveFunction, attachConnectorsToContext }; | ||
export { makeExecutableSchema, SchemaError, forEachField, chainResolvers, addErrorLoggingToSchema, addResolveFunctionsToSchema, addCatchUndefinedToSchema, assertResolveFunctionsPresent, buildSchemaFromTypeDefinitions, addSchemaLevelResolveFunction, attachConnectorsToContext }; |
@@ -10,2 +10,4 @@ // Generates a schema for graphql-js given a shorthand schema | ||
// and what it outputs. | ||
// TODO: we should refactor this file, rename it to makeExecutableSchema, and move | ||
// a bunch of utility functions into a separate utitlities folder, one file per function. | ||
var graphql_1 = require('graphql'); | ||
@@ -15,3 +17,3 @@ var lodash_1 = require('lodash'); | ||
var graphql_3 = require('graphql'); | ||
var deprecated_decorator_1 = require("deprecated-decorator"); | ||
var deprecated_decorator_1 = require('deprecated-decorator'); | ||
// @schemaDefinition: A GraphQL type schema in shorthand | ||
@@ -33,4 +35,2 @@ // @resolvers: Definitions for resolvers to be merged with schema | ||
allowUndefinedInResolve, resolverValidationOptions) { | ||
if (allowUndefinedInResolve === void 0) { allowUndefinedInResolve = true; } | ||
if (resolverValidationOptions === void 0) { resolverValidationOptions = {}; } | ||
if (typeof resolverValidationOptions !== 'object') { | ||
@@ -58,3 +58,3 @@ throw new SchemaError('Expected `resolverValidationOptions` to be an object'); | ||
function makeExecutableSchema(_a) { | ||
var typeDefs = _a.typeDefs, resolvers = _a.resolvers, connectors = _a.connectors, logger = _a.logger, _b = _a.allowUndefinedInResolve, allowUndefinedInResolve = _b === void 0 ? true : _b, _c = _a.resolverValidationOptions, resolverValidationOptions = _c === void 0 ? {} : _c; | ||
var typeDefs = _a.typeDefs, _b = _a.resolvers, resolvers = _b === void 0 ? {} : _b, connectors = _a.connectors, logger = _a.logger, _c = _a.allowUndefinedInResolve, allowUndefinedInResolve = _c === void 0 ? true : _c, _d = _a.resolverValidationOptions, resolverValidationOptions = _d === void 0 ? {} : _d; | ||
var jsSchema = _generateSchema(typeDefs, resolvers, logger, allowUndefinedInResolve, resolverValidationOptions); | ||
@@ -256,4 +256,4 @@ if (typeof resolvers['__schema'] === 'function') { | ||
if (resolverValidationOptions === void 0) { resolverValidationOptions = {}; } | ||
var _a = resolverValidationOptions.requireResolversForArgs, requireResolversForArgs = _a === void 0 ? true : _a, _b = resolverValidationOptions.requireResolversForNonScalar, requireResolversForNonScalar = _b === void 0 ? true : _b, _c = resolverValidationOptions.requireResolversForAllFields, requireResolversForAllFields = _c === void 0 ? false : _c; | ||
if (requireResolversForAllFields && (!requireResolversForArgs || !requireResolversForNonScalar)) { | ||
var _a = resolverValidationOptions.requireResolversForArgs, requireResolversForArgs = _a === void 0 ? false : _a, _b = resolverValidationOptions.requireResolversForNonScalar, requireResolversForNonScalar = _b === void 0 ? false : _b, _c = resolverValidationOptions.requireResolversForAllFields, requireResolversForAllFields = _c === void 0 ? false : _c; | ||
if (requireResolversForAllFields && (requireResolversForArgs || requireResolversForNonScalar)) { | ||
throw new TypeError('requireResolversForAllFields takes precedence over the more specific assertions. ' + | ||
@@ -302,2 +302,3 @@ 'Please configure either requireResolversForAllFields or requireResolversForArgs / ' + | ||
exports.addErrorLoggingToSchema = addErrorLoggingToSchema; | ||
// XXX badly named function. this doesn't really wrap, it just chains resolvers... | ||
function wrapResolver(innerResolver, outerResolver) { | ||
@@ -312,2 +313,13 @@ return function (obj, args, ctx, info) { | ||
} | ||
function chainResolvers(resolvers) { | ||
return function (root, args, ctx, info) { | ||
return resolvers.reduce(function (prev, curResolver) { | ||
if (curResolver) { | ||
return curResolver(prev, args, ctx, info); | ||
} | ||
return defaultResolveFn(prev, args, ctx, info); | ||
}, root); | ||
}; | ||
} | ||
exports.chainResolvers = chainResolvers; | ||
/* | ||
@@ -319,3 +331,2 @@ * fn: The function to decorate with the logger | ||
function decorateWithLogger(fn, logger, hint) { | ||
if (hint === void 0) { hint = ''; } | ||
if (typeof fn === 'undefined') { | ||
@@ -396,6 +407,8 @@ fn = defaultResolveFn; | ||
var property = source[fieldName]; | ||
return typeof property === 'function' ? source[fieldName]() : property; | ||
if (typeof property === 'function') { | ||
return source[fieldName](args, context); | ||
} | ||
return property; | ||
} | ||
return undefined; | ||
} | ||
//# sourceMappingURL=schemaGenerator.js.map |
{ | ||
"name": "graphql-tools", | ||
"version": "0.7.2", | ||
"description": "A set of useful GraphQL tools", | ||
"version": "0.8.0", | ||
"description": "A set of useful tools for GraphQL", | ||
"main": "dist/index.js", | ||
@@ -19,3 +19,3 @@ "typings": "dist/index.d.ts", | ||
"posttest": "npm run lint", | ||
"lint": "tslint ./src/**/*.ts", | ||
"lint": "tslint $(find src | grep ts$)", | ||
"watch": "tsc -w", | ||
@@ -25,3 +25,5 @@ "testonly": "mocha --reporter spec --full-trace ./dist/test/tests.js", | ||
"postcoverage": "remap-istanbul --input coverage/coverage.json --type lcovonly --output coverage/lcov.info", | ||
"prepublish": "npm run compile" | ||
"prepublish": "npm run compile", | ||
"prerelease": "npm test", | ||
"release": "standard-version" | ||
}, | ||
@@ -35,18 +37,19 @@ "repository": { | ||
"Apollo", | ||
"Meteor", | ||
"Proxy", | ||
"Cache", | ||
"Javascript" | ||
"JavaScript", | ||
"TypeScript", | ||
"Mock", | ||
"Schema", | ||
"Schema Language", | ||
"Tools" | ||
], | ||
"author": "Jonas Helfer <helfer@meteor.com>", | ||
"license": "ISC", | ||
"author": "Jonas Helfer <jonas@helfer.email>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/apollostack/apollo-proxy/issues" | ||
"url": "https://github.com/apollostack/graphql-tools/issues" | ||
}, | ||
"homepage": "https://github.com/apollostack/apollo-proxy#readme", | ||
"homepage": "https://github.com/apollostack/graphql-tools#readme", | ||
"dependencies": { | ||
"deprecated-decorator": "^0.1.6", | ||
"lodash": "^4.3.0", | ||
"node-uuid": "^1.4.7", | ||
"typed-graphql": "git://github.com/DxCx/typed-graphql.git#resolve-funcs-fix" | ||
"node-uuid": "^1.4.7" | ||
}, | ||
@@ -56,2 +59,5 @@ "peerDependencies": { | ||
}, | ||
"optionalDependencies": { | ||
"typed-graphql": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
@@ -68,23 +74,15 @@ "@types/bluebird": "^3.0.32", | ||
"chai": "^3.5.0", | ||
"dataloader": "git://github.com/DxCx/dataloader.git#typings-tmp-pkg", | ||
"delete-empty": "^0.1.3", | ||
"dts-bundle": "^0.5.0", | ||
"express": "^4.13.4", | ||
"express3": "0.0.0", | ||
"fs": "0.0.2", | ||
"graphql": "^0.7.0", | ||
"graphql-subscriptions": "^0.2.0", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^3.0.1", | ||
"multer": "^1.0.3", | ||
"nodemon": "^1.9.1", | ||
"remap-istanbul": "^0.6.4", | ||
"remap-istanbul": "^0.7.0", | ||
"request": "^2.72.0", | ||
"request-promise": "^4.1.0", | ||
"source-map-support": "^0.4.2", | ||
"supertest": "^2.0.0", | ||
"supertest-as-promised": "^4.0.0", | ||
"standard-version": "^3.0.0", | ||
"tslint": "^3.15.1", | ||
"tslint-loader": "^2.1.5", | ||
"typescript": "2.0.0" | ||
"typescript": "2.0.3" | ||
} | ||
} |
101
README.md
@@ -1,2 +0,2 @@ | ||
# GraphQL-tools: a set of tools for building GraphQL servers in JavaScript | ||
# GraphQL-tools: generate and mock GraphQL.js schemas | ||
@@ -7,9 +7,106 @@ [![npm version](https://badge.fury.io/js/graphql-tools.svg)](https://badge.fury.io/js/graphql-tools) | ||
[![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](http://www.apollostack.com/#slack) | ||
[![Standard Version](https://img.shields.io/badge/release-standard%20version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version) | ||
This package allows you to use the GraphQL schema language to build your [GraphQL.js](https://github.com/graphql/graphql-js) schema, and also includes useful schema tools like per-type mocking. | ||
## Documentation | ||
Documentation and a tutorial can be found on [docs.apollostack.com](http://dev.apollodata.com/tools/graphql-tools/index.html). | ||
[Read the docs.](http://dev.apollodata.com/tools/graphql-tools/index.html) | ||
## Example | ||
The ["Hello World" server](https://github.com/apollostack/frontpage-server) which powers our client examples is a great place to start if you're looking for a minimal codebase powered by `graphql-tools`. | ||
When using `graphql-tools`, you describe the schema as a GraphQL type language string: | ||
```js | ||
const schema = ` | ||
type Author { | ||
id: Int! # the ! means that every author object _must_ have an id | ||
firstName: String | ||
lastName: String | ||
posts: [Post] # the list of Posts by this author | ||
} | ||
type Post { | ||
id: Int! | ||
title: String | ||
author: Author | ||
votes: Int | ||
} | ||
# the schema allows the following query: | ||
type Query { | ||
posts: [Post] | ||
} | ||
# this schema allows the following mutation: | ||
type Mutation { | ||
upvotePost ( | ||
postId: Int! | ||
): Post | ||
} | ||
# we need to tell the server which types represent the root query | ||
# and root mutation types. We call them RootQuery and RootMutation by convention. | ||
schema { | ||
query: Query | ||
mutation: Mutation | ||
} | ||
`; | ||
export default schema; | ||
``` | ||
Then you define resolvers as a nested object that maps type and field names to resolver functions: | ||
```js | ||
const resolverMap = { | ||
Query: { | ||
posts() { | ||
return posts; | ||
}, | ||
}, | ||
Mutation: { | ||
upvotePost(_, { postId }) { | ||
const post = find(posts, { id: postId }); | ||
if (!post) { | ||
throw new Error(`Couldn't find post with id ${postId}`); | ||
} | ||
post.votes += 1; | ||
return post; | ||
}, | ||
}, | ||
Author: { | ||
posts(author) { | ||
return filter(posts, { authorId: author.id }); | ||
}, | ||
}, | ||
Post: { | ||
author(post) { | ||
return find(authors, { id: post.authorId }); | ||
}, | ||
}, | ||
}; | ||
export default resolverMap; | ||
``` | ||
At the end, the schema and resolvers are combined using `makeExecutableSchema`: | ||
```js | ||
import schema from './data/schema.js'; | ||
import resolverMap from './data/resolvers'; | ||
const executableSchema = makeExecutableSchema({ | ||
typeDefs: schema, | ||
resolvers: resolverMap, | ||
}); | ||
``` | ||
This example has the entire type definition in one string and all resolvers in one object, but you can combine types and resolvers from multiple files, as documented in the [modularizing the schema](http://dev.apollodata.com/tools/graphql-tools/generate-schema.html#modularizing) section of the docs. | ||
## Contributions | ||
Contributions, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR! |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Git dependency
Supply chain riskContains a dependency which resolves to a remote git URL. Dependencies fetched from git URLs are not immutable can be used to inject untrusted code or reduce the likelihood of a reproducible install.
Found 1 instance in 1 package
165506
22
25
857
112
0
+ Addedtyped-graphql@1.0.2(transitive)
- Removedtyped-graphql@git://github.com/DxCx/typed-graphql.git#resolve-funcs-fix