javascript - Apollo Express GraphQL 中出现错误 : Error: Schema must contain uniquely named types but contains multiple types named "DateTime"

标签 javascript node.js express graphql apollo-server

我正在尝试使用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 的文档,通过现有schemasmiddlewaresapplyMiddleware()graphql-middleware 提供图书馆。

graphql/index.js文件包含:因此该文件包含组合所有 schemas 的代码, typesresolvers .

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.

我在互联网上搜索,但找不到解决方案。

最佳答案

这很奇怪,但问题出在这个 import { GraphQLDateTime } from 'graphql-iso-date'; 包上。

将其从架构中删除后,它开始工作。

import { gql, makeExecutableSchema } from 'apollo-server-express';
import { merge } from 'lodash';
import { GraphQLJSONObject } from 'graphql-type-json';
import { policyType, policyResolver, policySchema } from './policy';

import {
    gitProviderTypes,
    gitProviderResolver,
    gitProviderSchema,
} from './gitProvider';

const Root = gql`
    scalar JSON
    scalar JSONObject

    type MyType {
        myValue: JSON
        myObject: JSONObject
    }

    type Query {
        _empty: String
    }
    type Mutation {
        _empty: String
    }
    schema {
        query: Query
        mutation: Mutation
    }
`;

const resolvers = merge(
    { JSONObject: GraphQLJSONObject },
    policyResolver,
    gitProviderResolver
);

const typeDefs = [
    Root,
    policyType,
    policySchema,
    gitProviderTypes,
    gitProviderSchema,
];

export default makeExecutableSchema({
    typeDefs,
    resolvers,
});


关于javascript - Apollo Express GraphQL 中出现错误 : Error: Schema must contain uniquely named types but contains multiple types named "DateTime",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67866823/

相关文章:

node.js - 为什么 setImmediate() 比顺序代码运行得更快?

node.js - 勒纳致命 : ambiguous argument 'origin/heads/origin/master...heads/origin/master' : unknown revision or path not in the working tree

javascript - 在 express 之外的脚本中从 express 应用程序导入模块时出现问题

javascript - 如何使用此脚本创建多个插入文本区域的按钮

javascript - Flexbox 文本切断/不换行

javascript - 将 gulp@3.9.1 中的 gulp watch 转换为 gulp@4

javascript - Socket.io 不理解用户名

javascript - 更改散点图中标记的宽度和高度

javascript - 使用nodemon无法识别Electron包

javascript - nodejs/ejs 包含部分语法错误