GraphQL 自定义指令强制执行值限制
GraphQL Custom directive enforcing value restrictions
我需要在 INPUT_FIELD_DEFINITION 上创建一个自定义指令,以检查提供的枚举值是否未更改为之前的“状态”(业务逻辑是状态必须为未批准 - > 已批准-> 已取消 -> 已完成)但不太清楚如何在枚举类型的构造函数中映射值。
我的所有代码都可以在 github
我正在使用 nextJs 后端功能和 neo4j 数据库,为整个模式生成解析器。
// order Schema
export const order = gql`
type Order {
id: ID! @id
state: OrderState!
user: User! @relationship(type: "MADE", direction: IN)
createdAt: DateTime! @timestamp(operations: [CREATE])
products: [Product!] @relationship(type: "INCLUDE", direction: OUT)
}
enum OrderState {
UNAPPROVED
APPROVED
CANCELLED
FULFILLED
}
`;
export const extendOrder = gql`
extend input OrderCreateInput {
state: OrderState!
}
`;
我想创建 @checkState
指令来检查更新 state
是否有效
我使用了 GraphQL Tools docs 中的基本示例,但它包含字符串值。如果有任何帮助,我将不胜感激。
我没有使用自定义指令,而是使用 graphql-middleware
库来创建仅在使用 updateOrders 突变时触发的中间件。
Middleware
import { ValidationError } from "apollo-server-micro";
import { IMiddleware } from "graphql-middleware";
import { Order } from "pages/api/graphql";
export const checkStateMiddleware: IMiddleware = {
Mutation: {
updateOrders: async (resolve, parent, args, ctx, info) => {
const { where, update } = args;
const [existing] = await Order.find({
...where,
});
const states = ["UNAPPROVED", "APPROVED", "CANCELLED", "FULLFIELD"];
const currentState = states.indexOf(existing.state);
const toBeUpdatedState = states.indexOf(update.state);
if (toBeUpdatedState < currentState) {
throw new ValidationError("Can't update value with previous state.");
}
return resolve(parent, args);
},
},
};
然后我将它应用到 pages/api/graphql.ts
//...
import { applyMiddleware } from "graphql-middleware";
//...
const schemaWithMiddleware = applyMiddleware(schema, checkStateMiddleware);
const apolloServer = new ApolloServer({ schema: schemaWithMiddleware });
//...
我需要在 INPUT_FIELD_DEFINITION 上创建一个自定义指令,以检查提供的枚举值是否未更改为之前的“状态”(业务逻辑是状态必须为未批准 - > 已批准-> 已取消 -> 已完成)但不太清楚如何在枚举类型的构造函数中映射值。
我的所有代码都可以在 github
我正在使用 nextJs 后端功能和 neo4j 数据库,为整个模式生成解析器。
// order Schema
export const order = gql`
type Order {
id: ID! @id
state: OrderState!
user: User! @relationship(type: "MADE", direction: IN)
createdAt: DateTime! @timestamp(operations: [CREATE])
products: [Product!] @relationship(type: "INCLUDE", direction: OUT)
}
enum OrderState {
UNAPPROVED
APPROVED
CANCELLED
FULFILLED
}
`;
export const extendOrder = gql`
extend input OrderCreateInput {
state: OrderState!
}
`;
我想创建 @checkState
指令来检查更新 state
是否有效
我使用了 GraphQL Tools docs 中的基本示例,但它包含字符串值。如果有任何帮助,我将不胜感激。
我没有使用自定义指令,而是使用 graphql-middleware
库来创建仅在使用 updateOrders 突变时触发的中间件。
Middleware
import { ValidationError } from "apollo-server-micro";
import { IMiddleware } from "graphql-middleware";
import { Order } from "pages/api/graphql";
export const checkStateMiddleware: IMiddleware = {
Mutation: {
updateOrders: async (resolve, parent, args, ctx, info) => {
const { where, update } = args;
const [existing] = await Order.find({
...where,
});
const states = ["UNAPPROVED", "APPROVED", "CANCELLED", "FULLFIELD"];
const currentState = states.indexOf(existing.state);
const toBeUpdatedState = states.indexOf(update.state);
if (toBeUpdatedState < currentState) {
throw new ValidationError("Can't update value with previous state.");
}
return resolve(parent, args);
},
},
};
然后我将它应用到 pages/api/graphql.ts
//...
import { applyMiddleware } from "graphql-middleware";
//...
const schemaWithMiddleware = applyMiddleware(schema, checkStateMiddleware);
const apolloServer = new ApolloServer({ schema: schemaWithMiddleware });
//...