【发布时间】:2021-08-24 06:06:45
【问题描述】:
我正在尝试使用import { applyMiddleware } from 'graphql-middleware'; 库在突变的输入上添加验证中间件。
所以,我创建了一个示例中间件函数,它是日志输入
export const logInput = async (resolve, root, args, context, info) => {
console.log(`1. logInput: ${JSON.stringify(args)}`);
const result = await resolve(root, args, context, info);
console.log(`5. logInput`);
return result;
};
现在我根据graphql-middleware 的文档,将现有的schemas 和middlewares 传递给applyMiddleware(),由graphql-middleware 库提供。
graphql/index.js 文件包含:因此该文件包含结合所有schemas、types 和resolvers 的代码。
import { gql, makeExecutableSchema } from 'apollo-server-express';
import { merge } from 'lodash';
import { GraphQLJSONObject } from 'graphql-type-json';
import { GraphQLDateTime } from 'graphql-iso-date';
import { policyType, policyResolver, policySchema } from './policy';
import {
gitProviderTypes,
gitProviderResolver,
gitProviderSchema,
} from './gitProvider';
const Root = gql`
scalar JSON
scalar JSONObject
scalar GraphQLDateTime
type MyType {
myValue: JSON
myObject: JSONObject
myDate: GraphQLDateTime
}
type Query {
_empty: String
}
type Mutation {
_empty: String
}
schema {
query: Query
mutation: Mutation
}
`;
const resolvers = merge(
{ JSONObject: GraphQLJSONObject, GraphQLDateTime },
policyResolver,
gitProviderResolver
);
export default makeExecutableSchema({
typeDefs: [
Root,
policyType,
policySchema,
gitProviderTypes,
gitProviderSchema,
],
resolvers,
});
包含所有types的示例文件,还有很多文件可以处理其他资源
import { gql } from 'apollo-server-express';
export default gql`
type CreatePolicyResult {
id: String
name: String
adopted: Boolean
markdown: String
}
type CreateProcedureResult {
id: String
type: String
name: String
file: String
provider: String
adopted: Boolean
summary: String
guidance: String
applicable: Boolean
}
type Policy {
_id: ID
id: String
name: String
adopted: Boolean
tags: [String]
procedures: [Procedure]
markdown: String
html: String
file: String
}
type Procedure {
_id: ID
id: String
type: String
name: String
summary: String
applicable: String
provider: String
guidance: String
adopted: String
tags: [String!]
markdown: String
html: String
file: String
}
input ProcedureInput {
id: String
type: String
name: String
summary: String
applicable: Boolean
provider: String
guidance: String
adopted: Boolean
tags: [String]
markdown: String
}
input CreateProcedureInput {
id: String!
type: String!
name: String!
markdown: String!
provider: String!
adopted: Boolean!
summary: String
guidance: String
applicable: Boolean!
}
input PolicyInput {
id: String!
name: String!
adopted: Boolean!
markdown: String!
}
input UpdatePolicyInput {
id: String
name: String
adopted: Boolean
tags: [String]
markdown: String
}
input OrganizationInput {
companyFullName: String!
companyShortName: String!
companyEmailDomain: String!
companyWebsiteURL: String!
securityOfficerName: String!
securityOfficerEmail: String!
ctoName: String!
ctoEmail: String!
sourceControl: String!
ticketingSystem: String!
ciSystem: String!
privacyPolicyURL: String!
supportBYODandMDM: Boolean!
}
`;
express.js 文件包含:
import { ApolloServer } from 'apollo-server-express';
import { applyMiddleware } from 'graphql-middleware';
import schema from './graphql';
import { logInput } from './graphql/middlewares';
const schemaWithMiddleware = applyMiddleware(schema, logInput);
//Here schema is imported from file and logInput is middleware
//GraphQL Server
const server = new ApolloServer({
schema,
context: async ({ req, res }) => ({ req, res }),
});
每当我尝试将schema 与applyMiddleware() 一起使用时,当我尝试像这样直接使用它时会抛出错误,它可以正常工作。
import { ApolloServer } from 'apollo-server-express';
import { applyMiddleware } from 'graphql-middleware';
import schema from './graphql';
import { logInput } from './graphql/middlewares';
//Not using this time, now works without any problem
//const schemaWithMiddleware = applyMiddleware(schema, logInput);
//GraphQL Server
const server = new ApolloServer({
schema: schema,
context: async ({ req, res }) => ({ req, res }),
});
错误抛出:
node:14152) UnhandledPromiseRejectionWarning: Error: Schema must contain uniquely named types but contains multiple types named "DateTime".
at new GraphQLSchema (G:\nanoheal\tego\policy-builder-service\node_modules\graphql\type\schema.js:194:15)
at Object.mapSchema (G:\nanoheal\tego\policy-builder-service\dist\utils\src\mapSchema.js:31:12)
at createNewSchemaWithResolvers (G:\nanoheal\tego\policy-builder-service\dist\schema\src\addResolversToSchema.js:200:14)
at Object.addResolversToSchema (G:\nanoheal\tego\policy-builder-service\dist\schema\src\addResolversToSchema.js:87:11)
at addMiddlewareToSchema (G:\nanoheal\tego\policy-builder-service\node_modules\graphql-middleware\src\middleware.ts:42:21)
at normalisedMiddlewares.reduceRight.schema.schema (G:\nanoheal\tego\policy-builder-service\node_modules\graphql-middleware\src\middleware.ts:91:11)
at Array.reduceRight (<anonymous>)
at applyMiddlewareWithOptions (G:\nanoheal\tego\policy-builder-service\node_modules\graphql-middleware\src\middleware.ts:80:77)
at Object.applyMiddleware (G:\nanoheal\tego\policy-builder-service\node_modules\graphql-middleware\src\middleware.ts:132:10)
at Object.exports.default (G:\nanoheal\tego\policy-builder-service\src\loaders\express.ts:28:34)
at Object.exports.default (G:\nanoheal\tego\policy-builder-service\src\loaders\index.ts:14:24)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at startServer (G:\nanoheal\tego\policy-builder-service\src\server.ts:16:5)
(node:14152) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
(node:14152) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我在互联网上搜索,但找不到解决方案。
【问题讨论】:
-
错误指向
mapSchema.js第31行。这个文件是否在问题中,如果是,它是什么? -
在我的项目中没有名为
mapSchema.js的文件,我想,这在某个库中的某个地方 -
该文件位于 dist/utils/src 文件夹中,而不是 node_modules 文件夹中。这些文件来自哪里?他们可能编译了 TypeScript 吗? dist/schema/src/addResolversToSchema.js 正在引用该文件。
-
是的,我在这里使用 TypeScript,但在这里我没有看到任何
build文件夹。我正在使用 Nodemon 进行开发 -
您能否发布
dist/utils/src/mapSchema.js文件的代码,至少在第31 行?还有dist\schema\src\addResolversToSchema.js的第200行?
标签: javascript node.js express graphql apollo-server