Apollo GraphQL Server 中的标量指令
Directives on scalars in Apollo GraphQL Server
我在我的架构中将日期定义为标量。
有没有办法只在一个地方定义有关合法值的规则?
我或许可以在 FIELD_DEFINITION 上定义一个指令,并将其应用于该类型的每个字段。但是,我想做一次。这可能吗?
这是我的代码。 CitizenshipCode 实际上是一个字符串,仅限于“us”、“uk”、“de”等值。我想以集中的方式实现验证,而不是为这种类型的每个字段添加指令:
scalar CitizenshipCode # is it possible to implement the validation here?
type Client {
id: ID!
name: String
citizenship: CitizenshipCode # instead of here...
# other atts
}
type User {
id: ID!
email: String
citizenship: CitizenshipCode # ... and here
# other atts
}
也许您可以在代码中使用枚举类型或使用自定义架构:
const { ApolloServer, gql, SchemaDirectiveVisitor } = require('apollo-server');
const { GraphQLScalarType, Kind } = require('graphql');
const validCountryCodes = [ "US", "UK", "DE"];
const citizenshipCodeScalar = new GraphQLScalarType({
name: 'CitizenshipCode',
description: 'Allowed citizen ship codes',
serialize(value) {
return validCountryCodes.includes(value) ? value : "INVALID COUNTRY";
},
parseValue(value) {
return validCountryCodes.includes(value) ? value : null;
},
parseLiteral(ast) {
return ast.kind === Kind.STRING ? ast.value : null;
}
});
const typeDefs = gql `
scalar CitizenshipCode
type Client {
id: ID
name: String
citizenship: CitizenshipCode
}
type User {
id: Int
email: String
citizenship: CitizenshipCode
}
type Query {
clients: [Client]
users: [User]
}
`;
const resolvers = {
Query: {
clients: () => clients,
users: () => users
},
CitizenshipCode: citizenshipCodeScalar
};
const client = [{
id: 'client1',
name: 'The Awakening',
citizenship: 'US'
},
{
id: 'client2',
title: 'City of Glass',
citizenship: 'CA',
},
];
const users = [{
id: 1,
email: 'test1@test.com',
citizenship: 'UK',
},
{
id: 2,
email: 'test2@test.com',
citizenship: 'DE',
},
];
const server = new ApolloServer({
typeDefs,
resolvers
});
我在我的架构中将日期定义为标量。
有没有办法只在一个地方定义有关合法值的规则?
我或许可以在 FIELD_DEFINITION 上定义一个指令,并将其应用于该类型的每个字段。但是,我想做一次。这可能吗?
这是我的代码。 CitizenshipCode 实际上是一个字符串,仅限于“us”、“uk”、“de”等值。我想以集中的方式实现验证,而不是为这种类型的每个字段添加指令:
scalar CitizenshipCode # is it possible to implement the validation here?
type Client {
id: ID!
name: String
citizenship: CitizenshipCode # instead of here...
# other atts
}
type User {
id: ID!
email: String
citizenship: CitizenshipCode # ... and here
# other atts
}
也许您可以在代码中使用枚举类型或使用自定义架构:
const { ApolloServer, gql, SchemaDirectiveVisitor } = require('apollo-server');
const { GraphQLScalarType, Kind } = require('graphql');
const validCountryCodes = [ "US", "UK", "DE"];
const citizenshipCodeScalar = new GraphQLScalarType({
name: 'CitizenshipCode',
description: 'Allowed citizen ship codes',
serialize(value) {
return validCountryCodes.includes(value) ? value : "INVALID COUNTRY";
},
parseValue(value) {
return validCountryCodes.includes(value) ? value : null;
},
parseLiteral(ast) {
return ast.kind === Kind.STRING ? ast.value : null;
}
});
const typeDefs = gql `
scalar CitizenshipCode
type Client {
id: ID
name: String
citizenship: CitizenshipCode
}
type User {
id: Int
email: String
citizenship: CitizenshipCode
}
type Query {
clients: [Client]
users: [User]
}
`;
const resolvers = {
Query: {
clients: () => clients,
users: () => users
},
CitizenshipCode: citizenshipCodeScalar
};
const client = [{
id: 'client1',
name: 'The Awakening',
citizenship: 'US'
},
{
id: 'client2',
title: 'City of Glass',
citizenship: 'CA',
},
];
const users = [{
id: 1,
email: 'test1@test.com',
citizenship: 'UK',
},
{
id: 2,
email: 'test2@test.com',
citizenship: 'DE',
},
];
const server = new ApolloServer({
typeDefs,
resolvers
});