簡體   English   中英

在 Apollo Express GraphQL 中出現錯誤:錯誤:模式必須包含唯一命名的類型,但包含多個名為“DateTime”的類型

[英]Getting error in Apollo Express GraphQL: Error: Schema must contain uniquely named types but contains multiple types named "DateTime"

我正在嘗試使用import { applyMiddleware } from 'graphql-middleware'; 庫以在突變的輸入上添加驗證中間件。

所以,我創建了一個示例中間件 function 這是日志輸入

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的文檔,將現有的schemasmiddlewares傳遞給由graphql-middleware庫提供的applyMiddleware()

graphql/index.js文件包含:因此該文件包含結合所有schemastypesresolvers的代碼。

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 }),
    });

每當我嘗試將schemaapplyMiddleware()一起使用時,當我嘗試像這樣直接使用它時會引發錯誤,它可以正常工作。

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'; package。

從架構中刪除后,它開始工作。

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,
});


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM