Skip to content

Commit

Permalink
[sdk] Add graphql schema export (MystenLabs#20679)
Browse files Browse the repository at this point in the history
## Description 

Describe the changes or additions included in this PR.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
hayes-mysten authored Dec 18, 2024
1 parent 715edf0 commit a872b97
Show file tree
Hide file tree
Showing 16 changed files with 5,147 additions and 37 deletions.
6 changes: 6 additions & 0 deletions .changeset/healthy-swans-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@mysten/graphql-transport': patch
'@mysten/zksend': patch
---

Use `latest` as schema for graphql types
5 changes: 5 additions & 0 deletions .changeset/sweet-experts-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/sui': minor
---

Add `latest` as an exported graphql schema
2 changes: 1 addition & 1 deletion graphql.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { IGraphQLConfig } from 'graphql-config';
const config: IGraphQLConfig = {
projects: {
tsSDK: {
schema: './sdk/typescript/src/graphql/generated/2024.4/schema.graphql',
schema: './sdk/typescript/src/graphql/generated/latest/schema.graphql',
documents: [
'./sdk/graphql-transport/src/**/*.ts',
'./sdk/graphql-transport/src/**/*.graphql',
Expand Down
1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ packages:
- '!sdk/signers/gcp'
- '!sdk/typescript/zklogin'
- '!sdk/typescript/keypairs/passkey'
- '!sdk/typescript/graphql/schemas/latest'
2 changes: 1 addition & 1 deletion sdk/docs/pages/typescript/graphql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ We'll start by creating our client, and executing a very basic query:

```typescript
import { SuiGraphQLClient } from '@mysten/sui/graphql';
import { graphql } from '@mysten/sui/graphql/schemas/2024.4';
import { graphql } from '@mysten/sui/graphql/schemas/latest';

const gqlClient = new SuiGraphQLClient({
url: 'https://sui-testnet.mystenlabs.com/graphql',
Expand Down
45 changes: 34 additions & 11 deletions sdk/graphql-transport/src/generated/queries.ts

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions sdk/typescript/graphql/schemas/latest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"import": "../../../dist/esm/graphql/schemas/latest/index.js",
"main": "../../../dist/cjs/graphql/schemas/latest/index.js",
"sideEffects": false
}
4 changes: 4 additions & 0 deletions sdk/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
"./graphql/schemas/2024.4": {
"import": "./dist/esm/graphql/schemas/2024.4/index.js",
"require": "./dist/cjs/graphql/schemas/2024.4/index.js"
},
"./graphql/schemas/latest": {
"import": "./dist/esm/graphql/schemas/latest/index.js",
"require": "./dist/cjs/graphql/schemas/latest/index.js"
}
},
"scripts": {
Expand Down
51 changes: 30 additions & 21 deletions sdk/typescript/scripts/update-graphql-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ for (const release of result) {
}

for (const { minorVersion, schema } of releasesByVersion.values()) {
await addSchemaVersion(minorVersion, schema);
}

await addSchemaVersion(
'latest',
'https://raw.githubusercontent.com/MystenLabs/sui/refs/heads/mainnet/crates/sui-graphql-rpc/schema.graphql',
);

await addExportsToPackageJson([...releasesByVersion.keys(), 'latest']);

async function addExportsToPackageJson(versions: string[]) {
const packageJsonPath = resolve(import.meta.url.slice(5), '../../package.json');
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8'));

for (const version of versions) {
packageJson.exports[`./graphql/schemas/${version}`] = {
import: `./dist/esm/graphql/schemas/${version}/index.js`,
require: `./dist/cjs/graphql/schemas/${version}/index.js`,
};
}

await writeFile(packageJsonPath, `${JSON.stringify(packageJson, null, '\t')}\n`);
}

async function addSchemaVersion(versionName: string, schema: string) {
const res = await fetch(schema);

if (!res.ok) {
Expand All @@ -54,7 +79,7 @@ for (const { minorVersion, schema } of releasesByVersion.values()) {

const filePath = resolve(
import.meta.url.slice(5),
`../../src/graphql/generated/${minorVersion}/schema.graphql`,
`../../src/graphql/generated/${versionName}/schema.graphql`,
);

await mkdir(resolve(filePath, '..'), { recursive: true });
Expand All @@ -69,7 +94,7 @@ for (const { minorVersion, schema } of releasesByVersion.values()) {
{
"name": "@0no-co/graphqlsp",
"schema": "./schema.graphql",
"tadaOutputLocation": "src/graphql/generated/${minorVersion}/tada-env.d.ts"
"tadaOutputLocation": "src/graphql/generated/${versionName}/tada-env.d.ts"
}
]
}
Expand All @@ -81,16 +106,16 @@ for (const { minorVersion, schema } of releasesByVersion.values()) {
stdio: 'inherit',
});

await mkdir(resolve(filePath, '../../../schemas', minorVersion), { recursive: true });
await mkdir(resolve(filePath, '../../../schemas', versionName), { recursive: true });
await writeFile(
resolve(filePath, `../../../schemas/${minorVersion}/index.ts`),
resolve(filePath, `../../../schemas/${versionName}/index.ts`),
`
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
import { initGraphQLTada } from 'gql.tada';
import type { introspection } from '../../generated/${minorVersion}/tada-env.js';
import type { introspection } from '../../generated/${versionName}/tada-env.js';
import type { CustomScalars } from '../../types.js';
export * from '../../types.js';
Expand All @@ -105,19 +130,3 @@ export const graphql = initGraphQLTada<{
`.trimStart(),
);
}

await addExportsToPackageJson(Array.from(releasesByVersion.keys()));

async function addExportsToPackageJson(versions: string[]) {
const packageJsonPath = resolve(import.meta.url.slice(5), '../../package.json');
const packageJson = JSON.parse(await readFile(packageJsonPath, 'utf-8'));

for (const version of versions) {
packageJson.exports[`./graphql/schemas/${version}`] = {
import: `./dist/esm/graphql/schemas/${version}/index.js`,
require: `./dist/cjs/graphql/schemas/${version}/index.js`,
};
}

await writeFile(packageJsonPath, `${JSON.stringify(packageJson, null, '\t')}\n`);
}
Loading

0 comments on commit a872b97

Please sign in to comment.