This package has been deprecated

Author message:

Package no longer supported. Actual version can be found here: https://www.npmjs.com/package/@de-fi/sdk

@defiyield-app/sdk
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

SDK client for De.Fi

Create the client

import { createClient } from '@defiyield-app/sdk'

const client = createClient({
  url: 'https://graphql-sdk-url',
  headers: { 'X-Api-Key': '' },
});

await client.query({
  chains: {
    id: true,
    name: true,
  },
});

Using a custom fetcher

You can use your own http fetcher function, must be of type (operation: {query, variables}) => Promise<{data, errors}>

const client = createClient({
  fetcherMethod: (operation) => {
    return fetch('http://graphql-sdk-url', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(operation),
    }).then((response) => response.json())
  },
})

Selecting fields

Client queries are similar to graphql queries in structure, to select some fields you pass an object where the keys are the field names.

Here is an example of a query made with the client:

client
  .query({
    countries: {
      name: true,
      code: true,
      languages: {
        name: true,
      },
    },
  })
  .then(console.log)

Passing arguments

If you want to pass arguments to a query you must use an array where the first object represents the arguments and the second object the fields selection.

client
  .query({
    countries: [
      {
        filter: { code: 'BR' },
      },
      {
        name: true,
        code: true,
        languages: {
          name: true,
        },
      },
    ],
  })
  .then(console.log)

client
  .mutation({
    createUser: [{ user: { name: 'user' } }, { name: true, age: true }],
  })
  .then(console.log)

Querying all fields

Client let you query all scalar fields in a type by using the everything object:

// everything is just an object: const everything = { __scalar: true };
import { everything  } from '@defiyield-app/sdk'

client
  .query({
    countries: {
      ...everything, // same as __scalar: true
      languages: {
        ...everything,
      },
    },
  })
  .then((x) => console.log(JSON.stringify(x)))

everything queries only the leaf types, you have to manually query object types

Excluding fields

You can also exclude some fields from the selection passing falsy values

client
  .query({
    countries: {
      ...everything, // same as __scalar: true
      id: false,
      code: false,
    },
  })
  .then((x) => console.log(JSON.stringify(x)))

Lib

Additional usage detailes and examples: gqlts docs

Package Sidebar

Install

npm i @defiyield-app/sdk

Weekly Downloads

1

Version

2.1.1

License

Apache-2.0

Unpacked Size

90.4 kB

Total Files

8

Last publish

Collaborators

  • de-fi
  • defiyield-app