graphql-constraint-directive
graphql-constraint-directive copied to clipboard
Is there any way to make this library work alongside Apollo federation?
I didn't find a way to have makeExecutableSchema work alongside Apollo-federated. Is there any way to use this library while using Apollo federation?
There are multiple tickets opened with this same question. I also have this question. I've been able to get the federated schema to show the constraint directive in it.
There are no errors at startup and no complaints about my declaration or usage of @constraint in the schema. However, no errors are thrown when testing constraint violations either. It simply doesn't do anything.
I thought I was able to get this working by executing the transform after building the federated schema. This produced all the expected behaviors in terms of constraints. However, I just found that the transform wipes out __resolveReference() functions on resolvers, rendering federated entity type resolution non-operational (#55). I'm back to square one with using this. It would be really nice if contributors could at least acknowledge this ticket in some way.
@robross0606 Sorry for your frustrations, I haven't used Apollo federation. Thanks for looking into it, it would be great if you could share your changes so once https://github.com/ardatan/graphql-tools/issues/2687 is resolved this can subsequently be resolved quickly
@confuser, the example jest test on ardatan/graphql-tools#2687 shows how I execute the transform after building the federated schema. Just replace mapSchema() in that example with the transform of choice. In this case, it would be the constraintDirective:
const gql = require('graphql-tag')
const { constraintDirective, constraintDirectiveTypeDefs } = require('graphql-constraint-directive')
const { mergeTypeDefs } = require('@graphql-tools/merge')
const { buildFederatedSchema } = require('@apollo/federation')
// Add test validation schema
const validatedEntity = gql`
input ValidatedInput {
minString: String @constraint(minLength: 5)
}
`
const federatedEntity = gql`
type FederatedEntity @key(fields: "id") {
id: ID!
properties: [String!]!
}
`
const federatedEntityResolver = {
FederatedEntity: {
__resolveReference: async object => {
object.properties = ['a', 'couple', 'of', 'properties']
return object
}
}
}
const schema = buildFederatedSchema([
{
typeDefs: mergeTypeDefs([federatedEntity, validatedEntity, constraintDirectiveTypeDefs]),
resolvers: federatedEntityResolver
}
])
// Constraints transform
const mappedSchema = constraintDirective(schema)
const server = new ApolloServer({ schema: mappedSchema })
...
I believe this is now resolved with the latest v4 release
I went to go try this and noticed this in the README:
For GraphQL v15 and below, use v2 of this package
npm install graphql-constraint-directive@v2
This means using the input validation plugin instead of schema wrapper is only compatible with GraphQL 16? Is that true?