|
| 1 | +--- |
| 2 | +title: Android Studio / IntelliJ plugin |
| 3 | +--- |
| 4 | + |
| 5 | +To help your day-to-day development with Apollo Kotlin, a plugin for Android Studio and IntelliJ is available. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +The plugin is available on the JetBrains plugin repository. To install it from the IDE, go to |
| 10 | +<kbd>Settings</kbd> | <kbd>Plugins</kbd> | <kbd>Marketplace</kbd> | search for "Apollo GraphQL" | <kbd>Install</kbd> |
| 11 | + |
| 12 | +At the time of writing, the plugin is supported on: |
| 13 | + |
| 14 | +- IntelliJ 2022.2 and above |
| 15 | +- Android Studio 2022.2 (Flamingo) and above |
| 16 | + |
| 17 | +Please check the [plugin page](https://plugins.jetbrains.com/plugin/20645-apollo-graphql) for up to date information. |
| 18 | + |
| 19 | +## Features |
| 20 | + |
| 21 | +### Automatic code generation |
| 22 | + |
| 23 | +The plugin automatically generates models whenever GraphQL files change. |
| 24 | +This means you don't need to manually run `./gradlew generateApolloSources` after editing the files. |
| 25 | + |
| 26 | +Note: this works by running Gradle in continuous mode, and can be disabled in the plugin settings. |
| 27 | + |
| 28 | +### Integration with the GraphQL IntelliJ Plugin |
| 29 | + |
| 30 | +The plugin depends on and integrates with the [GraphQL IntelliJ Plugin](https://plugins.jetbrains.com/plugin/8097-js-graphql). |
| 31 | +The file structure of the Apollo project is automatically contributed, so there is no need to create a `graphql.config.yml` / `.graphqlconfig` file. |
| 32 | + |
| 33 | +### Navigation |
| 34 | + |
| 35 | +#### Kotlin → GraphQL |
| 36 | + |
| 37 | +When editing Kotlin code, <img src="images/gutter-icon.png" alt="Gutter icon" width="16" valign="middle" style="display: inline; margin: 2px;"/> icons will show next to references to generated operation and fragment classes. |
| 38 | +Clicking on them will navigate to the corresponding GraphQL definition. |
| 39 | + |
| 40 | +You can also go to the GraphQL definition with <kbd>Cmd</kbd> Click (Mac) or <kbd>Ctrl</kbd> Click (Windows / Linux) on an operation, field, enum, etc. |
| 41 | + |
| 42 | +#### GraphQL → Kotlin |
| 43 | + |
| 44 | +You can search for code usages of a GraphQL operation, fragment, enum type/value and input type/field, with right click | <kbd>Find Usages</kbd>. |
| 45 | + |
| 46 | +### Unused operations / fields |
| 47 | + |
| 48 | +In GraphQL files, operations and fields are shown in grey if the corresponding generated code is not used in your project. |
| 49 | + |
| 50 | +### Migration helpers |
| 51 | + |
| 52 | +In the <kbd>Refactor</kbd> | <kbd>Apollo</kbd> menu, you can find helpers to migrate your project: |
| 53 | + |
| 54 | +- Apollo Android 2.x → Apollo Kotlin 3.x |
| 55 | +- `compat` codegen → `operationBased` codegen |
| 56 | +- Apollo Kotlin 3.x → Apollo Kotlin 4.x |
| 57 | + |
| 58 | +Note: while these helpers will automatically update your code when possible, there are some cases where this isn't possible and manual changes are required. |
| 59 | +Please refer to the migration guides ([3.x](../migration/3.0/), [4.x](../migration/4.0/)) when upgrading. |
| 60 | + |
| 61 | +### Open in Apollo Sandbox |
| 62 | + |
| 63 | +You can open a GraphQL file in [Apollo Sandbox](https://studio.apollographql.com/sandbox/explorer) with right click | <kbd>Open in</kbd> | <kbd>Apollo Sandbox</kbd>. |
| 64 | +This is a handy way to share operations with coworkers for instance. There, they will be able to execute the operation and see the result. |
| 65 | + |
| 66 | +This is also accessible from the <img src="images/open-in-sandbox.png" alt="Sandbox icon" width="16" valign="middle" style="display: inline; margin: 2px;"/> button at the top of the editor. |
| 67 | + |
| 68 | +### High latency fields |
| 69 | + |
| 70 | +If your project uses Apollo GraphOS, the plugin can use the collected metrics (["Field Insights"](https://www.apollographql.com/docs/graphos/metrics/field-usage)) and report a warning on fields that have a high latency. |
| 71 | + |
| 72 | +<img src="images/high-latency-field-editor.png" width="400" alt="High latency in editor"/> |
| 73 | + |
| 74 | +By default, fields with a latency above 100ms will be highlighted. You can change this threshold in <kbd>Settings</kbd> | <kbd>Editor</kbd> | <kbd>Inspections</kbd> | <kbd>GraphQL</kbd> | <kbd>Apollo GraphOS</kbd> | <kbd>High latency field</kbd>. |
| 75 | + |
| 76 | +A quick fix is available to encapsulate the field in a fragment with the [`@defer`](../fetching/defer) directive. |
| 77 | + |
| 78 | +For the plugin to be able to fetch the metrics, you need to configure your project with its [Apollo GraphOS API key](https://www.apollographql.com/docs/graphos/api-keys/). To do this, go to the plugin's settings: <kbd>Settings</kbd> | <kbd>Languages & Frameworks</kbd> | <kbd>GraphQL</kbd> | <kbd>Apollo Kotlin</kbd>. Here you can find the <kbd>API keys</kbd> table where you can add: |
| 79 | +- API key: should start with `service:` or `user:` |
| 80 | +- Graph ID: can be found on your graph page in Apollo Studio or is filled automatically if you provide a `service:` API key |
| 81 | + |
| 82 | +## Weekly snapshots |
| 83 | + |
| 84 | +If you wish to try the latest features, you can install the weekly snapshots. To do so, add the following repository in <kbd>Settings</kbd> | <kbd>Plugins</kbd> | <kbd>⚙</kbd>️ | <kbd>Manage Plugin Repositories</kbd> | <kbd>+</kbd>: `https://raw.githubusercontent.com/apollographql/apollo-kotlin/main/intellij-plugin/snapshots/plugins.xml`. |
| 85 | + |
| 86 | +Then installing the plugin by searching "Apollo GraphQL" in the Marketplace will install the latest snapshot. |
0 commit comments