Rename Argument using Directive #2291
Replies: 3 comments 24 replies
-
| do you mean something like that?
the logic behind @fetch is still implemented in graphql-java, but you must "activate" it/make-it-accessable by writing the directive statement explicit into your graphql-schema like in that code-example here, see line 1 take a look here: https://www.graphql-java.com/documentation/v16/data-fetching/ |
Beta Was this translation helpful? Give feedback.
-
| Rename works onField of SchemaDirectiveWiring and has Datafetcher. But I need an example, how i could use the datafetcher for onArgument method. So that the client doesn't' need to know about the field name that app can use internally, Please share an example or show me how to get that working like onField method. When I call, dataFetchingEnvironment.getArguments(), need the field with replaced field name and the values. |
Beta Was this translation helpful? Give feedback.
-
public class GraphQLTypeVisitorStubWithCodeRegistry extends GraphQLTypeVisitorStub { @Getter protected GraphQLCodeRegistry.Builder graphQLCodeRegistryBuilder; @Getter protected GraphQLSchema graphQLSchema; public void registerCodeRegistryBuilder(GraphQLCodeRegistry.Builder graphQLCodeRegistryBuilder) { this.graphQLCodeRegistryBuilder = graphQLCodeRegistryBuilder; } public void registerGraphQLSchema(GraphQLSchema graphQLSchema) { this.graphQLSchema = graphQLSchema; } }public class ArgumentsDirectiveVisitor extends GraphQLTypeVisitorStubWithCodeRegistry { @Override public TraversalControl visitGraphQLFieldDefinition(GraphQLFieldDefinition fieldDefinition, TraverserContext<GraphQLSchemaElement> context) { GraphQLFieldsContainer parentContainerType = (GraphQLFieldsContainer) context.getParentContext().thisNode(); newDataFetcher = (dfe) ->{ final Map<String, Object> originArguments = dfe.getArguments(); final GraphQLContext dfeContext = (GraphQLContext) dfe.getContext(); final LinkedHashMap<String, Object> mySpecialArguments = dfeContext.getOrDefault("mySpecialArguments", new LinkedHashMap<>()); mySpecialArguments.put("...","..."); //do something with the originArguments related to the directives return this.graphQLCodeRegistryBuilder.getDataFetcher(parentContainerType, fieldDefinition).get(dfe); }; this.graphQLCodeRegistryBuilder.dataFetcher(coordinates(parentContainerType, fieldDefintion), newDataFetcher); }So its some code i use changed a little bit to give you an idea, so briefly im not sure which exactly method you must override of GraphQLTypeVisitorStub, but it should show you how you can put into it a datafetcher interceptor, the important code is the newDataetcher-lambda and how you can register it in the registry, for that you must inject the registry..... because i use different visitors in my code i putted it into a abstract parent class. The registry you get nearly on that location where you boostrap up your graphql engine, i think you will find it...... ok moment i give you some code: SchemaTransformer schemaTransformer = new SchemaTransformer(); GraphQLCodeRegistry.Builder codeRegistryBuilder = newCodeRegistry(graphQLSchema.getCodeRegistry()); for (GraphQLTypeVisitorStubWithCodeRegistry value : this.graphQLTypeVisitor) { value.registerCodeRegistryBuilder(codeRegistryBuilder); value.registerGraphQLSchema(graphQLSchema); graphQLSchema = schemaTransformer.transform(graphQLSchema, value); } graphQLSchema = graphQLSchema.transform(builder -> { builder.codeRegistry(codeRegistryBuilder.build()); }); this.graphQL = GraphQL.newGraphQL(graphQLSchema).build(); return this;the graphQLSchema itself i need for something other in my code, that you dont need |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi all, I am looking for rename directive using graphql Java. Is there a directive available that I can use?
Beta Was this translation helpful? Give feedback.
All reactions