@anvilco/ apollo-server-plugin-intertrocetion-metadata (英语)
阿波罗服务器的插件,允许将元数据添加到GraphQL Introvertion 查询响应中.
安装
使用 npm :
npm install --save-prod @anvilco/apollo-server-plugin-introspection-metadata
使用线条 :
yarn add @anvilco/apollo-server-plugin-introspection-metadata
使用量
考虑以下实例:
type MyType {
myField(
myArg: String
): String
}
type Query {
myQuery(
myArg: String
): String
}
type Mutation {
myMutation(
myArg: String
): String
}
interface MyInterface {
id: ID
}
enum MyEnum {
ONE
TWO
THREE
}
当瞬间 ApolloServer,通过下列选项:
import { ApolloServer } = from 'apollo-server'
import IntrospectionMetadataPlugin from '@anvilco/apollo-server-plugin-introspection-metadata'
// All keys are optional, all the way down the object.
// Only build/include what you want to add metadata for :)
const schemaMetadataByKind = {
// Metadata for things of the kind "OBJECT"
OBJECT: {
MyType: {
// Arbitrary metadata about the MyType Object
metadata: {
foo: "bar",
baz: "bop",
}
fields: {
myField: {
// Arbitrary metadata about the myField Field
metadata: {...}
args: {
myArg: {
// Arbitrary metadata about the myArg Argument
metadata: {...}
}
}
}
}
},
MyOtherType: {...},
Query: {
// Queries you've defined end up as Fields in the Queries Type, so this
// structure follows that pattern
fields: {
myQuery: {
// Arbitrary metadata about the myQuery Query
metadata: {...}
args: {
myArg: {
// Arbitrary metadata about the myArg Argument
metadata: {...}
}
}
}
}
},
Mutation: {
// Mutations you've defined end up as Fields in the Mutation Type, so
// this structure follows that pattern
fields: {
myMutation: {
// Arbitrary metadata about the myMutation Mutation
metadata: {...}
args: {
myArg: {
// Arbitrary metadata about the myArg Argument
metadata: {...}
}
}
}
}
},
...
},
// Metadata for things of the kind "INPUT_OBJECT"
INPUT_OBJECT: {
...
},
// Metadata for things of the kind "SCALAR"
SCALAR: {
...
},
// Metadata for things of the kind "ENUM"
ENUM: {
...
},
// Metadata for things of the kind "INTERFACE"
INTERFACE: {
...
},
}
// If you want to only add this metadata in the Development
// environment:
const apolloPlugins = [];
if (process.env.NODE_ENV === 'development') {
apolloPlugins.push(
IntrospectionMetadataPlugin({
schemaMetadata: schemaMetadataByKind
})
)
}
const apolloServer = new ApolloServer({
//...
introspection: true,
plugins: apolloPlugins,
//...
})
进取查询的输出会变成这样:
{
data: {
__schema: {
//...
types: [
{
kind: "OBJECT"
name: "MyType"
metadata: {
foo: "bar",
baz: "bop",
},
fields: [{
//...
name: "myField",
// myField metadata
metadata: {...}
args: [{
//...
name: "myArg",
// myField's myArg metadata
metadata: {...}
}]
}]
},
{
kind: "OBJECT"
name: "Query"
fields: [{
//...
name: "myQuery",
// myQuery metadata
metadata: {...}
args: [{
//...
name: "myArg",
// myQuery's myArg metadata
metadata: {...}
}]
}]
},
{
kind: "OBJECT"
name: "Mutation"
fields: [{
//...
name: "myMutation",
// myMutation metadata
metadata: {...}
args: [{
//...
name: "myArg",
// myMutation's myArg metadata
metadata: {...}
}]
}]
},
//...
]
}
}
}
API 密码
generateApolloPlugin (单位:千美元)default 出口)
这就是你最可能来这里的目的 见 Javadoc 在 指数.js 更多细节.
addMetadata
如果你真的没有使用阿波罗服务器, 或者想用你的元数据 来增强来自别处的内视查询反应, 那么这个功能可能就是你要找的。 见 Javadoc 在 指数.js
isIntrospectionQuery
一个以阿波罗为上下文并返回布尔函数,表示它是否看起来是一个内向查询。 有用,如果你考虑执行自己的 testFn 联合国 generateApolloPlugin 电话.
REGEX_INTROSPECTION_QUERY
用于测试查询字符串的Javascription RegEx,用于确定它是否是内检查询.
参考资料
灵感来自 @ cleartax/ apollo-server-plugin-introvision-auth (英语)。 。 。 .