diff --git a/docs/source/development-testing/static-typing.md b/docs/source/development-testing/static-typing.md index 2a14d72da0f..0b43a16895a 100644 --- a/docs/source/development-testing/static-typing.md +++ b/docs/source/development-testing/static-typing.md @@ -1,18 +1,19 @@ --- -title: Using Apollo with TypeScript +title: TypeScript with Apollo Client +descriptions: How to generate and use TypeScript types in your application --- -As your application grows, you may find it helpful to include a type system to assist in development. Apollo supports type definitions for TypeScript out of the box. Apollo Client ships with definitions in its associated npm package, so installation should be done for you after the libraries are included in your project. +As your application grows, a type system can become an essential tool for catching bugs early and improving your overall developer experience. -These docs assume you already have TypeScript configured in your project, if not start [here](https://github.com/Microsoft/TypeScript-React-Conversion-Guide#typescript-react-conversion-guide). - -The most common need when using type systems with GraphQL is to type the results of an operation. Given that a GraphQL server's schema is strongly typed, we can generate TypeScript definitions automatically using a tool like [GraphQL Code Generator](https://www.the-guild.dev/graphql/codegen). +GraphQL uses a type system to clearly define the available data for each type and field in a GraphQL schema. Given that a GraphQL server's schema is strongly typed, we can generate TypeScript definitions automatically using a tool like [GraphQL Code Generator](https://www.the-guild.dev/graphql/codegen). We'll use our generated types to ensure type safety for the _inputs_ and _results_ of our GraphQL operations. Below, we'll guide you through installing and configuring GraphQL Code Generator to generate types for your hooks and components. ## Setting up your project -To get started using GraphQL Code Generator, we'll begin by installing the following packages (using Yarn or NPM): +> This article assumes your project already uses TypeScript. If not, [configure your project to use TypeScript](https://github.com/Microsoft/TypeScript-React-Conversion-Guide#typescript-react-conversion-guide) or [start a new project](https://create-react-app.dev/docs/adding-typescript/). + +To get started using GraphQL Code Generator, begin by installing the following packages (using Yarn or NPM): ```bash yarn add -D typescript @graphql-codegen/cli @graphql-codegen/client-preset @@ -41,7 +42,7 @@ const config: CodegenConfig = { export default config; ``` -> There are multiple ways to [specify a schema](https://www.the-guild.dev/graphql/codegen/docs/config-reference/schema-field#root-level) in your `codegen.ts`, so pick the way that works best for your project setup. +> There are multiple ways to [specify a schema](https://www.the-guild.dev/graphql/codegen/docs/config-reference/schema-field#root-level) in your `codegen.ts`, so pick whichever way works best for your project setup. Finally, we'll add the following scripts to our `package.json` file: @@ -54,7 +55,7 @@ Finally, we'll add the following scripts to our `package.json` file: } ``` -GraphQL Code Generator generates types based on our GraphQL schema if we run either code-generation script above: +Running either of the scripts above generates types based on the schema file or GraphQL API you provided in `codegen.ts`: ```bash $ yarn run compile @@ -64,10 +65,12 @@ $ yarn run compile ## Typing hooks -GraphQL Code Generator automatically generates a `gql` function (from the `src/__genterated__/gql.ts` file), which we can use to type our hooks. +GraphQL Code Generator automatically creates a `gql` function (from the `src/__genterated__/gql.ts` file). This function enables us to type the variables that go into our React hooks, along with the results from those hooks. ### `useQuery` +Below we use the `gql` function to define our query, which automatically generates types for our `useQuery` hook: + ```tsx import React from 'react'; import { useQuery } from '@apollo/client'; @@ -86,7 +89,7 @@ const GET_ROCKET_INVENTORY = gql(/* GraphQL */ ` `); export function RocketInventoryList() { - // data is typed! + // our query's result, data, is typed! const { loading, data } = useQuery( GET_ROCKET_INVENTORY, // variables are also typed! @@ -122,15 +125,17 @@ export function RocketInventoryList() { #### `fetchMore` and `subscribeToMore` -`useQuery` returns an instance of `QueryResult`. This includes the `fetchMore` and `subscribeToMore` functions. See the `Result` section of the [Queries](../data/queries#result) documentation page for detailed type information. Because these functions execute GraphQL operations, they accept type parameters. +The `useQuery` hook returns an instance of `QueryResult`, which includes the `fetchMore` and `subscribeToMore` functions. See [Queries for detailed type information](../data/queries#result). Because these functions execute GraphQL operations, they accept type parameters. -`fetchMore`'s type parameters are similar to those of `useQuery`. In fact, the type parameters are set to the same values as `useQuery`'s by default. Since both `fetchMore` and `useQuery` encapsulate a `query` operation, it's unlikely that you will need to pass any type arguments to `fetchMore`. Here's a sketch derived from the previous example: +By default, the type parameters for `fetchMore` are the same as those for `useQuery`. Because both `fetchMore` and `useQuery` encapsulate a query operation, it's unlikely that you'll need to pass any type arguments to `fetchMore`. +Expanding our previous example, notice that we don't explicitly type `fetchMore`, because it defaults to using the same type parameters as `useQuery`: ```tsx // ... export function RocketInventoryList() { const { fetchMore, loading, data } = useQuery( GET_ROCKET_INVENTORY, + // variables are typed! { variables: { year: 2019 } } ); @@ -138,6 +143,7 @@ export function RocketInventoryList() { //...