-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL Yoga driver for NestJS GraphQL (#2525)
- Loading branch information
Showing
41 changed files
with
3,789 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-yoga/nestjs-federation': major | ||
--- | ||
|
||
GraphQL Yoga driver with Apollo Federation for NestJS GraphQL |
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,20 @@ | ||
--- | ||
'@graphql-yoga/nestjs': major | ||
--- | ||
|
||
GraphQL Yoga driver for NestJS GraphQL. | ||
|
||
### BREAKING CHANGES | ||
|
||
- No more `subscriptionWithFilter` in YogaBaseDriver. | ||
- `YogaBaseDriver.yogaInstance` has been renamed to `YogaBaseDriver.yoga` | ||
- `YogaBaseDriver` has been renamed to `AbstractYogaDriver` | ||
- Drop `@envelop/apollo-server-errors`, if you want to use it - supply it to the plugins yourself | ||
- `graphql` is now a peer dependency | ||
- `graphql-yoga` is now a peer dependency | ||
- `installSubscriptionHandlers` driver option has been dropped, please use the `subscriptions` | ||
option | ||
- Apollo Federation v2 support | ||
- Apollo Federation driver has been moved to a separate package `@graphql-yoga/nestjs-federation` | ||
- Dropped support for `@nestjs/graphql@v10`, now at least v11 is required (https://github.com/nestjs/graphql/pull/2435) | ||
- Minimum Node.js engine is v14 |
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,59 @@ | ||
// WARNING: please make sure the versions are the same across all workspaces | ||
const singletons = [ | ||
'@nestjs/core', | ||
'@nestjs/common', | ||
'@nestjs/graphql', | ||
'@apollo/subgraph', | ||
'@apollo/federation-subgraph-compatibility', | ||
] | ||
|
||
function afterAllResolved(lockfile, context) { | ||
context.log('Enforcing single version for: ' + singletons.join(', ')) | ||
|
||
// find and choose one version for the singletons | ||
const singletonsMap = {} | ||
const danglingSingletons = [] | ||
for (const pkg of Object.keys(lockfile.packages)) { | ||
const singlePkg = singletons.find((singlePkg) => | ||
pkg.startsWith(`/${singlePkg}/`), | ||
) | ||
if (!singlePkg) { | ||
continue | ||
} | ||
if (singlePkg in singletonsMap) { | ||
danglingSingletons.push(pkg) | ||
continue | ||
} | ||
singletonsMap[singlePkg] = pkg.replace(`/${singlePkg}/`, '') | ||
} | ||
|
||
// remove dangling singletons from lockfile | ||
for (const dangling of danglingSingletons) { | ||
delete lockfile.packages[dangling] | ||
} | ||
|
||
// apply singleton versions | ||
;[lockfile.packages, lockfile.importers].forEach((list) => { | ||
for (const info of Object.values(list)) { | ||
const deps = info.dependencies | ||
const devDeps = info.devDependencies | ||
|
||
for (const [pkg, ver] of Object.entries(singletonsMap)) { | ||
if (pkg in (deps || {})) { | ||
deps[pkg] = ver | ||
} | ||
if (pkg in (devDeps || {})) { | ||
devDeps[pkg] = ver | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return lockfile | ||
} | ||
|
||
module.exports = { | ||
hooks: { | ||
afterAllResolved, | ||
}, | ||
} |
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
12 changes: 12 additions & 0 deletions
12
examples/nestjs-apollo-federation-compatibility/docker-compose.yaml
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,12 @@ | ||
services: | ||
products: | ||
image: 'node:19' | ||
user: 'node' | ||
working_dir: /web | ||
environment: | ||
- NODE_ENV=production | ||
volumes: | ||
- ./dist:/web | ||
command: 'node index.js' | ||
ports: | ||
- 4001:4001 |
40 changes: 40 additions & 0 deletions
40
examples/nestjs-apollo-federation-compatibility/package.json
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,40 @@ | ||
{ | ||
"name": "example-nestjs-apollo-federation-compatibility", | ||
"version": "1.0.0", | ||
"description": "Apollo Federation implemented with GraphQL Yoga running as a Nest.js driver.", | ||
"private": true, | ||
"scripts": { | ||
"build": "nest build && node scripts/bundle.js", | ||
"prebuild": "rimraf dist", | ||
"start": "nest start", | ||
"test": "fedtest docker --compose ./docker-compose.yaml --schema ./schema.graphql --path /graphql --port 4001 --debug --format markdown" | ||
}, | ||
"devDependencies": { | ||
"@apollo/federation-subgraph-compatibility": "1.2.1", | ||
"@apollo/rover": "^0.13.0", | ||
"@graphql-yoga/nestjs-federation": "0.0.0", | ||
"@grpc/proto-loader": "^0.7.5", | ||
"@nestjs/cli": "^9.3.0", | ||
"@nestjs/common": "^9.3.12", | ||
"@nestjs/core": "^9.3.12", | ||
"@nestjs/graphql": "^11.0.4", | ||
"@nestjs/microservices": "^9.3.12", | ||
"@nestjs/platform-socket.io": "^9.3.12", | ||
"@nestjs/websockets": "^9.3.12", | ||
"amqp-connection-manager": "^4.1.11", | ||
"amqplib": "^0.10.3", | ||
"cache-manager": "^5.2.0", | ||
"class-transformer": "0.3.1", | ||
"class-validator": "^0.14.0", | ||
"esbuild": "0.17.12", | ||
"graphql": "^16.6.0", | ||
"kafkajs": "^2.2.4", | ||
"mqtt": "^4.3.7", | ||
"nats": "^2.13.1", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^4.1.2", | ||
"rxjs": "^7.8.0", | ||
"ts-morph": "^17.0.1", | ||
"typescript": "^4.9.5" | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
examples/nestjs-apollo-federation-compatibility/schema.graphql
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,83 @@ | ||
extend schema | ||
@link( | ||
url: "https://specs.apollo.dev/federation/v2.3" | ||
import: [ | ||
"@composeDirective" | ||
"@extends" | ||
"@external" | ||
"@key" | ||
"@inaccessible" | ||
"@interfaceObject" | ||
"@override" | ||
"@provides" | ||
"@requires" | ||
"@shareable" | ||
"@tag" | ||
] | ||
) | ||
@link(url: "https://myspecs.dev/myCustomDirective/v1.0", import: ["@custom"]) | ||
@composeDirective(name: "@custom") | ||
|
||
directive @custom on OBJECT | ||
|
||
type Product | ||
@custom | ||
@key(fields: "id") | ||
@key(fields: "sku package") | ||
@key(fields: "sku variation { id }") { | ||
id: ID! | ||
sku: String | ||
package: String | ||
variation: ProductVariation | ||
dimensions: ProductDimension | ||
createdBy: User @provides(fields: "totalProductsCreated") | ||
notes: String @tag(name: "internal") | ||
research: [ProductResearch!]! | ||
} | ||
|
||
type DeprecatedProduct @key(fields: "sku package") { | ||
sku: String! | ||
package: String! | ||
reason: String | ||
createdBy: User | ||
} | ||
|
||
type ProductVariation { | ||
id: ID! | ||
} | ||
|
||
type ProductResearch @key(fields: "study { caseNumber }") { | ||
study: CaseStudy! | ||
outcome: String | ||
} | ||
|
||
type CaseStudy { | ||
caseNumber: ID! | ||
description: String | ||
} | ||
|
||
type ProductDimension @shareable { | ||
size: String | ||
weight: Float | ||
unit: String @inaccessible | ||
} | ||
|
||
extend type Query { | ||
product(id: ID!): Product | ||
deprecatedProduct(sku: String!, package: String!): DeprecatedProduct | ||
@deprecated(reason: "Use product query instead") | ||
} | ||
|
||
extend type User @key(fields: "email") { | ||
averageProductsCreatedPerYear: Int | ||
@requires(fields: "totalProductsCreated yearsOfEmployment") | ||
email: ID! @external | ||
name: String @override(from: "users") | ||
totalProductsCreated: Int @external | ||
yearsOfEmployment: Int! @external | ||
} | ||
|
||
type Inventory @interfaceObject @key(fields: "id") { | ||
id: ID! | ||
deprecatedProducts: [DeprecatedProduct!]! | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/nestjs-apollo-federation-compatibility/scripts/bundle.js
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,29 @@ | ||
/* eslint-disable */ | ||
const { build } = require('esbuild') | ||
const { copyFileSync } = require('fs') | ||
const { join } = require('path') | ||
|
||
async function main() { | ||
await build({ | ||
entryPoints: ['./dist/main.js'], | ||
outfile: 'dist/index.js', | ||
format: 'cjs', | ||
minify: false, | ||
bundle: true, | ||
platform: 'node', | ||
target: 'es2020', | ||
loader: { '.node': 'file' }, | ||
}) | ||
|
||
console.info(`NestJS Apollo Subgraph test build done!`) | ||
|
||
copyFileSync( | ||
join(__dirname, '../schema.graphql'), | ||
join(__dirname, '../dist/schema.graphql'), | ||
) | ||
} | ||
|
||
main().catch((e) => { | ||
console.error(e) | ||
process.exit(1) | ||
}) |
28 changes: 28 additions & 0 deletions
28
examples/nestjs-apollo-federation-compatibility/src/app.module.ts
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,28 @@ | ||
import { | ||
YogaFederationDriver, | ||
YogaFederationDriverConfig, | ||
} from '@graphql-yoga/nestjs-federation' | ||
import { Module } from '@nestjs/common' | ||
import { GraphQLModule } from '@nestjs/graphql' | ||
import { DeprecatedProductsResolver } from './deprecated-products.resolver' | ||
import { InventoryResolver } from './inventory.resolver' | ||
import { ProductResearchResolver } from './product-research.resolver' | ||
import { ProductsResolver } from './products.resolver' | ||
import { UsersResolver } from './users.resolver' | ||
|
||
@Module({ | ||
imports: [ | ||
GraphQLModule.forRoot<YogaFederationDriverConfig>({ | ||
driver: YogaFederationDriver, | ||
typePaths: ['**/*.graphql'], | ||
}), | ||
], | ||
providers: [ | ||
UsersResolver, | ||
ProductsResolver, | ||
ProductResearchResolver, | ||
DeprecatedProductsResolver, | ||
InventoryResolver, | ||
], | ||
}) | ||
export class AppModule {} |
Oops, something went wrong.