@acknow-srl/graphql 中文文档教程

发布于 5年前 浏览 12 更新于 3年前

GrapQL

允许您与 GraphQL 服务器交互并执行查询。

GraphQLModule (Module)

Methods

  • forRoot(config: GQLConfig): void: configures the connection to the GraphQL server.

GQLConfig (Interface)

  • server (string): GraphQL server URL.

GQL (Service)

它在 root 中提供,因此它对整个应用程序可用。

Methods

  • query(query: string, vars: object | null = null): Observable:向 GraphQL 服务器发送一个查询(带有可选参数,作为普通对象传递)。 返回带有 GraphQL 服务器响应的 Observable

  • trim(value: string | null | undefined): string:返回一个没有前导和尾随空格的字符串。 此方法对于字符串、nullundefined 值是安全的。 如果传递了 nullundefined,则此方法返回一个空字符串。

Example

/**
 * 1. Import the module and all classes you need in your main module (usually app.module.ts).
 */

import { AckGraphQLModule, GQLConfig } from '@acknow-srl/graphql';

/**
 * 2. Add the module to your app imports and configure it.
 */

const conf: GQLConfig = {
    server: 'http://my-graphql-server-url'
};

@NgModule({
  declarations: [
    AppComponent
    ...
  ],
  imports: [
    ...
    AckGraphQLModule.forRoot(conf),
    ...
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

/**
 * 3. Implement GQL service in your services or components.
 */

import { Injectable } from '@angular/core';

import { throwError } from 'rxjs';

import { map, catchError } from 'rxjs/operators';

import { GQL } from '@acknow-srl/graphql';

@Injectable({
    providedIn: 'root'
})
export class MyService {

    constructor(private _gql: GQL) {
    }

    // Gets an entry from the database by ID.

    getEntry(id: number) {

            // Sets parameters.

            let vars = {
                id: id 
            };

            // Sets query.

            let q = `query Entry($id: Int!) {
                    allEntries(condition: {id: $id}) {
                            nodes {
                                    id
                                    field1
                                    field2
                                    field3
                            }
                    }
            }`;

            return this._gql.query(q, vars).pipe(map(res => {
                    return (res && res.data)? res.data.allEntries.nodes[0] : null;
            }),
            catchError(err => {
                    throwError(err);
            }));
    }

}

GrapQL

Allows you to interface with a GraphQL server and execute queries.

GraphQLModule (Module)

Methods

  • forRoot(config: GQLConfig): void: configures the connection to the GraphQL server.

GQLConfig (Interface)

  • server (string): GraphQL server URL.

GQL (Service)

It is provided in root, so it is available to the whole app.

Methods

  • query(query: string, vars: object | null = null): Observable<any>: sends a query (with optional parameters, passed as a plain object) to the GraphQL server. Returns an Observable with the GraphQL server response.

  • trim(value: string | null | undefined): string: returns a string without leading and trailing spaces. This method is safe for string, null and undefined values. If null or undefined is passed, this method returns an empty string.

Example

/**
 * 1. Import the module and all classes you need in your main module (usually app.module.ts).
 */

import { AckGraphQLModule, GQLConfig } from '@acknow-srl/graphql';

/**
 * 2. Add the module to your app imports and configure it.
 */

const conf: GQLConfig = {
    server: 'http://my-graphql-server-url'
};

@NgModule({
  declarations: [
    AppComponent
    ...
  ],
  imports: [
    ...
    AckGraphQLModule.forRoot(conf),
    ...
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}

/**
 * 3. Implement GQL service in your services or components.
 */

import { Injectable } from '@angular/core';

import { throwError } from 'rxjs';

import { map, catchError } from 'rxjs/operators';

import { GQL } from '@acknow-srl/graphql';

@Injectable({
    providedIn: 'root'
})
export class MyService {

    constructor(private _gql: GQL) {
    }

    // Gets an entry from the database by ID.

    getEntry(id: number) {

            // Sets parameters.

            let vars = {
                id: id 
            };

            // Sets query.

            let q = `query Entry($id: Int!) {
                    allEntries(condition: {id: $id}) {
                            nodes {
                                    id
                                    field1
                                    field2
                                    field3
                            }
                    }
            }`;

            return this._gql.query(q, vars).pipe(map(res => {
                    return (res && res.data)? res.data.allEntries.nodes[0] : null;
            }),
            catchError(err => {
                    throwError(err);
            }));
    }

}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文