You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
We have a use case where we have an API Gateway that will be stitching multiple microservices into one schema. As part of this, we would like to automatically remove certain field arguments (based on a naming convention, in the future potentially a marker directive) from the microservice schema and automatically inject these fields using ContextData from the API Gateway eg:
publicclassContextDataMiddleware{privateconststringContextUserIdFieldName="userId";// The fields we want to replace with ContextData and not exposepublicstaticreadonlystring[]ContextFieldNames={ContextUserIdFieldName};publicstaticFieldMiddlewareMiddleware=> next =>async context =>{context.ContextData[ContextUserIdFieldName]=5;awaitnext.Invoke(context);};}
Then rewrite any field with that argument ("userId") with a delegation directive and remove the field from the exposed API eg.
publicclassContextDataArgumentRewriter:ITypeRewriter{privatereadonlystring_schema;privatereadonlyHashSet<string>_contextDataFields;publicContextDataArgumentRewriter(stringschema,paramsstring[]contextDataFields){_schema=schema;_contextDataFields=newHashSet<string>(contextDataFields);}publicITypeDefinitionNodeRewrite(ISchemaInfoschema,ITypeDefinitionNodetypeDefinition){if(!(typeDefinitionisObjectTypeDefinitionNodeobjectTypeDefinition))returntypeDefinition;varfields=newList<FieldDefinitionNode>();foreach(varfieldinobjectTypeDefinition.Fields){varcontextDataArguments=field.Arguments.Where(x =>_contextDataFields.Contains(x.Name.Value)).ToList();if(!contextDataArguments.Any()){fields.Add(field);continue;}varnormalArguments=field.Arguments.Except(contextDataArguments).ToList();varnormalArgumentNodes=normalArguments.Select(
x =>newArgumentNode(x.Name,newScopedVariableNode(null,newNameNode(ScopeNames.Arguments),x.Name)));varcontextDataArgumentNodes=contextDataArguments.Select(
x =>newArgumentNode(x.Name,newScopedVariableNode(null,newNameNode(ScopeNames.ContextData),x.Name)));varpath=newSelectionPathComponent(field.Name,normalArgumentNodes.Concat(contextDataArgumentNodes).ToList());fields.Add(field// Doesn't seem to add the delegate directive in the resultant schema.AddDelegationPath(_schema,path)// Ensure we exclude these fields we are now filling.WithArguments(normalArguments));}returnobjectTypeDefinition.WithFields(fields);}}
Even after using the new AddDelegationPath method:
...field.AddDelegationPath(_schema,path)
...
The resultant schema does not seem to have the field with the directive applied, and does not show in GraphQL Playground / Graphiql (may be a seperate issue?):
Running a query where we expect the field to be filled with ContextData and sent to the microservice does not seem to work.
Run both the ApiGateway and Microservice projects concurrently.
Notice Microservice has a "userId" argument, and ApiGateway does not, for the "userFeed" field.
Run a query like:
{
userFeed(limit: 5) {
cursor
}
}
Notice you will receive an error: ""The argument userId is required and does not allow null values." and the resultant schema does show the delegate on the field argument.
Expected behavior
The Delegate directive should be applied and queries where the delegate is used should automatically fill with the context data when sent to the stitched service.
Desktop (please complete the following information):
As a side note, I don't think custom directives are outputted on fields/arguments via introspection as per: graphql/graphql-spec#300, that might be why Playground/ graphiql are not showing the delegate directive there.
Describe the bug
We have a use case where we have an API Gateway that will be stitching multiple microservices into one schema. As part of this, we would like to automatically remove certain field arguments (based on a naming convention, in the future potentially a marker directive) from the microservice schema and automatically inject these fields using ContextData from the API Gateway eg:
Then rewrite any field with that argument ("userId") with a delegation directive and remove the field from the exposed API eg.
Even after using the new AddDelegationPath method:
The resultant schema does not seem to have the field with the directive applied, and does not show in GraphQL Playground / Graphiql (may be a seperate issue?):
Running a query where we expect the field to be filled with ContextData and sent to the microservice does not seem to work.
To Reproduce
Please see example project: schema-stitch-test (1).zip
Run both the ApiGateway and Microservice projects concurrently.
Notice Microservice has a "userId" argument, and ApiGateway does not, for the "userFeed" field.
Run a query like:
Notice you will receive an error: ""The argument
userId
is required and does not allow null values." and the resultant schema does show the delegate on the field argument.Expected behavior
The Delegate directive should be applied and queries where the delegate is used should automatically fill with the context data when sent to the stitched service.
Desktop (please complete the following information):
Additional context
As discussed on slack here.
The text was updated successfully, but these errors were encountered: