Skip to content

Commit

Permalink
GraphQL API (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ committed Jan 15, 2023
1 parent a08530d commit da661b9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 12 additions & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,18 @@ export abstract class CrowdinApi {
this.config = config;
}

graphql<T>(req: { query: string; operationName?: string; variables?: any }): Promise<ResponseObject<T>> {
if (!this.organization) {
throw new Error('GraphQL API could be used only with Crowdin Enterprise.');
}

return this.post<ResponseObject<T>>(
`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) ? '&' : '?';
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit da661b9

Please sign in to comment.