如何在 apollo 中使用自定义指令 graphql-modules
How to use custom directives graphql-modules with apollo
我在使用 graphql-modules 库时需要有关自定义指令的帮助。不知道在哪里放置我的自定义指令,所以它与整体模式结合
我想 post 来自用户 Maapteh 的 Discord 社区的回答。
这里引用他的话
in our app with old version we had everything in common module. We
kept that approach partly when using the latest version of modules.
See some parts of our codebase:
import { ApolloServer, SchemaDirectiveVisitor } from
'apollo-server-express';
const schema = AppModule.createSchemaForApollo();
SchemaDirectiveVisitor.visitSchemaDirectives(schema, {
isMember: IsMemberDirective,
deprecated: isDeprecated,
...SNIP... });
as you can see we create the schema we pass eventually to the apollo
server (example using apollo). We add our generic directives like so.
The schema for them is in our common module. Read further...
For common scalars we added a common module. With their schema (so in
schema directory we also have the directives schemas) and their
resolver.
const typeDefsArray = loadFilesSync(${__dirname}/schema/*.graphql
, {
useRequire: true, }); const typeDefs = mergeTypeDefs(typeDefsArray, { useSchemaDefinition: false });
const resolverFunctions = {
ImageUrl: ImageUrlType,
PageSize: PageSizeType,
Date: DateResolver,
...SNIP... };
export const CommonModule = createModule({
id: 'common',
typeDefs: typeDefs,
resolvers: resolverFunctions, });
hope it helps you
https://discord.com/channels/625400653321076807/631489837416841253/832595211166548049
我在使用 graphql-modules 库时需要有关自定义指令的帮助。不知道在哪里放置我的自定义指令,所以它与整体模式结合
我想 post 来自用户 Maapteh 的 Discord 社区的回答。
这里引用他的话
in our app with old version we had everything in common module. We kept that approach partly when using the latest version of modules. See some parts of our codebase:
import { ApolloServer, SchemaDirectiveVisitor } from 'apollo-server-express';
const schema = AppModule.createSchemaForApollo();
SchemaDirectiveVisitor.visitSchemaDirectives(schema, { isMember: IsMemberDirective, deprecated: isDeprecated, ...SNIP... });
as you can see we create the schema we pass eventually to the apollo server (example using apollo). We add our generic directives like so. The schema for them is in our common module. Read further...
For common scalars we added a common module. With their schema (so in schema directory we also have the directives schemas) and their resolver.
const typeDefsArray = loadFilesSync(
${__dirname}/schema/*.graphql
, { useRequire: true, }); const typeDefs = mergeTypeDefs(typeDefsArray, { useSchemaDefinition: false });const resolverFunctions = { ImageUrl: ImageUrlType, PageSize: PageSizeType, Date: DateResolver, ...SNIP... };
export const CommonModule = createModule({ id: 'common', typeDefs: typeDefs, resolvers: resolverFunctions, });
hope it helps you
https://discord.com/channels/625400653321076807/631489837416841253/832595211166548049