Reputation: 1489
I am trying to use import { applyMiddleware } from 'graphql-middleware';
library to add validation middleware on mutation's input.
So, I created a sample middleware function which is log input
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;
};
Now I as per the documentation of graphql-middleware
, pass existing schemas
and middlewares
to applyMiddleware()
which is provided by graphql-middleware
library.
graphql/index.js
file contains: So this file contains code that combines all schemas
, types
and 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,
});
Sample file which holding all types
, there are many files to handle other resources
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
file contains:
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 }),
});
Whenever I try to use schema
with applyMiddleware()
throws an error when I try to use it directly like this, it work without any issues.
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 }),
});
Error throwing:
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.
I searched over the internet, but I couldn't able to find a solution.
Upvotes: 2
Views: 1835
Reputation: 1489
It's strange but the problem was with this import { GraphQLDateTime } from 'graphql-iso-date';
package.
After removing it from the schema, it started working.
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,
});
Upvotes: 2