Skip to content

Latest commit

 

History

History

blue-api-sdk

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

@morpho-org/blue-api-sdk

Version MIT License Downloads per month

GraphQL SDK that exports types from the API's GraphQL schema and a useful Apollo cache controller.

Installation

npm install @morpho-org/blue-api-sdk
yarn add @morpho-org/blue-api-sdk

Getting Started

Codegen

Create a codegen.ts file and define your desired preset & plugins, importing types from @morpho-org/blue-api-sdk. Below is given 3 typically recommended configurations:

Recommended near-operation-file preset config

import type { CodegenConfig } from "@graphql-codegen/cli";

import { BLUE_API_GRAPHQL_URL } from "@morpho-org/morpho-ts";

const config: CodegenConfig = {
  ...,
  schema: BLUE_API_GRAPHQL_URL,
  documents: ["src/graphql/**/*.{query,fragment}.gql"],
  generates: {
    "src/graphql/": {
      ...,
      preset: "near-operation-file",
      presetConfig: {
        baseTypesPath: "~@morpho-org/blue-api-sdk",
      },
    },
  },
};

export default config;

Recommended import-types preset config

import type { CodegenConfig } from "@graphql-codegen/cli";

import { BLUE_API_GRAPHQL_URL } from "@morpho-org/morpho-ts";

const config: CodegenConfig = {
  ...,
  schema: BLUE_API_GRAPHQL_URL,
  documents: ["graphql/*.{query,fragment}.gql"],
  generates: {
    "src/api/types.ts": {
      ...,
      preset: "import-types",
      presetConfig: {
        typesPath: "@morpho-org/blue-api-sdk",
      },
    },
  },
};

Recommended typescript-operations plugin config

import type { CodegenConfig } from "@graphql-codegen/cli";

import { BLUE_API_GRAPHQL_URL } from "@morpho-org/morpho-ts";

const config: CodegenConfig = {
  ...,
  schema: BLUE_API_GRAPHQL_URL,
  generates: {
    [...]: {
      plugins: ["typescript-operations", ...],
      config: {
        avoidOptionals: {
          field: true,
          inputValue: false,
          defaultValue: true,
        },
        scalars: {
          BigInt: {
            input: `Types.Scalars["BigInt"]["input"]`,
            output: `Types.Scalars["BigInt"]["output"]`,
          },
          HexString: {
            input: `Types.Scalars["HexString"]["input"]`,
            output: `Types.Scalars["HexString"]["output"]`,
          },
          Address: {
            input: `Types.Scalars["Address"]["input"]`,
            output: `Types.Scalars["Address"]["output"]`,
          },
          MarketId: {
            input: `Types.Scalars["MarketId"]["input"]`,
            output: `Types.Scalars["MarketId"]["output"]`,
          },
        },
      },
    },
  },
};

Apollo

Define an Apollo cache to use and specify the cache type policies exported from this package:

import { typePolicies } from "@morpho-org/blue-api-sdk";

// Apollo InMemoryCache needs to serialize BigInts to JSON, so we need to add a toJSON method to BigInt.prototype.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json
// @ts-ignore
BigInt.prototype.toJSON = function () {
  return this.toString();
};

export const inMemoryCache = new InMemoryCache({ typePolicies });