Skip to content

Conversation

jmfayard
Copy link
Contributor

📝 Description

Helo, I tried and failed to implement a GraphQL directive as explained here
-> https://opensource.expediagroup.com/graphql-kotlin/docs/schema-generator/customizing-schemas/directives#custom-directives

Dariusz Kuc asked me on Slack to provide a sample that shows the issue and here it is.

Reproducing steps:

  • run the :ktor-server:run Gradle task
  • fetch the GraphQL schema

Expected:

"This GraphQL directive allows to add constraints to the GraphQL Schema - https://www.apollographql.com/blog/backend/validation/graphql-validation-using-directives/"
directive @constraint(format: String!, maxLength: Int!, minLength: Int!, pattern: String!) on VARIABLE_DEFINITION | FIELD_DEFINITION | INPUT_OBJECT

input DirectiveInput {
    deviceId: String! @constraint(minLength: 8, maxLength: 16)
    email: String! @constraint(format: "email")
}


type DirectivePayload {
    deviceId: String! @constraint(minLength: 8, maxLength: 16)
    email: String! @constraint(format: "email")
}

Got:

"This GraphQL directive allows to add constraints to the GraphQL Schema - https://www.apollographql.com/blog/backend/validation/graphql-validation-using-directives/"
directive @constraint(format: String!, maxLength: Int!, minLength: Int!, pattern: String!) on VARIABLE_DEFINITION | FIELD_DEFINITION | INPUT_OBJECT

input DirectiveInput {
    deviceId: String!
    email: String!
}


type DirectivePayload {
    deviceId: String!
    email: String!
}

🔗 Related Issues

@dariuszkuc
Copy link
Collaborator

How are you printing the schema? When I updated ktorGraphQLSchema.kt to print it before returning new schema object (and also added ARGUMENT_DEFINITION to the directive target locations)

fun getGraphQLObject(): GraphQL {
    println(graphQLSchema.print()) // print out SDL
    return GraphQL.newGraphQL(graphQLSchema).build()
}

I got following schema printed out (omitted non interesting parts)

"This GraphQL directive allows to add constraints to the GraphQL Schema - https://www.apollographql.com/blog/backend/validation/graphql-validation-using-directives/"
directive @constraint(format: String!, maxLength: Int!, minLength: Int!, pattern: String!) on FIELD_DEFINITION | ARGUMENT_DEFINITION | INPUT_OBJECT | INPUT_FIELD_DEFINITION

type DirectivePayload {
  deviceId: String! @constraint(format : "", maxLength : 16, minLength : 8, pattern : "")
  email: String! @constraint(format : "email", maxLength : 2147483647, minLength : 1, pattern : "")
}

type Mutation {
  directive(input: DirectiveInput!): DirectivePayload!
  login(aliasUUID: String, email: String! @constraint(format : "email", maxLength : 2147483647, minLength : 1, pattern : ""), password: String!): AuthPayload!
}

input DirectiveInput {
  deviceId: String! @constraint(format : "", maxLength : 16, minLength : 8, pattern : "")
  email: String! @constraint(format : "email", maxLength : 2147483647, minLength : 1, pattern : "")
}

@jmfayard
Copy link
Contributor Author

I was using IntelliJ's plugin for GraphQL to generate the schema https://plugins.jetbrains.com/plugin/8097-graphql

This was the issue, this plugin does not print the directive

I added a unit test that re-generates the schema using graphQLSchema.print() and it all works

Thanks a lot for your help @dariuszkuc

@dariuszkuc
Copy link
Collaborator

Pretty sure that plugin (same as most of the tooling) relies on introspection to get the schema (which does not include directive information, see graphql/graphql-spec#300). Generally you will only be able to get the schema with the directives if you explicitly print it out (e.g. in spring-server we expose /sdl endpoint).

jmfayard added a commit to jmfayard/graphql-kotlin that referenced this pull request Jan 26, 2022
Not having the GraphQL schema complete with directives caused me troubles in the issue ExpediaGroup#1350

In spring-server, the `/sdl` endpoint

If this commit is merged, it will be exposed as well in the ktor-server example
@jmfayard
Copy link
Contributor Author

Follow up: #1351

dariuszkuc pushed a commit that referenced this pull request Jan 27, 2022
Not having the GraphQL schema complete with directives caused me troubles in the issue #1350

In spring-server, the `/sdl` endpoint

If this commit is merged, it will be exposed as well in the ktor-server example
dariuszkuc pushed a commit to dariuszkuc/graphql-kotlin that referenced this pull request Aug 5, 2022
Not having the GraphQL schema complete with directives caused me troubles in the issue ExpediaGroup#1350

In spring-server, the `/sdl` endpoint

If this commit is merged, it will be exposed as well in the ktor-server example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants