You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/platform/operation-registry.md
+178-3Lines changed: 178 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,8 +29,183 @@ Operations defined within client applications are automatically extracted and up
29
29
30
30
> Make sure you've met the requirements for _Prerequisites_ above.
31
31
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:**
33
33
34
-
npm install apollo --save-dev
34
+
```
35
+
npm install apollo --save-dev
36
+
```
35
37
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`:
**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
+
constserver=newApolloServer({
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:
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:
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:
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