graphql-constraint-directive icon indicating copy to clipboard operation
graphql-constraint-directive copied to clipboard

Is there any way to make this library work alongside Apollo federation?

Open grillorafael opened this issue 5 years ago • 4 comments

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?

grillorafael avatar Aug 03 '20 17:08 grillorafael

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.
image 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.

robross0606 avatar Aug 25 '20 13:08 robross0606

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 avatar Mar 03 '21 23:03 robross0606

@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 avatar Mar 09 '21 12:03 confuser

@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 })
...

robross0606 avatar Mar 10 '21 15:03 robross0606

I believe this is now resolved with the latest v4 release

confuser avatar Sep 05 '22 20:09 confuser

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?

robross0606 avatar Jan 27 '23 18:01 robross0606