Have defined a custom directives in the directives.graphql file in the schema:
directive @reference on FIELD
Have also passed the .graphql file in my GraphQLConfig.java class:
private static final String[] SCHEMA_FILES = new String[]{"schema/root.graphql","schema/directives.graphql","schema/train-details.graphql"};
root.graphql is as below:
schema{ query: Query}
type Query{ trainDetails( trainId: String!): TrainDetail}
train-details.graphql is as below:
type TrainDetail{ trainId: String trainStatus: String}
In the passed request to the springboot graphql API, I want to check if the directive @reference is passed then perform some business logic in the code. The schema which is generated during server start up in Java has the directive present in it but when I extract the directive from the passed query in the request it is not present.
query passed in request, url (starting server locally)
http://localhost:8080/graphql
GraphQL query:
query($trainId: String!){trainDetails(trainId: $trainId){trainStatus @reference}}
Query Variables:
{ "trainId": "123456"}
dataFecther(which has been added in graphql config class as well) in which checking the directives in the passed request:
public DataFetcher getTrainDetailsDataFecther(){ return environment -> trainDetailsFetcher.getTrainDetails.getTrainDetails(environment);}
getTrainDetails method:
public TrainDetails getTrainDetails(DataFectchingEnvironment environment){ if(environment.getQueryDirectives().getImmediateDirective("reference").isEmpty()){//perform some logic}}
The if check is always returning false, even when @reference is passed in the request query as a directive. It is not recognizing that a directive has been passed on FIELD in the query.
Please note that this is directive is On FIELD and not on FIELD DEFINITION, hence not added the directive in train-details.graphql as a definition in the schema.