การเสริมขยาย Schemaการขยาย Schema ผ่าน Introspection
การขยาย Schema ผ่าน Introspection
เมทาดาต้าที่กำหนดเองซึ่งแนบกับองค์ประกอบของ schema สามารถ query ได้ผ่านฟิลด์ extensions เมื่อทำ introspection
องค์ประกอบ introspection ทั้งหมดของ schema จะ implement ฟิลด์นี้ โดยแต่ละองค์ประกอบจะคืนค่าออบเจกต์ของ type "Extensions" ที่สอดคล้องกัน ซึ่ง expose คุณสมบัติที่กำหนดเองสำหรับองค์ประกอบนั้น
GraphQL schema มีลักษณะดังนี้:
# Using "_" instead of "__" in introspection type name to avoid errors in graphql-js
type _SchemaExtensions {
# Is the schema being namespaced?
isNamespaced: Boolean!
}
extend type __Schema {
extensions: _SchemaExtensions!
}
type _NamedTypeExtensions {
# The type name
elementName: String!
# The "namespaced" type name
namespacedName: String!
# Enum-like "possible values" for EnumString type resolvers, `null` otherwise
possibleValues: [String!]
# OneOf Input Objects are a special variant of Input Objects where the type system asserts that exactly one of the fields must be set and non-null, all others being omitted.
isOneOf: Boolean!
}
extend type __Type {
# Non-null for named types, null for wrapping types (Non-Null and List)
extensions: _NamedTypeExtensions
}
type _DirectiveExtensions {
# If no objects are returned in the field (eg: because they failed validation), does the directive still need to be executed?
needsDataToExecute: Boolean!
# Names or descriptions of the types the field directives is restricted to, or `null` if it supports any type (i.e. it defines no restrictions)
fieldDirectiveSupportedTypeNamesOrDescriptions: [String!]
}
extend type __Directive {
extensions: _DirectiveExtensions!
}
type _FieldExtensions {
# Useful for nested mutations
isMutation: Boolean!
# `true` => Only exposed when "Expose "sensitive" data elements" is enabled
isSensitiveDataElement: Boolean!
}
extend type __Field {
extensions: _FieldExtensions!
}
type _InputValueExtensions {
isSensitiveDataElement: Boolean!
}
extend type __InputValue {
extensions: _InputValueExtensions!
}
type _EnumValueExtensions {
isSensitiveDataElement: Boolean!
}
extend type __EnumValue {
extensions: _EnumValueExtensions!
}วิธีใช้งาน
GraphQL introspection query ต่อไปนี้แสดงให้เห็นถึงคุณสมบัติที่มีอยู่ในแต่ละฟิลด์ extensions:
query ExtensionsIntrospectionQuery {
__schema {
extensions {
isNamespaced
}
types {
name
extensions {
elementName
namespacedName
possibleValues
isOneOf
}
fields {
name
extensions {
isMutation
isSensitiveDataElement
}
args {
name
extensions {
isSensitiveDataElement
}
}
}
inputFields {
name
extensions {
isSensitiveDataElement
}
}
enumValues {
name
extensions {
isSensitiveDataElement
}
}
}
directives {
name
extensions {
needsDataToExecute
fieldDirectiveSupportedTypeNamesOrDescriptions
}
args {
name
extensions {
isSensitiveDataElement
}
}
}
}
}ข้อกำหนด GraphQL
ฟังก์ชันนี้ยังไม่เป็นส่วนหนึ่งของข้อกำหนด GraphQL ในปัจจุบัน แต่มีการร้องขอไว้ใน: