gpt4 book ai didi

python - 如何使用 Python 获取 GraphQL 模式?

转载 作者:太空宇宙 更新时间:2023-11-04 00:07:14 25 4
gpt4 key购买 nike

有很多 GUI 客户端,如 GraphQL Playground、GraphiQl 等,能够从 URL 获取 GraphQL 模式。如何使用 Python 获取模式?

最佳答案

来自规范:

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 Playground 和 GraphiQL 等工具利用内省(introspection)来获取有关模式的信息。您不需要任何其他工具或库来进行内省(introspection)查询——因为它只是一个 GraphQL 查询,您将以与向端点发出任何其他请求相同的方式发出请求(使用 requests 例如)。

这是来自 graphql-core 的完整内省(introspection)查询:

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
}
}
}
}
}
}
}
}
"""

关于python - 如何使用 Python 获取 GraphQL 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com