Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit a6b1aab

Browse files
Fix for relationship types using federated entities (#441)
* handles external entities * handles external entities on relationship types * adds helpers for type extension definitions * adds and uses isExternalTypeExtension * adds more tests for relationship fields between entities * adds relationship property data * adds and uses isExternalTypeExtension * adds helpers for type extension definitions * handles external entities on relationship types * handles external entities * adds relationship types
1 parent 94bc8bc commit a6b1aab

File tree

9 files changed

+504
-120
lines changed

9 files changed

+504
-120
lines changed

example/apollo-federation/seed-data.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const seedData = {
44
{
55
id: '1',
66
body: 'Love it!',
7+
rating: 9.9,
78
product: {
89
upc: '1',
910
name: 'Table',
@@ -28,7 +29,8 @@ export const seedData = {
2829
metric: 1,
2930
data: 2
3031
}
31-
]
32+
],
33+
value: 1
3234
},
3335
authorID: '1',
3436
author: {
@@ -41,6 +43,7 @@ export const seedData = {
4143
{
4244
id: '2',
4345
body: 'Too expensive.',
46+
rating: 5.5,
4447
product: {
4548
upc: '2',
4649
name: 'Couch',
@@ -49,7 +52,8 @@ export const seedData = {
4952
inStock: false,
5053
metrics: [],
5154
objectCompoundKey: null,
52-
listCompoundKey: []
55+
listCompoundKey: [],
56+
value: 2
5357
},
5458
authorID: '1',
5559
author: {
@@ -62,6 +66,7 @@ export const seedData = {
6266
{
6367
id: '3',
6468
body: 'Could be better.',
69+
rating: 3.8,
6570
product: {
6671
upc: '3',
6772
name: 'Chair',
@@ -70,7 +75,8 @@ export const seedData = {
7075
inStock: true,
7176
metrics: [],
7277
objectCompoundKey: null,
73-
listCompoundKey: []
78+
listCompoundKey: [],
79+
value: 3
7480
},
7581
authorID: '2',
7682
author: {
@@ -83,6 +89,7 @@ export const seedData = {
8389
{
8490
id: '4',
8591
body: 'Prefer something else.',
92+
rating: 5.0,
8693
product: {
8794
upc: '1',
8895
name: 'Table',
@@ -107,7 +114,8 @@ export const seedData = {
107114
metric: 1,
108115
data: 2
109116
}
110-
]
117+
],
118+
value: 4
111119
},
112120
authorID: '2',
113121
author: {

example/apollo-federation/services/reviews/index.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const reviewsSchema = buildFederatedSchema([
1919
# Normal use of @relation field directive
2020
product: Product
2121
@relation(name: "REVIEW_OF", direction: OUT)
22+
ratings: [Rating]
2223
}
2324
2425
extend type Account @key(fields: "id") {
@@ -32,12 +33,33 @@ export const reviewsSchema = buildFederatedSchema([
3233
MATCH (this)-[:AUTHOR_OF]->(review:Review)
3334
RETURN count(review)
3435
`})
36+
product: [Product] @relation(name: "PRODUCT_ACCOUNT", direction: IN)
37+
entityRelationship: [EntityRelationship]
3538
}
3639
3740
extend type Product @key(fields: "upc") {
3841
upc: String! @external
3942
reviews(body: String): [Review]
4043
@relation(name: "REVIEW_OF", direction: IN)
44+
ratings: [Rating]
45+
account(filter: LocalAccountFilter): [Account] @relation(name: "PRODUCT_ACCOUNT", direction: OUT)
46+
entityRelationship: [EntityRelationship]
47+
}
48+
49+
input LocalAccountFilter {
50+
id_not: ID
51+
}
52+
53+
type Rating @relation(name: "REVIEW_OF") {
54+
from: Review
55+
rating: Float
56+
to: Product
57+
}
58+
59+
type EntityRelationship @relation(name: "PRODUCT_ACCOUNT") {
60+
from: Product
61+
value: Int
62+
to: Account
4163
}
4264
4365
# Used in testing and for example of nested merge import
@@ -111,9 +133,18 @@ export const reviewsSchema = buildFederatedSchema([
111133
weight: product.weight,
112134
inStock: product.inStock
113135
}
114-
MERGE (p)<-[:REVIEW_OF]-(r)
136+
MERGE (p)<-[:REVIEW_OF {
137+
rating: review.rating
138+
}]-(r)
139+
WITH *
140+
UNWIND review.author AS account
141+
MATCH (a:Account {
142+
id: account.id
143+
})
144+
MERGE (p)-[:PRODUCT_ACCOUNT {
145+
value: product.value
146+
}]->(a)
115147
WITH *
116-
117148
// Merge Review.product.metrics / .objectCompoundKey / .listCompoundKey
118149
UNWIND product.metrics AS metric
119150
MERGE (m:Metric {

src/augment/types/node/node.js

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Kind } from 'graphql';
21
import {
32
augmentNodeQueryAPI,
43
augmentNodeQueryArgumentTypes,
@@ -37,7 +36,9 @@ import {
3736
isNodeType,
3837
isRelationshipType,
3938
isQueryTypeDefinition,
40-
isUnionTypeDefinition
39+
isUnionTypeDefinition,
40+
isObjectTypeExtensionDefinition,
41+
isInterfaceTypeExtensionDefinition
4142
} from '../../types/types';
4243
import { getPrimaryKey } from '../../../utils';
4344

@@ -69,9 +70,12 @@ export const augmentNodeType = ({
6970
if (typeExtensions.length) {
7071
typeExtensionDefinitionMap[typeName] = typeExtensions.map(extension => {
7172
let isIgnoredType = false;
72-
const isObjectExtension = extension.kind === Kind.OBJECT_TYPE_EXTENSION;
73-
const isInterfaceExtension =
74-
extension.kind === Kind.INTERFACE_TYPE_EXTENSION;
73+
const isObjectExtension = isObjectTypeExtensionDefinition({
74+
definition: extension
75+
});
76+
const isInterfaceExtension = isInterfaceTypeExtensionDefinition({
77+
definition: extension
78+
});
7579
if (isObjectExtension || isInterfaceExtension) {
7680
[
7781
extensionNodeInputTypeMap,
@@ -82,6 +86,7 @@ export const augmentNodeType = ({
8286
typeName,
8387
definition: extension,
8488
typeDefinitionMap,
89+
typeExtensionDefinitionMap,
8590
generatedTypeMap,
8691
operationTypeMap,
8792
nodeInputTypeMap: extensionNodeInputTypeMap,
@@ -110,6 +115,7 @@ export const augmentNodeType = ({
110115
isUnionType,
111116
isQueryType,
112117
typeDefinitionMap,
118+
typeExtensionDefinitionMap,
113119
generatedTypeMap,
114120
operationTypeMap,
115121
nodeInputTypeMap,
@@ -178,6 +184,7 @@ export const augmentNodeTypeFields = ({
178184
isUnionType,
179185
isQueryType,
180186
typeDefinitionMap,
187+
typeExtensionDefinitionMap,
181188
generatedTypeMap,
182189
operationTypeMap,
183190
nodeInputTypeMap = {},
@@ -206,10 +213,6 @@ export const augmentNodeTypeFields = ({
206213
};
207214
}
208215
}
209-
if (fields === undefined) {
210-
console.log('\ndefinition: ', definition);
211-
console.log('fields: ', fields);
212-
}
213216
propertyOutputFields = fields.reduce((outputFields, field) => {
214217
let fieldType = field.type;
215218
let fieldArguments = field.arguments;
@@ -264,13 +267,14 @@ export const augmentNodeTypeFields = ({
264267
outputType,
265268
nodeInputTypeMap,
266269
typeDefinitionMap,
270+
typeExtensionDefinitionMap,
267271
generatedTypeMap,
268272
operationTypeMap,
269-
config,
270273
relationshipDirective,
271274
outputTypeWrappers,
272275
isObjectExtension,
273-
isInterfaceExtension
276+
isInterfaceExtension,
277+
config
274278
});
275279
} else if (isRelationshipType({ definition: outputDefinition })) {
276280
[
@@ -292,8 +296,11 @@ export const augmentNodeTypeFields = ({
292296
outputDefinition,
293297
nodeInputTypeMap,
294298
typeDefinitionMap,
299+
typeExtensionDefinitionMap,
295300
generatedTypeMap,
296301
operationTypeMap,
302+
isObjectExtension,
303+
isInterfaceExtension,
297304
config
298305
});
299306
}
@@ -305,21 +312,22 @@ export const augmentNodeTypeFields = ({
305312
});
306313
return outputFields;
307314
}, []);
308-
309-
if (!isQueryType && extensionNodeInputTypeMap) {
310-
if (extensionNodeInputTypeMap[FilteringArgument.FILTER]) {
311-
const extendedFilteringFields =
312-
extensionNodeInputTypeMap[FilteringArgument.FILTER].fields;
313-
nodeInputTypeMap[FilteringArgument.FILTER].fields.push(
314-
...extendedFilteringFields
315-
);
316-
}
317-
if (extensionNodeInputTypeMap[OrderingArgument.ORDER_BY]) {
318-
const extendedOrderingValues =
319-
extensionNodeInputTypeMap[OrderingArgument.ORDER_BY].values;
320-
nodeInputTypeMap[OrderingArgument.ORDER_BY].values.push(
321-
...extendedOrderingValues
322-
);
315+
if (!isObjectExtension && !isInterfaceExtension) {
316+
if (!isQueryType && extensionNodeInputTypeMap) {
317+
if (extensionNodeInputTypeMap[FilteringArgument.FILTER]) {
318+
const extendedFilteringFields =
319+
extensionNodeInputTypeMap[FilteringArgument.FILTER].fields;
320+
nodeInputTypeMap[FilteringArgument.FILTER].fields.push(
321+
...extendedFilteringFields
322+
);
323+
}
324+
if (extensionNodeInputTypeMap[OrderingArgument.ORDER_BY]) {
325+
const extendedOrderingValues =
326+
extensionNodeInputTypeMap[OrderingArgument.ORDER_BY].values;
327+
nodeInputTypeMap[OrderingArgument.ORDER_BY].values.push(
328+
...extendedOrderingValues
329+
);
330+
}
323331
}
324332
}
325333
} else {
@@ -347,13 +355,12 @@ const augmentNodeTypeField = ({
347355
outputType,
348356
nodeInputTypeMap,
349357
typeDefinitionMap,
358+
typeExtensionDefinitionMap,
350359
generatedTypeMap,
351360
operationTypeMap,
352361
config,
353362
relationshipDirective,
354-
outputTypeWrappers,
355-
isObjectExtension,
356-
isInterfaceExtension
363+
outputTypeWrappers
357364
}) => {
358365
const isUnionType = isUnionTypeDefinition({ definition: outputDefinition });
359366
fieldArguments = augmentNodeTypeFieldArguments({
@@ -365,19 +372,11 @@ const augmentNodeTypeField = ({
365372
typeDefinitionMap,
366373
config
367374
});
368-
if (!isUnionType && !isObjectExtension && !isInterfaceExtension) {
375+
if (!isUnionType) {
369376
if (
370377
relationshipDirective &&
371378
!isQueryTypeDefinition({ definition, operationTypeMap })
372379
) {
373-
nodeInputTypeMap = augmentNodeQueryArgumentTypes({
374-
typeName,
375-
fieldName,
376-
outputType,
377-
outputTypeWrappers,
378-
nodeInputTypeMap,
379-
config
380-
});
381380
const relationshipName = getRelationName(relationshipDirective);
382381
const relationshipDirection = getRelationDirection(relationshipDirective);
383382
// Assume direction OUT
@@ -388,6 +387,14 @@ const augmentNodeTypeField = ({
388387
fromType = outputType;
389388
toType = temp;
390389
}
390+
nodeInputTypeMap = augmentNodeQueryArgumentTypes({
391+
typeName,
392+
fieldName,
393+
outputType,
394+
outputTypeWrappers,
395+
nodeInputTypeMap,
396+
config
397+
});
391398
[
392399
typeDefinitionMap,
393400
generatedTypeMap,
@@ -400,6 +407,7 @@ const augmentNodeTypeField = ({
400407
toType,
401408
relationshipName,
402409
typeDefinitionMap,
410+
typeExtensionDefinitionMap,
403411
generatedTypeMap,
404412
operationTypeMap,
405413
config

0 commit comments

Comments
 (0)