Thank you for looking in to this… I can give you few piece of working and non working code
You can get azure function based non working code is giving below
Working code
More details are giving below
Below is working Code with apollo-server package######
const { ApolloServer, gql } = require(‘apollo-server’)
const ConstraintDirective = require(‘apollo-server-v2-constraint-directive’)
const {makeExecutableSchema} = require(‘graphql-tools’);
const schemaDirectives = {
constraint: ConstraintDirective,
};
const typeDefs = gql`
scalar ValidateString
scalar ValidateNumber
directive @constraint(
# String constraints
minLength: Int
maxLength: Int
startsWith: String
endsWith: String
notContains: String
pattern: String
format: String
# Number constraints
min: Int
max: Int
exclusiveMin: Int
exclusiveMax: Int
multipleOf: Int
) on INPUT_FIELD_DEFINITION
type Book {
title: String
author: String
}
type Query {
books: [Book],
booksByAuthor(input:BookInput):[Book],
}
input BookInput {
author: String @constraint(minLength: 5, format: “email”)
}
; const books = [ { title: 'The Awakening', author: 'Kate Chopin', }, { title: 'City of Glass', author: 'Paul Auster', }, ]; const resolvers = { Query: { books: () => books, booksByAuthor: (obj, args, context, info) => getBooksByAuthor(obj, args, context, info) }, }; function getBooksByAuthor(obj, args, context, info) { console.log("inside getBooksByAuthor()"); console.log("Author: " + args.input.author); return books; } const server = new ApolloServer({ schema: makeExecutableSchema({ typeDefs, schemaDirectives, resolvers}) }) server.listen().then(({ url }) => { console.log(
Server ready at ${url}`)
})
Not working Code with apollo-server-azure-functions######
const { ApolloServer,gql} = require(‘apollo-server-azure-functions’);
const ConstraintDirective = require(‘apollo-server-v2-constraint-directive’);
const {makeExecutableSchema} = require(‘graphql-tools’);
const schemaDirectives = {
constraint: ConstraintDirective,
};
const typeDefs = gql`
scalar ValidateString
scalar ValidateNumber
directive @constraint(
# String constraints
minLength: Int
maxLength: Int
startsWith: String
endsWith: String
notContains: String
pattern: String
format: String
# Number constraints
min: Int
max: Int
exclusiveMin: Int
exclusiveMax: Int
multipleOf: Int
) on INPUT_FIELD_DEFINITION
type Book {
title: String
author: String
}
type Query {
books: [Book],
booksByAuthor(input:BookInput):[Book],
}
input BookInput {
author: String @constraint(minLength: 5, format: “email”)
}
`;
const books = [
{
title: ‘The Awakening’,
author: ‘Kate Chopin’,
},
{
title: ‘City of Glass’,
author: ‘Paul Auster’,
},
];
const resolvers = {
Query: {
books: () => books,
booksByAuthor: (obj, args, context, info) => getBooksByAuthor(obj, args, context, info)
},
};
function getBooksByAuthor(obj, args, context, info) {
console.log(“inside getBooksByAuthor()”);
console.log("Author: " + args.input.author);
return books;
}
const server = new ApolloServer({ schema: makeExecutableSchema({ typeDefs, schemaDirectives, resolvers})});
exports.graphqlHandler = server.createHandler();
###################################################
If you see both piece of codes(Working and Not working), server will be up and running without any issues in both cases…
But in the first piece of code, the validation is working good as expected… Please see the below request & response of the first code
Request Query:
{
booksByAuthor(input:{author:“Kate@Chopincom”})
{
title
author
}}
Response:
{
“errors”: [
{
“message”: “Expected value of type "ValidateString", found "Kate@Chopincom"; Must be in email format”,
“locations”: [
{
“line”: 2,
“column”: 33
}
],
“extensions”: {
“code”: “GRAPHQL_VALIDATION_FAILED”,
“exception”: {
“stacktrace”: [
“CustomDirectiveError: Must be in email format”
]
}
}
}
]
}
But when we run the second piece of code( based on azure function package) the constraint based validation not getting executed. I just put some dummy data as a response, thats the reason I am getting response, but code shouldn’t reach to that. I am expecting a validation error as like in first code
Request:
{
booksByAuthor(input:{author:“Kate@Chopincom”})
{
title
author
}}
Response:
{
“data”: {
“booksByAuthor”: [
{
“title”: “The Awakening”,
“author”: “Kate Chopin”
},
{
“title”: “City of Glass”,
“author”: “Paul Auster”
}
]
}
}