Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL API #221

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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