如何使用Python获取GraphQL模式?

2024-04-29 01:04:58 发布

您现在位置:Python中文网/ 问答频道 /正文

有很多GUI客户机,比如GraphQL游乐场、GraphiQl等等,它们能够从URL获取GraphQL模式。如何使用Python获得模式?在


Tags: url客户机模式guigraphql游乐场graphiql
1条回答
网友
1楼 · 发布于 2024-04-29 01:04:58

根据规范:

A GraphQL server supports introspection over its schema. This schema is queried using GraphQL itself, creating a powerful platform for tool‐building... The schema introspection system is accessible from the meta‐fields __schema and __type which are accessible from the type of the root of a query operation.

像GraphQL游乐场和GraphiQL这样的工具利用自省来获取关于模式的信息。您不需要任何额外的工具或库来进行自省查询,因为它只是一个GraphQL查询,您将以向端点发出任何其他请求的方式发出请求(例如使用requests)。在

以下是来自graphql-core的完整自省查询:

introspection_query = """
  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
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                }
              }
            }
          }
        }
      }
    }
  }
"""

相关问题 更多 >