Apollo-server-azure-functions js library is not supporting constraint-directive

Dear All ,

We are trying to integrate constraint directive based input validation Graph QL server 2. We are using Javascript based Apollo Server, we deployed Apollo Server by using “apollo-server-azure-functions” library in the azure function(javascript) environment. When We try to integrate “apollo-server-azure-functions” library along with “apollo-server-v2-constraint-directive” library, input validation is not working.

But when we try to integrate with “apollo-server” library along with “apollo-server-v2-constraint-directive” library, its working as expected.

So we found like “apollo-server-azure-functions” package is not supporting constraint validation.

Can some one help on this?

Can you please explain the behavior you’re seeing more precisely? Is it doing nothing, is it throwing errors? I’m unfamiliar with the apollo-server-v2-constraint-directive package, and it’s not something that Apollo maintains, so anything you can do to help us understand more would be great.

I’m happy to look into this, but I’ll need a runnable reproduction (clonable github repo, preferably) to do so.

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(:rocket: 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”
}
]
}
}