Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update @graphql-tools/utils dependency #106

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v5.0.1

- Update @graphql-tools/utils dependency and fix samples for Apollo server v3.

### v5.0.0

- Update graphql peer-dependency for Apollo server v3.
Expand Down
4 changes: 4 additions & 0 deletions examples/federation/reviews-service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const GraphQLComponent = require('../../../lib');
const ReviewsDataSource = require('./datasource');
const resolvers = require('./resolvers');
const types = require('./types');
const toUppercaseDirective = require('./toUppercaseDirective')

class ReviewsComponent extends GraphQLComponent {
constructor(options) {
Expand All @@ -17,6 +18,9 @@ const run = async function () {
types,
resolvers,
dataSources: [new ReviewsDataSource()],
directives: {
toUppercase: toUppercaseDirective
},
federation: true
});

Expand Down
4 changes: 3 additions & 1 deletion examples/federation/reviews-service/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
directive @toUppercase on FIELD_DEFINITION

type Review {
id: ID!
content: String!
content: String! @toUppercase
}

extend type Property @key(fields: "id") {
Expand Down
25 changes: 25 additions & 0 deletions examples/federation/reviews-service/toUppercaseDirective.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const {getDirective, MapperKind, mapSchema} = require("@graphql-tools/utils");
const {defaultFieldResolver} = require("graphql");

function toUppercaseDirective(directiveName) {
return (schema) => mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const upperDirective = getDirective(schema, fieldConfig, directiveName)?.[0];
if (upperDirective) {
const {resolve = defaultFieldResolver} = fieldConfig;
return {
...fieldConfig,
resolve: async function (source, args, context, info) {
const result = await resolve(source, args, context, info);
if (typeof result === 'string') {
return result.toUpperCase();
}
return result;
}
}
}
}
})
}

module.exports = toUppercaseDirective
23 changes: 15 additions & 8 deletions lib/__tests__.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const Test = require('tape');
const graphql = require('graphql');
const gql = require('graphql-tag');
const { SchemaDirectiveVisitor } = require('@graphql-tools/utils');
const { MapperKind, mapSchema, getDirective} = require('@graphql-tools/utils');
const GraphQLComponent = require('.');

Test('GraphQLComponent instance API (getters/setters)', (t) => {
Expand Down Expand Up @@ -1109,11 +1109,18 @@ Test(`importing root specifies 'federation: true' results in all components crea
})

Test('federated schema can include custom directive', (t) => {
class CustomDirective extends SchemaDirectiveVisitor {
// required for our dummy "custom" directive (ie. implement the SchemaDirectiveVisitor interface)
visitFieldDefinition() {
return;
}
function customDirective(directiveName) {
return (schema) => mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const directives = getDirective(schema, fieldConfig, directiveName)
const directive = directives && directives[0]
if (directive) {
return {
...fieldConfig,
}
}
}
})
}

const component = new GraphQLComponent({
Expand Down Expand Up @@ -1142,7 +1149,7 @@ Test('federated schema can include custom directive', (t) => {
}
},
},
directives: { custom: CustomDirective },
directives: { custom: customDirective },
federation: true
});

Expand All @@ -1163,7 +1170,7 @@ Test('federated schema can include custom directive', (t) => {
t.plan(2);
const { schema: { _typeMap: { Extended } } } = component;
t.equals(Extended.extensionASTNodes.length, 1, 'Extension AST Nodes is defined');
t.equals(Extended.extensionASTNodes[0].fields.filter((field) => field.name.value === "id" && field.directives[0].name.value === "external").length, 1, `id field marked external`);
t.equals(Extended.astNode.fields.filter((field) => field.name.value === "id" && field.directives[0].name.value === "external").length, 1, `id field marked external`);
});
});

Expand Down
8 changes: 5 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { mergeTypeDefs } = require('@graphql-tools/merge');
const { addMocksToSchema } = require('@graphql-tools/mock');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { delegateToSchema } = require('@graphql-tools/delegate');
const { pruneSchema, SchemaDirectiveVisitor } = require('@graphql-tools/utils');
const { pruneSchema } = require('@graphql-tools/utils');

const { bindResolvers } = require('./resolvers');
const { wrapContext, createContext } = require('./context');
Expand Down Expand Up @@ -102,11 +102,13 @@ class GraphQLComponent {
_getMakeSchemaFunction() {
if (this._federation) {
return (schemaConfig) => {
const schema = buildFederatedSchema(schemaConfig);
let schema = buildFederatedSchema(schemaConfig);

// allows a federated schema to have custom directives using the old class based directive implementation
if (this._directives) {
SchemaDirectiveVisitor.visitSchemaDirectives(schema, this._directives);
for (const name in this._directives) {
schema = this._directives[name](name)(schema)
}
}

return schema;
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-component",
"version": "5.0.0",
"version": "5.0.1",
"description": "Build, customize and compose GraphQL schemas in a componentized fashion",
"keywords": [
"graphql",
Expand All @@ -27,19 +27,19 @@
"@graphql-tools/mock": "^8.7.1",
"@graphql-tools/schema": "^8.5.1",
"@graphql-tools/stitch": "^8.7.1",
"@graphql-tools/utils": "^7.10.0",
"@graphql-tools/utils": "^8.6.10",
"@graphql-tools/wrap": "^8.5.1",
"debug": "^4.3.1"
},
"peerDependencies": {
"graphql": "^16.0.0"
},
"devDependencies": {
"@apollo/gateway": "^0.28.2",
"@apollo/gateway": "^2.7.1",
"apollo-server": "^3.13.0",
"casual": "^1.6.0",
"eslint": "^6.5.1",
"graphql": "^15.0.0",
"graphql": "^16.8.1",
"graphql-tag": "^2.12.4",
"nyc": "^14.1.1",
"sinon": "^12.0.1",
Expand Down
Loading