TypeScript Client
Installing depedencies and setting up new typescript project
We will be using graphql-codegen for generating TypeScript types and operations sdk for interacting with API with only data that we need. And making requests using generated sdk with graphql-request.
# Initializing new yarn project
$ yarn init -y
# Adding dependencies
$ yarn add graphql @graphql-codegen/cli @graphql-codegen/typescript-operations @graphql-codegen/typescript-graphql-request graphql-request typescript ts-node
# Downloads latest GraphQL API schema
$ curl https://api.lumar.io/schema.graphql > schema.graphql
# Generating typescript config
$ yarn tsc --init
codegen.yml
schema:
- schema.graphql
documents:
- src/**/*.graphql
generates:
./src/sdk.ts:
plugins:
- typescript
- typescript-operations
- typescript-graphql-request
config: &config
scalars:
BigInt: "number"
CompareToCrawl: "string | number"
ObjectID: "string | number"
JSONObject: "any"
JSON: "any"
DateTime: "string"
Upload: "unknown"
strictScalars: true
documentMode: string
Example operations that we will be generating SDK for and running queries and mutations against Lumar GraphQL API.
src/operations.graphql
mutation LoginWithUserKey($secret: String!, $userKeyId: ObjectID!) {
createSessionUsingUserKey(input: { userKeyId: $userKeyId, secret: $secret }) {
token
}
}
query MyAccounts {
me {
accounts(first: 10) {
totalCount
nodes {
id
name
}
}
}
}
Setup ENV variables for authenticating with Lumar API
Creating User key to use for API authentication.
export DEEPCRAWL_SECRET_ID=
export DEEPCRAWL_SECRET=
Writing your TypeScript script for making API calls
src/script.ts
import { GraphQLClient } from "graphql-request";
import { getSdk } from "./sdk";
const deepcrawlSecretId = process.env.DEEPCRAWL_SECRET_ID;
const deepcrawlSecret = process.env.DEEPCRAWL_SECRET;
if (!deepcrawlSecretId || !deepcrawlSecret) {
console.error("You must set the DEEPCRAWL_SECRET_ID and DEEPCRAWL_SECRET environment variables.");
process.exit(1);
}
async function main() {
const client = new GraphQLClient("https://api.lumar.io/graphql", {
headers: {
"apollographql-client-name": `your-client-name`,
},
});
const sdk = getSdk(client);
const sessionTokenResponse = await sdk.LoginWithUserKey({
userKeyId: process.env.DEEPCRAWL_SECRET_ID!,
secret: process.env.DEEPCRAWL_SECRET!,
});
client.setHeader("x-auth-token", sessionTokenResponse.createSessionUsingUserKey.token);
console.log("My accounts are:");
const accounts = await sdk.MyAccounts();
console.log(JSON.stringify(accounts, null, 2));
}
main().catch(err => {
console.error("Unexpected error occurect", err);
process.exit(1);
});
Generating typescript types and SDK operations. Should re-run it after any change in operations.graphql
file.
yarn graphql-codegen
Running your script:
yarn --silent ts-node src/script.ts
Successfull run will print out something like:
My accounts are:
{
"me": {
"accounts": {
"totalCount": 1,
"nodes": [
{
"id": "TjAwN0FjY291bnQyMTkyMQ",
"name": "Your account name"
}
]
}
}
}
Code from this example can be found on our Github.