Skip to content

Commit 1c908cb

Browse files
committed
Merge branch 'abernix/recover-md'
2 parents ce972f7 + 2cf8a89 commit 1c908cb

File tree

2 files changed

+179
-4
lines changed

2 files changed

+179
-4
lines changed

docs/_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ sidebar_categories:
2626
- platform/schema-registry
2727
- platform/client-awareness
2828
- platform/schema-validation
29-
# - platform/operation-registry
29+
- platform/operation-registry
3030
- platform/vscode
3131
- platform/tracing
3232
- platform/errors

docs/source/platform/operation-registry.md

Lines changed: 178 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,183 @@ Operations defined within client applications are automatically extracted and up
2929

3030
> Make sure you've met the requirements for _Prerequisites_ above.
3131
32-
1. Install the `apollo` command line tool as a development dependency of your client applicationt:
32+
**1. Install the `apollo` command line tool as a development dependency of your client application:**
3333

34-
npm install apollo --save-dev
34+
```
35+
npm install apollo --save-dev
36+
```
3537

36-
> Yarn users can run `yarn add apollo --dev`.
38+
> Yarn users can run `yarn add apollo --dev`.
39+
40+
**2. Register the server's schema with Apollo Engine:**
41+
42+
> If this server's schema has already been registered using `apollo service:push`, you can skip this step. For additional options and details, see the [documentation for the schema registry](./schema-registry.html).
43+
44+
First, make sure Apollo Server is running and that introspection is enabled (it is often disabled in production).
45+
46+
Next, using the following command as a reference, replace the `<ENGINE_API_KEY>` with the Apollo Engine API key from the appropriate service and specify the correct server endpoint with the `--endpoint` flag:
47+
48+
```
49+
npx apollo service:push \
50+
--key <ENGINE_API_KEY> \
51+
--endpoint https://server/graphql
52+
```
53+
54+
When successful, this command should return output similar to the following:
55+
56+
```
57+
✔ Loading Apollo config
58+
✔ Fetching current schema
59+
✔ Publishing <service> to Apollo Engine
60+
61+
id schema tag
62+
------ ------------- -------
63+
abc123 <service> current
64+
```
65+
66+
> If you encounter any errors, refer to the _**Troubleshooting**_ section below.
67+
68+
**3. Register operations from the client bundle.**
69+
70+
Now we'll use `apollo queries:register` to locate operations within the client codebase and upload a manifest of those operations to Apollo Engine. Once Apollo Server has been configured to respect the operation registry, only operations which have been included in the manifest will be permitted.
71+
72+
The `apollo queries:register` command:
73+
74+
* Supports multiple client bundles. Each bundle is identified by a `clientName` (e.g. `react-web`).
75+
* Supports JavaScript, TypeScript and `.graphql` files.
76+
* Accepts a list of files as a glob (e.g. `src/**/*.ts`) to search for GraphQL operations.
77+
* By default, includes the `__typename` fields which are added by Apollo Client at runtime.
78+
79+
To register operations, use the following command as a reference, taking care to replace the `<ENGINE_API_KEY>` with the appropriate Apollo Engine API key, specifying a unique name for this application with `<CLIENT_IDENTIFIER>`, and indicating the correct glob of files to search:
80+
81+
```
82+
npx apollo queries:register \
83+
--key <ENGINE_API_KEY> \
84+
--clientName <CLIENT_IDENTIFIER> \
85+
--queries="src/**/*.{ts,js,graphql}"
86+
```
87+
88+
When succesfull, the output from this command should look similar to the following:
89+
90+
```
91+
✔ Loading Apollo config
92+
✔ Loading schema
93+
✔ Resolving GraphQL document sets
94+
✔ Scanning for GraphQL queries (N found)
95+
✔ Isolating operations and fragments
96+
✔ Combining operations and fragments
97+
✔ Generating manifest
98+
✔ Registering operations with Apollo Engine service <service>
99+
```
100+
101+
If you encounter any errors, check the _**Troubleshooting**_ section below.
102+
103+
**4. Enable demand control by adding the operation registry to Apollo Server.**
104+
105+
To enable the operation registry within Apollo Server, it's necessary to install and enable the `apollo-server-plugin-operation-registry` plugin and ensure Apollo Server is configured to communicate with Apollo Engine.
106+
107+
First, add the appropriate plugin to the Apollo Server's `package.json`:
108+
109+
```
110+
npm install apollo-server-plugin-operation-registry
111+
```
112+
113+
> Yarn uses run: `yarn add apollo-server-plugin-operation-registry`.
114+
115+
Next, the plugin must be enabled. This requires adding the appropriate module to the `plugins` parameter to the Apollo Server options:
116+
117+
```js
118+
const server = new ApolloServer({
119+
// Existing configuration
120+
typeDefs,
121+
resolvers,
122+
// ...
123+
// New configuration
124+
plugins: [
125+
require("apollo-server-plugin-operation-registry")({
126+
forbidUnregisteredOperations: true,
127+
}),
128+
],
129+
});
130+
```
131+
132+
**5. Start Apollo Server with Apollo Engine enabled**
133+
134+
If the server was already configured to use Apollo Engine, no additional changes are necessary, but it's important to make sure that the server is configured to use the same service as the operations were registered with in step 3.
135+
136+
If the server was not previously configured with Apollo Engine, be sure to start the server with the `ENGINE_API_KEY` variable set to the appropriate API key. For example:
137+
138+
```
139+
ENGINE_API_KEY=<ENGINE_API_KEY> npm start
140+
```
141+
142+
Alternatively, the API key can be specified with the `engine` parameter on the Apollo Server constructor options:
143+
144+
```js
145+
const server = new ApolloServer({
146+
// ...
147+
engine: "<ENGINE_API_KEY>",
148+
// ...
149+
});
150+
```
151+
152+
For security, it's recommended to pass the Engine API key as an environment variable so it will not be checked into version control (VCS).
153+
154+
**6. Verification**
155+
156+
With the operation registry enabled, _only_ operations which have been registered will be permitted.
157+
158+
To confirm that everything is configured properly, try executing an operation against the server which was **not** registered from the client bundle in step 3.
159+
160+
For example, using `curl` this could be done with a command similar to:
161+
162+
```
163+
curl 'http://server/graphql/' \
164+
-H 'Content-Type: application/json' \
165+
--data-binary '{"query":"query { likes{title} }"}'
166+
```
167+
168+
If the server is configured properly, it should return:
169+
170+
```
171+
Execution forbidden
172+
```
173+
174+
Finally, to confirm that the server will allow permitted operations, try running an operation from the client.
175+
176+
## Troubleshooting
177+
178+
### Operations aren't being forbidden or operations which should be permitted are not allowed
179+
180+
The first step in debugging the operation registry behavior is to enable debugging. This can be done by enabling the `debug` setting on the plugin within the Apollo Server constructor options:
181+
182+
```js
183+
const server = new ApolloServer({
184+
typeDefs,
185+
resolvers,
186+
plugins: [
187+
require("apollo-server-plugin-operation-registry")({
188+
// ... other, existing options ...
189+
debug: true,
190+
});
191+
],
192+
});
193+
```
194+
195+
When the server is started with debugging enabled, additional information will be displayed at server startup which can be useful in determining the source of the problem. For example:
196+
197+
```
198+
Checking for manifest changes at https://...
199+
🚀 app running at http://localhost:4000/
200+
Incoming manifest ADDs: ba4573fca2e1491fd54b9f3984...
201+
Incoming manifest ADDs: 32a21510374c3c9ad25e064240...
202+
Incoming manifest ADDs: c60ac6dfe19ba70dd9d6a29a27...
203+
```
204+
205+
By clicking on the URL listed in the `Checking for manifest changes at ` message, it will be possible to see the full contents of the manifest and see the list of permitted operations. This information is not publicly available and this URL should not be shared.
206+
207+
### Schema registration
208+
209+
If a problem occurs during the `apollo service:push` command, make sure that the running Apollo Server can be accessed from the machine where the command is being executed.
210+
211+
Additionally, make sure that introspection is enabled on the server since introspection details are used to obtain and publish the schema.

0 commit comments

Comments
 (0)