11import { gql } from 'apollo-server' ;
22import { buildFederatedSchema } from '@apollo/federation' ;
33import { makeAugmentedSchema , neo4jgraphql , cypher } from '../../../../src' ;
4+ import { seedData } from '../../seed-data' ;
45
56export 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