Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ yarn-error.log

# Generated resources (all regenerated at build time)
src/resources/

# Published copy of the schema (regenerated by fetch:schema)
/schema.graphql
294 changes: 200 additions & 94 deletions README.md

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,19 @@ sdk.subscriptions.onPriceUpdated(

### Custom GraphQL Query

Custom queries let you request only the fields you need — you're billed for the fields your query requests, so this is the recommended pattern for production. Wrap the SDK's exported query types in `DeepPartial` so the compiler stays honest about fields your query didn't select:

```typescript
import { GetNetworksQuery } from "@codex-data/sdk";
import { DeepPartial, GetNetworksQuery } from "@codex-data/sdk";

const result = await sdk.send<GetNetworksQuery>(
const result = await sdk.send<DeepPartial<GetNetworksQuery>>(
`query GetNetworks { getNetworks { id name } }`,
{},
);
```

For exact types inferred from your query (no `DeepPartial` needed), see the [codegen example](./codegen/).

### Mutation Example

```typescript
Expand Down
4 changes: 3 additions & 1 deletion examples/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
overwrite: true,
schema: "../../src/resources/schema.graphql",
// The SDK ships its schema, so codegen runs offline against the exact
// schema version you have installed.
schema: "./node_modules/@codex-data/sdk/schema.graphql",
documents: "src/**/*.ts",
generates: {
"src/gql/": {
Expand Down
6,140 changes: 5,820 additions & 320 deletions examples/codegen/src/gql/graphql.ts

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions examples/codegen/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Codex } from "@codex-data/sdk";
import { graphql } from "./gql/gql";
import { NetworksQuery, NetworksQueryVariables } from "./gql/graphql";
import { graphql } from "./gql";

// The result type is inferred from exactly the fields selected here —
// and you're only billed for the fields you request.
const doc = graphql(`
query Networks {
getNetworks {
Expand All @@ -13,6 +14,6 @@ const doc = graphql(`

const sdk = new Codex(process.env.CODEX_API_KEY || "");

sdk.query<NetworksQuery, NetworksQueryVariables>(doc).then((res) => {
console.log("Fetched res", res);
sdk.query(doc).then((res) => {
console.log("Fetched res", res.getNetworks);
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
},
"./schema.graphql": "./schema.graphql"
},
"sideEffects": false,
"scripts": {
Expand All @@ -23,7 +24,7 @@
"build:cjs": "tsc",
"build:esm": "tsc -p tsconfig.esm.json",
"build:rename-esm": "find dist-esm -name '*.js' -exec sh -c 'mv \"$1\" \"${1%.js}.mjs\"' _ {} \\; && cp -r dist-esm/* dist/ && rm -rf dist-esm",
"fetch:schema": "curl -s https://graph.codex.io/schema/latest.graphql --output src/resources/schema.graphql",
"fetch:schema": "curl -s https://graph.codex.io/schema/latest.graphql --output src/resources/schema.graphql && cp src/resources/schema.graphql schema.graphql",
"generate:configs": "tsx src/scripts/generateNetworkConfigs.ts",
"generate:graphql": "tsx src/scripts/generateGraphql.ts",
"build:sdk": "tsx src/scripts/buildSdk.ts",
Expand All @@ -40,6 +41,7 @@
"dist/index.mjs",
"dist/index.d.ts",
"dist/sdk/",
"schema.graphql",
"README.md"
],
"keywords": [
Expand Down
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,22 @@ export type { ApiConfig, CleanupFunction } from "./sdk";

// Export GraphQL types for use in applications
export * from "./sdk/generated/graphql";

/**
* Recursively marks every field of a type as optional.
*
* Useful with `sdk.send()` when your query selects a subset of a type's
* fields: the exported query types (e.g. `FilterTokensQuery`) describe the
* full selection, so wrapping them in `DeepPartial` keeps the compiler honest
* about fields your query didn't ask for.
*
* @example
* const result = await sdk.send<DeepPartial<FilterTokensQuery>>(
* `query { filterTokens(limit: 10) { results { priceUSD } } }`,
* );
*/
export type DeepPartial<T> = T extends (infer U)[]
? DeepPartial<U>[]
: T extends object
? { [K in keyof T]?: DeepPartial<T[K]> }
: T;
Loading