Skip to content

Commit

Permalink
examples/acephei: update for #4561 and #4453
Browse files Browse the repository at this point in the history
In #4561 we removed `@apollo/federation` and `@apollo/gateway` from the
apollo-server repo. This means that the acephei example doesn't know where to
import them from. Adding them to examples/acephei/package.json led to very
strange errors so let's just add them as top-level dev dependencies.

Additionally, removing `@apollo/federation` seemed to make it so that TypeScript
can't find `apollo-env` any more, but it also seems like maybe the references to
it in the tsconfig.json lines aren't necessary? Not sure what's going on here
but leaving them in led to build errors and removing them didn't.

Finally, update the uses of `engine:` to use the new post-#4453 APIs, and spell
the version "set up" as two words.
  • Loading branch information
glasser committed Sep 22, 2020
1 parent 1af2792 commit 0c23878
Show file tree
Hide file tree
Showing 13 changed files with 473 additions and 33 deletions.
4 changes: 2 additions & 2 deletions examples/acephei/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ cd examples/acephei
npm install
```

* *(Optional)*: Setup [Apollo Studio](https://studio.apollographql.com/) account and create your first graph. You'll need to copy the service API key for later on.
* *(Optional)*: Set up [Apollo Studio](https://studio.apollographql.com/) account and create your first graph. You'll need to copy the service API key for later on.

### Quick start script

Expand Down Expand Up @@ -57,7 +57,7 @@ The launch configurations are defined in the `.vscode/launch.json` folders withi

## Apollo Studio - Setting up your graph

If you would like to setup this example with the graph you created in your [Apollo Studio](https://studio.apollographql.com/) account, you'll need to register each downstream service using the [Apollo CLI](https://github.com/apollographql/apollo-tooling):
If you would like to set up this example with the graph you created in your [Apollo Studio](https://studio.apollographql.com/) account, you'll need to register each downstream service using the [Apollo CLI](https://github.com/apollographql/apollo-tooling):

```
apollo service:push --key={service:michael-watson:key_1234} --localSchemaFile=./services/accounts/schema.graphql --serviceName=accounts --serviceURL=http://localhost:4001
Expand Down
29 changes: 16 additions & 13 deletions examples/acephei/gateway/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { ApolloServer } from "apollo-server";
import { ApolloServerPluginUsageReporting,
ApolloServerPluginUsageReportingDisabled } from "apollo-server-core";
import { ApolloGateway, RemoteGraphQLDataSource, GatewayConfig } from "@apollo/gateway";
import DepthLimitingPlugin from "./plugins/ApolloServerPluginDepthLimiting";
import StrictOperationsPlugin from "./plugins/ApolloServerPluginStrictOperations";
import ReportForbiddenOperationsPlugin from "./plugins/ApolloServerPluginReportForbiddenOperation";

const isProd = process.env.NODE_ENV === "production";
const apolloKey = process.env.APOLLO_KEY;
const graphVariant = process.env.APOLLO_GRAPH_VARIANT || "development";
if (!process.env.APOLLO_GRAPH_VARIANT) {
process.env.APOLLO_GRAPH_VARIANT = "development";
}

class AuthenticatedDataSource extends RemoteGraphQLDataSource {
willSendRequest({ request, context }) {
Expand Down Expand Up @@ -39,7 +43,6 @@ if (!apolloKey) {
}

const apolloOperationRegistryPlugin = apolloKey ? require("apollo-server-plugin-operation-registry")({
graphVariant,
forbidUnregisteredOperations({
context, // Destructure the shared request `context`.
request: {
Expand All @@ -53,20 +56,19 @@ const apolloOperationRegistryPlugin = apolloKey ? require("apollo-server-plugin-
}
}) : {};

const apolloUsageReportingPlugin = apolloKey ? ApolloServerPluginUsageReporting({
sendVariableValues: {
all: true
},
sendHeaders: {
all: true
}
}) : ApolloServerPluginUsageReportingDisabled();

const gateway = new ApolloGateway(gatewayOptions);
const server = new ApolloServer({
gateway,
subscriptions: false, // Must be disabled with the gateway; see above.
engine: {
apiKey: apolloKey, //We set the APOLLO_KEY environment variable
graphVariant, //We set the APOLLO_GRAPH_VARIANT environment variable
sendVariableValues: {
all: true
},
sendHeaders: {
all: true
}
},
context: ({ req }) => {
// get the user token from the headers
const token = req.headers.authorization || "";
Expand All @@ -82,7 +84,8 @@ const server = new ApolloServer({
DepthLimitingPlugin({ maxDepth: 10 }),
StrictOperationsPlugin(),
ReportForbiddenOperationsPlugin({ debug: true }),
apolloOperationRegistryPlugin
apolloOperationRegistryPlugin,
apolloUsageReportingPlugin,
]
});

Expand Down
4 changes: 2 additions & 2 deletions examples/acephei/gateway/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"noUnusedLocals": false,
"forceConsistentCasingInFileNames": true,
"lib": ["es2017", "esnext.asynciterable", "dom"],
"types": ["node", "apollo-env"],
"types": ["node"],
"jsx": "react",
"baseUrl": "."
},
"include": ["./src/**/*"],
"exclude": ["**/__tests__/*", "**/__mocks__/*"]
}
}
7 changes: 5 additions & 2 deletions examples/acephei/services/accounts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApolloServer, gql, AuthenticationError } from "apollo-server";
import { ApolloServerPluginUsageReportingDisabled } from "apollo-server-core";
import { buildFederatedSchema } from "@apollo/federation";
import { readFileSync } from "fs";
import { resolve } from "path";
Expand Down Expand Up @@ -48,9 +49,11 @@ interface Context {

const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }]),
engine: true,
context: ({ req }) => ({ userID: req.headers.userid }),
dataSources: () => ({ users: new UsersDataSource() })
dataSources: () => ({ users: new UsersDataSource() }),
// $APOLLO_KEY may be set in this process but that's for the gateway to report,
// not this implementing service.
plugins: [ApolloServerPluginUsageReportingDisabled()],
});

const port = process.env.PORT || 4001;
Expand Down
4 changes: 2 additions & 2 deletions examples/acephei/services/accounts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"noUnusedLocals": false,
"forceConsistentCasingInFileNames": true,
"lib": ["es2017", "esnext.asynciterable", "dom"],
"types": ["node", "apollo-env"],
"types": ["node"],
"jsx": "react",
"baseUrl": "."
},
"include": ["./src/**/*"],
"exclude": ["**/__tests__/*", "**/__mocks__/*"]
}
}
7 changes: 5 additions & 2 deletions examples/acephei/services/books/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApolloServer, gql, AuthenticationError } from "apollo-server";
import { ApolloServerPluginUsageReportingDisabled } from "apollo-server-core";
import { buildFederatedSchema } from "@apollo/federation";
import { readFileSync } from "fs";
import { resolve } from "path";
Expand Down Expand Up @@ -45,8 +46,10 @@ interface Context {

const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }]),
engine: true,
dataSources: () => ({ books: new BooksDataSource() })
dataSources: () => ({ books: new BooksDataSource() }),
// $APOLLO_KEY may be set in this process but that's for the gateway to report,
// not this implementing service.
plugins: [ApolloServerPluginUsageReportingDisabled()],
});

const port = process.env.PORT || 4005;
Expand Down
4 changes: 2 additions & 2 deletions examples/acephei/services/books/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"noUnusedLocals": false,
"forceConsistentCasingInFileNames": true,
"lib": ["es2017", "esnext.asynciterable", "dom"],
"types": ["node", "apollo-env"],
"types": ["node"],
"jsx": "react",
"baseUrl": "."
},
"include": ["./src/**/*"],
"exclude": ["**/__tests__/*", "**/__mocks__/*"]
}
}
7 changes: 5 additions & 2 deletions examples/acephei/services/products/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApolloServer, gql } from "apollo-server";
import { ApolloServerPluginUsageReportingDisabled } from "apollo-server-core";
import { buildFederatedSchema } from "@apollo/federation";
import { readFileSync } from "fs";
import { resolve } from "path";
Expand Down Expand Up @@ -81,10 +82,12 @@ interface Context {

const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }]),
engine: true,
dataSources: () => ({
products: new ProductsDataSource()
})
}),
// $APOLLO_KEY may be set in this process but that's for the gateway to report,
// not this implementing service.
plugins: [ApolloServerPluginUsageReportingDisabled()],
});

const port = process.env.PORT || 4003;
Expand Down
4 changes: 2 additions & 2 deletions examples/acephei/services/products/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"noUnusedLocals": false,
"forceConsistentCasingInFileNames": true,
"lib": ["es2017", "esnext.asynciterable", "dom"],
"types": ["node", "apollo-env"],
"types": ["node"],
"jsx": "react",
"baseUrl": "."
},
"include": ["./src/**/*"],
"exclude": ["**/__tests__/*", "**/__mocks__/*"]
}
}
7 changes: 5 additions & 2 deletions examples/acephei/services/reviews/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ApolloServer, gql } from "apollo-server";
import { ApolloServerPluginUsageReportingDisabled } from "apollo-server-core";
import { buildFederatedSchema } from "@apollo/federation";
import { readFileSync } from "fs";
import { resolve } from "path";
Expand Down Expand Up @@ -112,11 +113,13 @@ interface Context {

const server = new ApolloServer({
schema: buildFederatedSchema([{ typeDefs, resolvers }]),
engine: true,
dataSources: () => ({
reviews: new ReviewsDataSource(),
users: new UsersDataSource()
})
}),
// $APOLLO_KEY may be set in this process but that's for the gateway to report,
// not this implementing service.
plugins: [ApolloServerPluginUsageReportingDisabled()],
});

const port = process.env.PORT || 4002;
Expand Down
4 changes: 2 additions & 2 deletions examples/acephei/services/reviews/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"noUnusedLocals": false,
"forceConsistentCasingInFileNames": true,
"lib": ["es2017", "esnext.asynciterable", "dom"],
"types": ["node", "apollo-env"],
"types": ["node"],
"jsx": "react",
"baseUrl": "."
},
"include": ["./src/**/*"],
"exclude": ["**/__tests__/*", "**/__mocks__/*"]
}
}
Loading

0 comments on commit 0c23878

Please sign in to comment.