Skip to content

Commit

Permalink
Merge pull request #1955 from Shopify/liz/add-graphqlrc-examples
Browse files Browse the repository at this point in the history
[Docs] Add examples for codgen for UI extensions and functions
  • Loading branch information
lizkenyon authored Jan 20, 2025
2 parents 66fd00e + 1eb6657 commit 36e6ca6
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions packages/api-clients/api-codegen-preset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,84 @@ export default {
};
```

#### Example `.graphqlrc.ts` file with to generate types for UI extensions
You can specify multiple APIs in your `.graphqlrc.ts` file by adding multiple projects.

```js
import { LATEST_API_VERSION } from "@shopify/shopify-api";
import { shopifyApiProject, ApiType } from "@shopify/api-codegen-preset";
import type { IGraphQLConfig } from "graphql-config";

function getConfig() {
const config: IGraphQLConfig = {
projects: {
default: shopifyApiProject({
apiType: ApiType.Storefront,
apiVersion: LATEST_API_VERSION,
documents: ["./extensions/**/*.{js,ts,jsx,tsx}", "./extensions/.server/**/*.{js,ts,jsx,tsx}"],
outputDir: "./extensions/types",
}),
UIExtensions: shopifyApiProject({
apiType: ApiType.Admin,
apiVersion: LATEST_API_VERSION,
documents: ["./app/**/*.{js,ts,jsx,tsx}", "./app/.server/**/*.{js,ts,jsx,tsx}" ],
outputDir: "./app/types",
}),
},
};

const config = getConfig();
export default config;
```
### Example `graphqlrc.ts` file for autocompletion for Shopify Function Extensions
```ts
import fs from "fs";
import { LATEST_API_VERSION } from "@shopify/shopify-api";
import { shopifyApiProject, ApiType } from "@shopify/api-codegen-preset";
import type { IGraphQLConfig } from "graphql-config";

function getConfig() {
const config: IGraphQLConfig = {
projects: {
default: shopifyApiProject({
apiType: ApiType.Storefront,
apiVersion: LATEST_API_VERSION,
documents: ["./extensions/**/*.{js,ts,jsx,tsx}", "./extensions/.server/**/*.{js,ts,jsx,tsx}"],
outputDir: "./extensions/types",
}),
};

let extensions: string[] = [];
try {
extensions = fs.readdirSync("./extensions");
console.log(extensions, "extensions");
} catch {
// ignore if no extensions
}

for (const entry of extensions) {
console.log(entry, "entry");
const extensionPath = `./extensions/${entry}`;
const schema = `${extensionPath}/schema.graphql`;
if (!fs.existsSync(schema)) {
continue;
}
config.projects[entry] = {
schema,
documents: [`${extensionPath}/**/*.graphql`],
};
}


return config;
}

const config = getConfig();
export default config;
```
## Generating types
Once you configure your app, you can run `graphql-codegen` to start parsing your queries for types.
Expand Down Expand Up @@ -198,6 +276,21 @@ npm run graphql-codegen
pnpm graphql-codegen
```
### Generating types for more than one API
If you have specified more than one API in your `.graphqlrc.ts` file, you can run the script for each API by passing the `--project` [flag](https://the-guild.dev/graphql/codegen/docs/config-reference/multiproject-config#command-to-generate-files).
```sh
npm run graphql-codegen --project=UIExtensions
```
```sh
yarn graphql-codegen --project=UIExtensions
```
```sh
pnpm graphql-codegen --project=UIExtensions
```
> [!NOTE]
> Codegen will fail if it can't find any documents to parse.
> To fix that, either create a query or set the [`ignoreNoDocuments` option](https://the-guild.dev/graphql/codegen/docs/config-reference/codegen-config#configuration-options) to `true`.
Expand Down

0 comments on commit 36e6ca6

Please sign in to comment.