@de-fi/sdk
v2.2.7
Published
De.Fi sdk client
Downloads
560
Maintainers
Readme
SDK client for De.Fi
Create the client
import { createClient } from '@de-fi/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 '@de-fi/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