diff --git a/README.md b/README.md index 5f1da439a..027604e18 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Our API is a full-featured RESTful API that helps you to integrate localization - [Installation](#installation) - [Quick Start](#quick-start) - [Over-The-Air Content Delivery](#over-the-air-content-delivery) +- [GraphQL API](#graphql-api) - [Seeking Assistance](#seeking-assistance) - [Contributing](#contributing) - [License](#license) @@ -419,6 +420,47 @@ test(); You can also use the [Crowdin OTA Client JS](https://github.com/crowdin/ota-client-js) library to send the translated content to your web apps via content delivery. Crowdin Content Delivery uses a CDN vault that mirrors your project’s translated content. The updated translations will become available to users much faster. +## GraphQL API + +This library also provides possibility to use [GraphQL API](https://developer.crowdin.com/graphql-api/) (only for Crowdin Enterprise). + +```javascript +const crowdin = require('@crowdin/crowdin-api-client'); + +const client = new crowdin.default({ + token: '{token}', + organization: '{organization}' +}); + +const query = ` +query { + viewer { + projects(first: 50) { + edges { + node { + name + + files(first: 10) { + totalCount + edges { + node { + name + type + } + } + } + } + } + } + } +} +`; + +client + .graphql({ query }) + .then(res => console.log(JSON.stringify(res, null, 2))); +``` + ## Seeking Assistance If you find any problems or would like to suggest a feature, please read the [How can I contribute](/CONTRIBUTING.md#how-can-i-contribute) section in our contributing guidelines. diff --git a/package-lock.json b/package-lock.json index 069aef293..a63db3234 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@crowdin/crowdin-api-client", - "version": "1.20.1", + "version": "1.21.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@crowdin/crowdin-api-client", - "version": "1.20.1", + "version": "1.21.0", "license": "MIT", "dependencies": { "axios": "0.21.3" diff --git a/package.json b/package.json index 6851c44d4..6078aa521 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@crowdin/crowdin-api-client", - "version": "1.20.1", + "version": "1.21.0", "description": "JavaScript library for Crowdin API v2.", "main": "out/index.js", "types": "out/index.d.ts", diff --git a/src/core/index.ts b/src/core/index.ts index 722f72905..dfe8b974f 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -211,6 +211,18 @@ export abstract class CrowdinApi { this.config = config; } + graphql(req: { query: string; operationName?: string; variables?: any }): Promise> { + if (!this.organization) { + throw new Error('GraphQL API could be used only with Crowdin Enterprise.'); + } + + return this.post>( + `https://${this.organization}.api.crowdin.com/api/graphql`, + req, + this.defaultConfig(), + ); + } + protected addQueryParam(url: string, name: string, value?: string | number): string { if (value) { url += new RegExp(/\?.+=.*/g).test(url) ? '&' : '?'; diff --git a/src/index.ts b/src/index.ts index 75c5077e7..77d16c7fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import { Bundles } from './bundles'; -import { ClientConfig, Credentials } from './core'; +import { ClientConfig, Credentials, CrowdinApi } from './core'; import { Dictionaries } from './dictionaries'; import { Distributions } from './distributions'; import { Glossaries } from './glossaries'; @@ -55,7 +55,7 @@ export * from './workflows'; /** * @internal */ -export default class Client { +export default class Client extends CrowdinApi { readonly sourceFilesApi: SourceFiles; readonly glossariesApi: Glossaries; readonly languagesApi: Languages; @@ -86,6 +86,7 @@ export default class Client { readonly bundlesApi: Bundles; constructor(credentials: Credentials, config?: ClientConfig) { + super(credentials, config); this.sourceFilesApi = new SourceFiles(credentials, config); this.glossariesApi = new Glossaries(credentials, config); this.languagesApi = new Languages(credentials, config);