You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Ballerina获取其GraphQL API生成的GraphQL Schema?

如何通过Ballerina获取生成的GraphQL Schema?

当然可以,Ballerina提供了两种实用方式来获取生成的GraphQL Schema:

1. 在服务内部通过API直接获取

你可以在GraphQL服务代码中,通过self.getSchema()方法拿到graphql:Schema对象,再调用toString()方法导出SDL(Schema Definition Language)格式的Schema字符串。示例代码如下:

import ballerina/graphql;
import ballerina/io;

type User record {
    string id;
    string name;
};

service /graphql on new graphql:Listener(9090) {
    resource function get users() returns User[] {
        return [{id: "1", name: "Alice"}, {id: "2", name: "Bob"}];
    }

    // 服务初始化时获取并打印Schema
    function init() {
        graphql:Schema schema = self.getSchema();
        string schemaSDL = schema.toString();
        io:println("Generated GraphQL Schema:\n", schemaSDL);
    }
}

启动服务后,控制台会直接输出自动生成的SDL格式Schema内容。

2. 通过GraphQL自省查询从外部获取

如果不想修改服务代码,可以向Ballerina GraphQL服务发送标准的GraphQL自省查询请求,获取结构化的Schema数据,也可以将其转换为SDL格式使用。

自省查询语句

先准备标准的自省查询GraphQL代码:

query IntrospectionQuery {
  __schema {
    queryType { name }
    mutationType { name }
    subscriptionType { name }
    types {
      ...FullType
    }
    directives {
      name
      description
      locations
      args {
        ...InputValue
      }
    }
  }
}

fragment FullType on __Type {
  kind
  name
  description
  fields(includeDeprecated: true) {
    name
    description
    args {
      ...InputValue
    }
    type {
      ...TypeRef
    }
    isDeprecated
    deprecationReason
  }
  inputFields {
    ...InputValue
  }
  interfaces {
    ...TypeRef
  }
  enumValues(includeDeprecated: true) {
    name
    description
    isDeprecated
    deprecationReason
  }
  possibleTypes {
    ...TypeRef
  }
}

fragment InputValue on __InputValue {
  name
  description
  type { ...TypeRef }
  defaultValue
}

fragment TypeRef on __Type {
  kind
  name
  ofType {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
      }
    }
  }
}

使用Ballerina HTTP客户端发送请求

以下是用Ballerina代码发送自省查询的示例:

import ballerina/http;
import ballerina/io;

public function main() returns error? {
    // 替换为你的GraphQL服务地址
    http:Client graphqlClient = check new http:Client("http://localhost:9090/graphql");
    
    string introspectionQuery = `#graphql
        query IntrospectionQuery {
          __schema {
            queryType { name }
            mutationType { name }
            subscriptionType { name }
            types {
              ...FullType
            }
            directives {
              name
              description
              locations
              args {
                ...InputValue
              }
            }
          }
        }

        fragment FullType on __Type {
          kind
          name
          description
          fields(includeDeprecated: true) {
            name
            description
            args {
              ...InputValue
            }
            type {
              ...TypeRef
            }
            isDeprecated
            deprecationReason
          }
          inputFields {
            ...InputValue
          }
          interfaces {
            ...TypeRef
          }
          enumValues(includeDeprecated: true) {
            name
            description
            isDeprecated
            deprecationReason
          }
          possibleTypes {
            ...TypeRef
          }
        }

        fragment InputValue on __InputValue {
          name
          description
          type { ...TypeRef }
          defaultValue
        }

        fragment TypeRef on __Type {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
              }
            }
          }
        }
    `;
    
    http:Response response = check graphqlClient->post("", {"query": introspectionQuery});
    json schemaData = check response.getJsonPayload();
    io:println("Introspection Result:\n", schemaData);
}

运行这段代码,就能从外部获取到目标服务的GraphQL Schema数据。

内容的提问来源于stack exchange,提问作者Kavindu Gimhan Zoysa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 20:55:34