menu

Apollo

A community of developers, designers and others who love Apollo and GraphQL. 🚀

Channels
# All channels
view-forward
# General
view-forward
# Apollo Angular
view-forward
# Announcements
view-forward
# Apollo Android
view-forward
# Apollo Client
view-forward
# Apollo iOS
view-forward
# Apollo Link
view-forward
# Apollo Link Rest
view-forward
# Local State
view-forward
# Apollo Platform
view-forward
# Apollo Server
view-forward
# Apollo Tooling
view-forward
# Contributing
view-forward
# Docs
view-forward
# Events
view-forward
# GraphQL Tools
view-forward
# Jobs
view-forward
# Random
view-forward
# React Apollo
view-forward
# Showcase
view-forward
# Subscriptions
view-forward
# Testing
view-forward
# Vue Apollo
view-forward
Team

Why does `buildFederatedSchema` ignore custom directives?

May 31, 2019 at 8:32pm

Why does `buildFederatedSchema` ignore custom directives?

May 31, 2019 at 8:32pm
Hi! I'm diving into /federation and the demo app and I'm wondering why directives defined on federated schemas are thrown away in the _Service.sdl field. Just wondering if I'm missing some key concepts!
It looks like directives are filtered down the federation-specific ones, with some additional special treatment of the @deprecated directive directive
(This is related to my other question ... if we can't share directives with the gateway we can't add centralized logic.)

June 4, 2019 at 2:23pm
Hey ! Schema directives are on the near term roadmap for support with federation! There are still a ton of open questions around service level directives and graph / gateway level ones. Would you mind opening an issue on GitHub to kick start that discussion?
  • reply
  • like
Ticket filed, thanks for the quick response ! https://github.com/apollographql/apollo-server/issues/2778
like-fill
1
  • reply
  • like

June 5, 2019 at 7:34pm
Thank you ! Excited to find the right path here together :)
  • reply
  • like

June 11, 2019 at 8:19am
Is there any workaround for having custom directives with Gateway and Federated Schemas?
  • reply
  • like

June 24, 2019 at 3:33pm
any updates with directives support?
  • reply
  • like

June 26, 2019 at 8:43pm
I have a work around! You can attach functional directives to the federatedSchema!
const {attachDirectiveResolvers} = require('graphql-tools');
const typeDefs = gql`
directive @upper on FIELD_DEFINITION
... rest of schema ...
`;
const directiveResolvers = {
upper(next, src, args, context) {
return next().then((str) => {
if (typeof(str) === 'string') {
return str.toUpperCase();
}
return str;
});
}
const schema = buildFederatedSchema([
{
typeDefs,
resolvers,
}
]);
attachDirectiveResolvers(
schema,
directiveResolvers,
);
const server = new ApolloServer({
schema
});
  • reply
  • like

July 3, 2019 at 5:35pm
I cannot get your implementation to work. Did you have to do anything else to get it to work?
  • reply
  • like
okay so i did get it to work. Can you use functional directives to omit fields from the schema based on auth status>
  • reply
  • like
any idea when this might get implemented? also ima be in SF in a couple weeks. Could i swing by?
  • reply
  • like

March 20, 2020 at 5:01pm
apollographql/apollo-server#3464 & apollographql/apollo-server#3536.
I'm still at a standstill figuring out how i make the federated service to call my directive class so i can perform my various actions on my fields.
Basically i have this small directive for validating various lengths:
directive @length(
min: Int
equal: Int
) on FIELD_DEFINITION | ARGUMENT_DEFINITION
And using it like so:
extend type Query {
people(
names: [String!]! @length(min: 2)
): [Person!]
}
Following this I got this directive:
import { SchemaDirectiveVisitor } from "apollo-server-express";
class LengthDirective extends SchemaDirectiveVisitor {
visitArgumentDefinition(argument) {
console.log("visitArgumentDefinition", argument, this.args);
}
visitFieldDefinition(field) {
console.log("visitFieldDefinition args", this.args);
return field;
}
}
export default LengthDirective;
No console logs are fired, I've tried using mergeSchema (that just kills all my directives, including the federated ones and also just using schemaDirectives on apollo-server, nothing makes it work, did any one here make it work and if so could you please make a example? :)
I can't use the attatchDirectResolver because that doesn't allow for usage on ARGUMENT_DEFINITION
  • reply
  • like

March 23, 2020 at 10:55am
For mergeSchemas, did you set the mergeDirectives argument to true?
  • reply
  • like

March 24, 2020 at 7:01am
Nope I could try that for sure.
  • reply
  • like

April 3, 2020 at 5:24pm
Nope I could try that for sure.
Hi Lars, just wondering if you had tried that yet and if it worked!
  • reply
  • like