Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GraphQL packages peer dependencies #239

Merged
merged 4 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased
<!-- here goes all the unreleased changes descriptions -->
### Features
- **Breaking Change**: make `graphql-js` packages a peer dependencies, bump `graphql` to `^14.1.1` and `@types/graphql` to `^14.0.5` (#239)
- **Breaking Change**: change the default `PrintSchemaOptions` option `commentDescriptions` to false (no more `#` comments in SDL)
- add support for passing `PrintSchemaOptions` in `buildSchema.emitSchemaFile` (e.g. `commentDescriptions: true` to restore previous behavior)
- add `buildTypeDefsAndResolvers` utils function for generating apollo-like `typeDefs` and `resolvers` pair (#233)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ Below you can find installation instructions that are also important.

### Installation

1. Install module:
1. Install the package and the `graphql` (peer dependency):
```
npm i type-graphql
npm i graphql @types/graphql type-graphql
```

2. `reflect-metadata` shim is required:
Expand Down
13 changes: 6 additions & 7 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ title: Installation

Before getting started with TypeGraphQL we need to install some additional dependencies and properly configure TypeScript configuration for our project.

## Prerequisites

Before you begin, make sure your development environment includes Node.js and npm.
> #### Prerequisites
> Before you begin, make sure your development environment includes Node.js and npm.

## Packages installation

Install module:
First, you have to install the main package, as well as the [`graphql-js`](https:/graphql/graphql-js) (and it's typings) which is a peer dependency of TypeGraphQL:
```sh
npm i type-graphql
npm i graphql @types/graphql type-graphql
```

`reflect-metadata` shim is required:
Also, the `reflect-metadata` shim is required to make the type reflection works:
```sh
npm i reflect-metadata
```

and make sure to import it on top of your entry file (before you use/import `type-graphql` or your resolvers):
Please make sure to import it on top of your entry file (before you use/import `type-graphql` or your resolvers):
```typescript
import "reflect-metadata";
```
Expand Down
151 changes: 136 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,26 @@
"check": "tsc --noEmit",
"lint": "tslint --project tsconfig.json"
},
"peerDependencies": {
"@types/graphql": "^14.0.5",
"graphql": "^14.1.1"
},
"dependencies": {
"@types/glob": "^7.1.1",
"@types/graphql": "^14.0.3",
"@types/node": "*",
"@types/semver": "^5.5.0",
"class-validator": ">=0.9.1",
"glob": "^7.1.3",
"graphql": "^14.0.2",
"graphql-query-complexity": "^0.2.2",
"graphql-subscriptions": "^1.0.0",
"semver": "^5.6.0",
"tslib": "^1.9.3"
},
"devDependencies": {
"@types/del": "^3.0.1",
"@types/express": "^4.16.0",
"@types/express-graphql": "^0.6.2",
"@types/graphql": "^14.0.5",
"@types/gulp": "^4.0.5",
"@types/gulp-replace": "0.0.31",
"@types/gulp-shell": "0.0.31",
Expand All @@ -73,6 +78,7 @@
"del": "^3.0.0",
"express": "^4.16.4",
"express-graphql": "^0.7.1",
"graphql": "^14.1.1",
"graphql-playground-middleware-express": "^1.7.8",
"graphql-redis-subscriptions": "^2.0.0",
"graphql-tag": "^2.10.0",
Expand Down
16 changes: 16 additions & 0 deletions src/errors/UnmetGraphQLPeerDependencyError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
getPeerDependencyGraphQLRequirement,
getInstalledGraphQLVersion,
} from "../utils/graphql-version";

export class UnmetGraphQLPeerDependencyError extends Error {
constructor() {
super(
`Looks like you use an incorrect version of the 'graphql' package: "${getInstalledGraphQLVersion()}". ` +
`Please ensure that you have installed a version ` +
`that meets TypeGraphQL's requirement: "${getPeerDependencyGraphQLRequirement()}".`,
);

Object.setPrototypeOf(this, new.target.prototype);
}
}
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export * from "./ReflectMetadataMissingError";
export * from "./SymbolKeysNotSupportedError";
export * from "./UnauthorizedError";
export * from "./UnionResolveTypeError";
export * from "./UnmetGraphQLPeerDependencyError";
export * from "./WrongNullableListOptionError";
2 changes: 2 additions & 0 deletions src/schema/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
} from "../errors";
import { ResolverFilterData, ResolverTopicData } from "../interfaces";
import { getFieldMetadataFromInputType, getFieldMetadataFromObjectType } from "./utils";
import { ensureInstalledCorrectGraphQLPackage } from "../utils/graphql-version";

interface ObjectTypeInfo {
target: Function;
Expand Down Expand Up @@ -98,6 +99,7 @@ export abstract class SchemaGenerator {
}

private static checkForErrors(options: SchemaGeneratorOptions) {
ensureInstalledCorrectGraphQLPackage();
if (getMetadataStorage().authorizedFields.length !== 0 && options.authChecker === undefined) {
throw new Error(
"You need to provide `authChecker` function for `@Authorized` decorator usage!",
Expand Down
22 changes: 22 additions & 0 deletions src/utils/graphql-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as semVer from "semver";

import { UnmetGraphQLPeerDependencyError } from "../errors";

export function getInstalledGraphQLVersion(): string {
const graphqlPackageJson = require("graphql/package.json");
return graphqlPackageJson.version;
}

export function getPeerDependencyGraphQLRequirement(): string {
const ownPackageJson = require("../../package.json");
return ownPackageJson.peerDependencies.graphql;
}

export function ensureInstalledCorrectGraphQLPackage() {
const installedVersion = getInstalledGraphQLVersion();
const versionRequirement = getPeerDependencyGraphQLRequirement();

if (!semVer.satisfies(installedVersion, versionRequirement)) {
throw new UnmetGraphQLPeerDependencyError();
}
}
Loading