@@ -6,10 +6,12 @@ import {
6
6
execute ,
7
7
type DefinitionNode ,
8
8
OperationTypeNode ,
9
+ GraphQLScalarType ,
9
10
GraphQLUnionType ,
10
11
printType ,
11
12
} from 'graphql' ;
12
13
import { buildSubgraphSchema } from '../buildSubgraphSchema' ;
14
+ import { printSubgraphSchema } from '../printSubgraphSchema' ;
13
15
import { typeSerializer } from 'apollo-federation-integration-testsuite' ;
14
16
import { errorCauses } from '@apollo/federation-internals' ;
15
17
@@ -964,10 +966,12 @@ describe('buildSubgraphSchema', () => {
964
966
965
967
const validateTag = async (
966
968
header : string ,
969
+ printedHeader : string ,
967
970
directiveDefinitions : string ,
968
971
typeDefinitions : string ,
972
+ withResolvers : boolean ,
969
973
) => {
970
- const schema = buildSubgraphSchema ( gql `
974
+ const typeDefs = gql `
971
975
${ header }
972
976
type User @key(fields: "email") @tag(name: "tagOnType") {
973
977
email: String @tag(name: "tagOnField")
@@ -978,19 +982,15 @@ describe('buildSubgraphSchema', () => {
978
982
}
979
983
980
984
union UserButAUnion @tag(name: "tagOnUnion") = User
981
- ` ) ;
982
985
983
- const { data, errors } = await graphql ( { schema, source : query } ) ;
984
- expect ( errors ) . toBeUndefined ( ) ;
985
- expect ( ( data ?. _service as any ) . sdl ) . toMatchString (
986
- ( header . length === 0
987
- ? ''
988
- : `
989
- ${ header . trim ( ) }
990
- ` ) +
991
- `
992
- ${ directiveDefinitions . trim ( ) }
986
+ scalar CustomScalar @tag(name: "tagOnCustomScalar")
993
987
988
+ enum Enum @tag(name: "tagOnEnum") {
989
+ ENUM_VALUE @tag(name: "tagOnEnumValue")
990
+ }
991
+ ` ;
992
+
993
+ const printedTypeDefsWithoutHeader = `
994
994
type User
995
995
@key(fields: "email")
996
996
@tag(name: "tagOnType")
@@ -1008,15 +1008,72 @@ describe('buildSubgraphSchema', () => {
1008
1008
@tag(name: "tagOnUnion")
1009
1009
= User
1010
1010
1011
+ scalar CustomScalar
1012
+ @tag(name: "tagOnCustomScalar")
1013
+
1014
+ enum Enum
1015
+ @tag(name: "tagOnEnum")
1016
+ {
1017
+ ENUM_VALUE @tag(name: "tagOnEnumValue")
1018
+ }
1019
+ ` ;
1020
+
1021
+ const schema = withResolvers
1022
+ ? buildSubgraphSchema ( {
1023
+ typeDefs,
1024
+ resolvers : {
1025
+ User : {
1026
+ email : ( ) => 1 ,
1027
+ } ,
1028
+ CustomScalar : new GraphQLScalarType ( {
1029
+ name : 'CustomScalar' ,
1030
+ serialize : ( ) => 1 ,
1031
+ parseValue : ( ) => 1 ,
1032
+ parseLiteral : ( ) => 1 ,
1033
+ } ) ,
1034
+ Enum : {
1035
+ ENUM_VALUE : 1 ,
1036
+ } ,
1037
+ } ,
1038
+ } )
1039
+ : buildSubgraphSchema ( typeDefs ) ;
1040
+
1041
+ // Check the sdl field's schema.
1042
+ const { data, errors } = await graphql ( { schema, source : query } ) ;
1043
+ expect ( errors ) . toBeUndefined ( ) ;
1044
+ expect ( ( data ?. _service as any ) . sdl ) . toMatchString (
1045
+ ( header . length === 0
1046
+ ? ''
1047
+ : `
1048
+ ${ header . trim ( ) }
1049
+ ` ) +
1050
+ `
1051
+ ${ directiveDefinitions . trim ( ) }
1052
+
1053
+ ${ printedTypeDefsWithoutHeader . trim ( ) }
1054
+
1011
1055
${ typeDefinitions . trim ( ) }
1012
1056
` ,
1013
1057
) ;
1058
+
1059
+ // Check the printSubgraphSchema's schema.
1060
+ expect ( printSubgraphSchema ( schema ) ) . toMatchString (
1061
+ ( printedHeader . length === 0
1062
+ ? ''
1063
+ : `
1064
+ ${ printedHeader . trim ( ) }
1065
+ ` ) +
1066
+ `
1067
+ ${ printedTypeDefsWithoutHeader . trim ( ) }
1068
+ ` ,
1069
+ ) ;
1014
1070
} ;
1015
1071
1016
- it . each ( [
1072
+ const testCases = [
1017
1073
{
1018
1074
name : 'fed1' ,
1019
1075
header : '' ,
1076
+ printedHeader : '' ,
1020
1077
directiveDefinitions : `
1021
1078
directive @key(fields: _FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE
1022
1079
@@ -1053,6 +1110,10 @@ describe('buildSubgraphSchema', () => {
1053
1110
extend schema
1054
1111
@link(url: "https://specs.apollo.dev/link/v1.0")
1055
1112
@link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@tag"])
1113
+ ` ,
1114
+ printedHeader : `
1115
+ extend schema
1116
+ @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@tag"])
1056
1117
` ,
1057
1118
directiveDefinitions : `
1058
1119
directive @link(url: String, as: String, for: link__Purpose, import: [link__Import]) repeatable on SCHEMA
@@ -1106,10 +1167,41 @@ describe('buildSubgraphSchema', () => {
1106
1167
}
1107
1168
` ,
1108
1169
} ,
1109
- ] ) (
1170
+ ] ;
1171
+
1172
+ it . each ( testCases ) (
1110
1173
'adds it for $name schema' ,
1111
- async ( { header, directiveDefinitions, typesDefinitions } ) => {
1112
- await validateTag ( header , directiveDefinitions , typesDefinitions ) ;
1174
+ async ( {
1175
+ header,
1176
+ printedHeader,
1177
+ directiveDefinitions,
1178
+ typesDefinitions,
1179
+ } ) => {
1180
+ await validateTag (
1181
+ header ,
1182
+ printedHeader ,
1183
+ directiveDefinitions ,
1184
+ typesDefinitions ,
1185
+ false ,
1186
+ ) ;
1187
+ } ,
1188
+ ) ;
1189
+
1190
+ it . each ( testCases ) (
1191
+ 'adds it for $name schema with resolvers' ,
1192
+ async ( {
1193
+ header,
1194
+ printedHeader,
1195
+ directiveDefinitions,
1196
+ typesDefinitions,
1197
+ } ) => {
1198
+ await validateTag (
1199
+ header ,
1200
+ printedHeader ,
1201
+ directiveDefinitions ,
1202
+ typesDefinitions ,
1203
+ true ,
1204
+ ) ;
1113
1205
} ,
1114
1206
) ;
1115
1207
} ) ;
0 commit comments