Skip to content

Commit b56f58a

Browse files
authored
Merge pull request #106 from gky99/fix-directive
Update @graphql-tools/utils dependency
2 parents f408d20 + 8f82443 commit b56f58a

File tree

7 files changed

+60
-16
lines changed

7 files changed

+60
-16
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
### v5.0.1
2+
3+
- Update @graphql-tools/utils dependency and fix samples for Apollo server v3.
4+
15
### v5.0.0
26

37
- Update graphql peer-dependency for Apollo server v3.

Diff for: examples/federation/reviews-service/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const GraphQLComponent = require('../../../lib');
55
const ReviewsDataSource = require('./datasource');
66
const resolvers = require('./resolvers');
77
const types = require('./types');
8+
const toUppercaseDirective = require('./toUppercaseDirective')
89

910
class ReviewsComponent extends GraphQLComponent {
1011
constructor(options) {
@@ -17,6 +18,9 @@ const run = async function () {
1718
types,
1819
resolvers,
1920
dataSources: [new ReviewsDataSource()],
21+
directives: {
22+
toUppercase: toUppercaseDirective
23+
},
2024
federation: true
2125
});
2226

Diff for: examples/federation/reviews-service/schema.graphql

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
directive @toUppercase on FIELD_DEFINITION
2+
13
type Review {
24
id: ID!
3-
content: String!
5+
content: String! @toUppercase
46
}
57

68
extend type Property @key(fields: "id") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const {getDirective, MapperKind, mapSchema} = require("@graphql-tools/utils");
2+
const {defaultFieldResolver} = require("graphql");
3+
4+
function toUppercaseDirective(directiveName) {
5+
return (schema) => mapSchema(schema, {
6+
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
7+
const upperDirective = getDirective(schema, fieldConfig, directiveName)?.[0];
8+
if (upperDirective) {
9+
const {resolve = defaultFieldResolver} = fieldConfig;
10+
return {
11+
...fieldConfig,
12+
resolve: async function (source, args, context, info) {
13+
const result = await resolve(source, args, context, info);
14+
if (typeof result === 'string') {
15+
return result.toUpperCase();
16+
}
17+
return result;
18+
}
19+
}
20+
}
21+
}
22+
})
23+
}
24+
25+
module.exports = toUppercaseDirective

Diff for: lib/__tests__.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const Test = require('tape');
44
const graphql = require('graphql');
55
const gql = require('graphql-tag');
6-
const { SchemaDirectiveVisitor } = require('@graphql-tools/utils');
6+
const { MapperKind, mapSchema, getDirective} = require('@graphql-tools/utils');
77
const GraphQLComponent = require('.');
88

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

11111111
Test('federated schema can include custom directive', (t) => {
1112-
class CustomDirective extends SchemaDirectiveVisitor {
1113-
// required for our dummy "custom" directive (ie. implement the SchemaDirectiveVisitor interface)
1114-
visitFieldDefinition() {
1115-
return;
1116-
}
1112+
function customDirective(directiveName) {
1113+
return (schema) => mapSchema(schema, {
1114+
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
1115+
const directives = getDirective(schema, fieldConfig, directiveName)
1116+
const directive = directives && directives[0]
1117+
if (directive) {
1118+
return {
1119+
...fieldConfig,
1120+
}
1121+
}
1122+
}
1123+
})
11171124
}
11181125

11191126
const component = new GraphQLComponent({
@@ -1142,7 +1149,7 @@ Test('federated schema can include custom directive', (t) => {
11421149
}
11431150
},
11441151
},
1145-
directives: { custom: CustomDirective },
1152+
directives: { custom: customDirective },
11461153
federation: true
11471154
});
11481155

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

Diff for: lib/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { mergeTypeDefs } = require('@graphql-tools/merge');
66
const { addMocksToSchema } = require('@graphql-tools/mock');
77
const { makeExecutableSchema } = require('@graphql-tools/schema');
88
const { delegateToSchema } = require('@graphql-tools/delegate');
9-
const { pruneSchema, SchemaDirectiveVisitor } = require('@graphql-tools/utils');
9+
const { pruneSchema } = require('@graphql-tools/utils');
1010

1111
const { bindResolvers } = require('./resolvers');
1212
const { wrapContext, createContext } = require('./context');
@@ -102,11 +102,13 @@ class GraphQLComponent {
102102
_getMakeSchemaFunction() {
103103
if (this._federation) {
104104
return (schemaConfig) => {
105-
const schema = buildFederatedSchema(schemaConfig);
105+
let schema = buildFederatedSchema(schemaConfig);
106106

107107
// allows a federated schema to have custom directives using the old class based directive implementation
108108
if (this._directives) {
109-
SchemaDirectiveVisitor.visitSchemaDirectives(schema, this._directives);
109+
for (const name in this._directives) {
110+
schema = this._directives[name](name)(schema)
111+
}
110112
}
111113

112114
return schema;

Diff for: package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-component",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "Build, customize and compose GraphQL schemas in a componentized fashion",
55
"keywords": [
66
"graphql",
@@ -27,19 +27,19 @@
2727
"@graphql-tools/mock": "^8.7.1",
2828
"@graphql-tools/schema": "^8.5.1",
2929
"@graphql-tools/stitch": "^8.7.1",
30-
"@graphql-tools/utils": "^7.10.0",
30+
"@graphql-tools/utils": "^8.6.10",
3131
"@graphql-tools/wrap": "^8.5.1",
3232
"debug": "^4.3.1"
3333
},
3434
"peerDependencies": {
3535
"graphql": "^16.0.0"
3636
},
3737
"devDependencies": {
38-
"@apollo/gateway": "^0.28.2",
38+
"@apollo/gateway": "^2.7.1",
3939
"apollo-server": "^3.13.0",
4040
"casual": "^1.6.0",
4141
"eslint": "^6.5.1",
42-
"graphql": "^15.0.0",
42+
"graphql": "^16.8.1",
4343
"graphql-tag": "^2.12.4",
4444
"nyc": "^14.1.1",
4545
"sinon": "^12.0.1",

0 commit comments

Comments
 (0)