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

Commit e735bbf

Browse files
general type extension support / some federation revision
1 parent 1d1baa4 commit e735bbf

File tree

19 files changed

+2127
-295
lines changed

19 files changed

+2127
-295
lines changed

example/apollo-federation/gateway.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { productsSchema } from './services/products';
66
import { reviewsSchema } from './services/reviews';
77
import neo4j from 'neo4j-driver';
88

9+
// The schema and seed data are based on the Apollo Federation demo
10+
// See: https://github.com/apollographql/federation-demo
11+
912
const driver = neo4j.driver(
1013
process.env.NEO4J_URI || 'bolt://localhost:7687',
1114
neo4j.auth.basic(
@@ -107,6 +110,6 @@ const gateway = new ApolloGateway({
107110
});
108111

109112
server.listen({ port: 4000 }).then(({ url }) => {
110-
console.log(`🚀 Server ready at ${url}`);
113+
console.log(`🚀 Apollo Gateway ready at ${url}`);
111114
});
112115
})();
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
export const seedData = {
2+
data: {
3+
Review: [
4+
{
5+
id: '1',
6+
body: 'Love it!',
7+
product: {
8+
upc: '1',
9+
name: 'Table',
10+
price: 899,
11+
weight: 100,
12+
inStock: true,
13+
metrics: [
14+
{
15+
id: '100',
16+
metric: 1,
17+
data: 2
18+
}
19+
],
20+
objectCompoundKey: {
21+
id: '100',
22+
metric: 1,
23+
data: 2
24+
},
25+
listCompoundKey: [
26+
{
27+
id: '100',
28+
metric: 1,
29+
data: 2
30+
}
31+
]
32+
},
33+
authorID: '1',
34+
author: {
35+
id: '1',
36+
name: 'Ada Lovelace',
37+
username: '@ada',
38+
numberOfReviews: 2
39+
}
40+
},
41+
{
42+
id: '2',
43+
body: 'Too expensive.',
44+
product: {
45+
upc: '2',
46+
name: 'Couch',
47+
price: 1299,
48+
weight: 1000,
49+
inStock: false,
50+
metrics: [],
51+
objectCompoundKey: null,
52+
listCompoundKey: []
53+
},
54+
authorID: '1',
55+
author: {
56+
id: '1',
57+
name: 'Ada Lovelace',
58+
username: '@ada',
59+
numberOfReviews: 2
60+
}
61+
},
62+
{
63+
id: '3',
64+
body: 'Could be better.',
65+
product: {
66+
upc: '3',
67+
name: 'Chair',
68+
price: 54,
69+
weight: 50,
70+
inStock: true,
71+
metrics: [],
72+
objectCompoundKey: null,
73+
listCompoundKey: []
74+
},
75+
authorID: '2',
76+
author: {
77+
id: '2',
78+
name: 'Alan Turing',
79+
username: '@complete',
80+
numberOfReviews: 2
81+
}
82+
},
83+
{
84+
id: '4',
85+
body: 'Prefer something else.',
86+
product: {
87+
upc: '1',
88+
name: 'Table',
89+
price: 899,
90+
weight: 100,
91+
inStock: true,
92+
metrics: [
93+
{
94+
id: '100',
95+
metric: 1,
96+
data: 2
97+
}
98+
],
99+
objectCompoundKey: {
100+
id: '100',
101+
metric: 1,
102+
data: 2
103+
},
104+
listCompoundKey: [
105+
{
106+
id: '100',
107+
metric: 1,
108+
data: 2
109+
}
110+
]
111+
},
112+
authorID: '2',
113+
author: {
114+
id: '2',
115+
name: 'Alan Turing',
116+
username: '@complete',
117+
numberOfReviews: 2
118+
}
119+
}
120+
]
121+
}
122+
};

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { gql } from 'apollo-server';
22
import { buildFederatedSchema } from '@apollo/federation';
3-
import { makeAugmentedSchema, neo4jgraphql, cypher } from '../../../../src';
3+
import { makeAugmentedSchema, cypher } from '../../../../src';
44

55
export const accountsSchema = buildFederatedSchema([
66
makeAugmentedSchema({
77
typeDefs: gql`
88
extend type Query {
9-
me: User
9+
me: Account
10+
Account: [Account] @cypher(${cypher`
11+
MATCH (account: Account)
12+
RETURN account
13+
`})
1014
}
1115
12-
type User @key(fields: "id") {
16+
type Account @key(fields: "id") {
1317
id: ID!
1418
name: String
1519
username: String
@@ -22,7 +26,7 @@ export const accountsSchema = buildFederatedSchema([
2226
})
2327
]);
2428

25-
export const users = [
29+
export const accounts = [
2630
{
2731
id: '1',
2832
name: 'Ada Lovelace',

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { gql } from 'apollo-server';
22
import { buildFederatedSchema } from '@apollo/federation';
3-
import { makeAugmentedSchema, neo4jgraphql, cypher } from '../../../../src';
3+
import { makeAugmentedSchema, cypher } from '../../../../src';
44

55
export const inventorySchema = buildFederatedSchema([
66
makeAugmentedSchema({
77
typeDefs: gql`
8-
extend type Product @key(fields: "upc listCompoundKey { id } objectCompoundKey { id }") {
8+
extend type Product @key(fields: "upc listCompoundKey { id } objectCompoundKey { id } nullKey") {
99
upc: String! @external
1010
weight: Int @external
1111
price: Int @external
12+
nullKey: String @external
1213
inStock: Boolean
1314
shippingEstimate: Int
1415
@requires(fields: "weight price")

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import { gql } from 'apollo-server';
22
import { buildFederatedSchema } from '@apollo/federation';
3-
import { makeAugmentedSchema, neo4jgraphql, cypher } from '../../../../src';
3+
import { makeAugmentedSchema } from '../../../../src';
44

55
export const productsSchema = buildFederatedSchema([
66
makeAugmentedSchema({
77
typeDefs: gql`
88
extend type Query {
9+
Product: [Product]
910
topProducts(first: Int = 5): [Product]
1011
}
1112
1213
type Product
13-
@key(fields: "upc listCompoundKey { id } objectCompoundKey { id }") {
14+
@key(
15+
fields: "upc listCompoundKey { id } objectCompoundKey { id } nullKey"
16+
) {
1417
upc: String!
1518
name: String
1619
price: Int
1720
weight: Int
21+
nullKey: String
1822
objectCompoundKey: Metric @relation(name: "METRIC_OF", direction: OUT)
1923
listCompoundKey: [Metric] @relation(name: "METRIC_OF", direction: OUT)
2024
}

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

Lines changed: 121 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import { gql } from 'apollo-server';
22
import { buildFederatedSchema } from '@apollo/federation';
33
import { makeAugmentedSchema, neo4jgraphql, cypher } from '../../../../src';
4+
import { seedData } from '../../seed-data';
45

56
export const reviewsSchema = buildFederatedSchema([
67
makeAugmentedSchema({
78
typeDefs: gql`
8-
type Review @key(fields: "id") {
9+
type Review @key(fields: "id authorID") {
910
id: ID!
1011
body: String
11-
author: User
12-
@cypher(statement: "MATCH (this)<-[:AUTHOR_OF]-(user:User) RETURN user")
12+
authorID: ID
13+
# Scalar property to lookup associate node
14+
author: Account
15+
@cypher(${cypher`
16+
MATCH (account:Account {id: this.authorID})
17+
RETURN account
18+
`})
19+
# Normal use of @relation field directive
1320
product: Product
1421
@relation(name: "REVIEW_OF", direction: OUT)
1522
}
1623
17-
extend type User @key(fields: "id") {
24+
extend type Account @key(fields: "id") {
1825
id: ID! @external
19-
reviews: [Review]
26+
# Object list @relation field added to nonlocal type for local type
27+
reviews(body: String): [Review]
2028
@relation(name: "AUTHOR_OF", direction: OUT)
29+
# Scalar @cypher field added to nonlocal type for local type
2130
numberOfReviews: Int
2231
@cypher(${cypher`
2332
MATCH (this)-[:AUTHOR_OF]->(review:Review)
@@ -27,12 +36,117 @@ export const reviewsSchema = buildFederatedSchema([
2736
2837
extend type Product @key(fields: "upc") {
2938
upc: String! @external
30-
reviews: [Review]
39+
reviews(body: String): [Review]
3140
@relation(name: "REVIEW_OF", direction: IN)
3241
}
42+
43+
# Used in testing and for example of nested merge import
44+
input MergeReviewsInput {
45+
id: ID!
46+
body: String
47+
product: MergeProductInput
48+
author: MergeAccountInput
49+
}
50+
51+
input MergeProductInput {
52+
upc: String!
53+
name: String
54+
price: Int
55+
weight: Int
56+
inStock: Boolean
57+
metrics: [MergeMetricInput]
58+
objectCompoundKey: MergeMetricInput
59+
listCompoundKey: [MergeMetricInput]
60+
}
61+
62+
input MergeMetricInput {
63+
id: ID!
64+
metric: String
65+
}
66+
67+
input MergeAccountInput {
68+
id: ID!
69+
name: String
70+
username: String
71+
}
72+
73+
extend type Mutation {
74+
MergeSeedData(data: MergeReviewsInput): Boolean @cypher(${cypher`
75+
UNWIND $data AS review
76+
MERGE (r:Review {
77+
id: review.id
78+
})
79+
SET r += {
80+
body: review.body,
81+
authorID: review.authorID
82+
}
83+
WITH *
84+
85+
// Merge Review.author
86+
UNWIND review.author AS account
87+
MERGE (a:Account {
88+
id: account.id
89+
})
90+
ON CREATE SET a += {
91+
name: account.name,
92+
username: account.username
93+
}
94+
MERGE (r)<-[:AUTHOR_OF]-(a)
95+
// Resets processing context for unwound sibling relationship data
96+
WITH COUNT(*) AS SCOPE
97+
98+
// Unwind second sibling, Review.product
99+
UNWIND $data AS review
100+
MATCH (r:Review {
101+
id: review.id
102+
})
103+
// Merge Review.product
104+
UNWIND review.product AS product
105+
MERGE (p:Product {
106+
upc: product.upc
107+
})
108+
ON CREATE SET p += {
109+
name: product.name,
110+
price: product.price,
111+
weight: product.weight,
112+
inStock: product.inStock
113+
}
114+
MERGE (p)<-[:REVIEW_OF]-(r)
115+
WITH *
116+
117+
// Merge Review.product.metrics / .objectCompoundKey / .listCompoundKey
118+
UNWIND product.metrics AS metric
119+
MERGE (m:Metric {
120+
id: metric.id
121+
})
122+
ON CREATE SET m += {
123+
metric: metric.metric
124+
}
125+
MERGE (p)-[:METRIC_OF]->(m)
126+
// End processing scope for Review.product
127+
WITH COUNT(*) AS SCOPE
128+
129+
RETURN true
130+
`})
131+
DeleteSeedData: Boolean @cypher(${cypher`
132+
MATCH (account: Account)
133+
MATCH (product: Product)
134+
MATCH (review: Review)
135+
MATCH (metric: Metric)
136+
DETACH DELETE account, product, review, metric
137+
RETURN TRUE
138+
`})
139+
}
140+
33141
`,
34142
resolvers: {
35-
User: {
143+
Mutation: {
144+
async MergeSeedData(object, params, context, resolveInfo) {
145+
const data = seedData.data['Review'];
146+
return await neo4jgraphql(object, { data }, context, resolveInfo);
147+
}
148+
},
149+
Account: {
36150
// Generated
37151
// async __resolveReference(object, context, resolveInfo) {
38152
// const data = await neo4jgraphql(object, {}, context, resolveInfo);

0 commit comments

Comments
 (0)