Skip to content

Commit 1c8329c

Browse files
committed
update readme
1 parent 72d34ed commit 1c8329c

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

graphql-kotlin-federation/README.md

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,136 @@ compile(group: 'com.expedia', name: 'graphql-kotlin-federation', version: "$late
2727

2828
## Usage
2929

30-
// TODO
30+
In order to generate valid federated schemas, you will need to annotate your both base service and the one extending it. Federated Gateway (e.g. Apollo) will then combine the individual graphs to form single federated graph.
31+
32+
> Base Service
33+
34+
```kotlin
35+
@KeyDirective(fields = FieldSet("id"))
36+
data class Product(val id: Int, val description: String)
37+
38+
class ProductQuery {
39+
fun product(id: Int): Product? {
40+
// grabs product from a data source, might return null
41+
}
42+
}
43+
44+
// Generate the schema
45+
val federatedTypeRegistry = FederatedTypeRegistry(emptyMap())
46+
val config = FederatedSchemaGeneratorConfig(supportedPackages = listOf("org.example"), hooks = FederatedSchemaGeneratorHooks(federatedTypeRegistry))
47+
val queries = listOf(TopLevelObject(ProductQuery()))
48+
49+
toFederatedSchema(config, queries)
50+
```
51+
52+
Generates the following schema with additional federated types
53+
54+
```graphql
55+
schema {
56+
query: Query
57+
}
58+
59+
#Space separated list of primary keys needed to access federated object
60+
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
61+
62+
union _Entity = Product
63+
64+
type Product @key(fields : "id") {
65+
description: String!
66+
id: String!
67+
}
68+
69+
type Query {
70+
#Union of all types that use the @key directive, including both types native to the schema and extended types
71+
_entities(representations: [_Any!]!): [_Entity]!
72+
_service: _Service
73+
product(id: String!): Product!
74+
}
75+
76+
type _Service {
77+
sdl: String!
78+
}
79+
80+
#Federation scalar type used to represent any external entities passed to _entities query.
81+
scalar _Any
82+
83+
#Federation type representing set of fields
84+
scalar _FieldSet
85+
```
86+
87+
> Extended Service
88+
89+
```kotlin
90+
@KeyDirective(fields = FieldSet("id"))
91+
@ExtendsDirective
92+
data class Product(@property:ExternalDirective val id: Int) {
93+
94+
fun reviews(): List<Review> {
95+
// returns list of product reviews
96+
}
97+
}
98+
99+
data class Review(val reviewId: String, val text: String)
100+
101+
// Generate the schema
102+
val productResolver = object: FederatedTypeResolver<Product> {
103+
override fun resolve(keys: Map<String, Any>): Product {
104+
val id = keys["id"]?.toString()?.toIntOrNull()
105+
// instantiate product using id
106+
}
107+
}
108+
val federatedTypeRegistry = FederatedTypeRegistry(mapOf("Product" to productResolver))
109+
val config = FederatedSchemaGeneratorConfig(supportedPackages = listOf("org.example"), hooks = FederatedSchemaGeneratorHooks(federatedTypeRegistry))
110+
111+
toFederatedSchema(config)
112+
```
113+
114+
Generates the following federated schema
115+
116+
```graphql
117+
schema {
118+
query: Query
119+
}
120+
121+
#Marks target field as external meaning it will be resolved by federated schema
122+
directive @external on FIELD_DEFINITION
123+
124+
#Marks target object as part of the federated schema
125+
directive @extends on OBJECT | INTERFACE
126+
127+
#Space separated list of primary keys needed to access federated object
128+
directive @key(fields: _FieldSet!) on OBJECT | INTERFACE
129+
130+
union _Entity = Product
131+
132+
type Product @extends @key(fields : "id") {
133+
id: Int! @external
134+
reviews: [Review!]!
135+
}
136+
137+
type Query {
138+
#Union of all types that use the @key directive, including both types native to the schema and extended types
139+
_entities(representations: [_Any!]!): [_Entity]!
140+
_service: _Service
141+
}
142+
143+
type Review {
144+
reviewId: String!
145+
text: String!
146+
}
147+
148+
type _Service {
149+
sdl: String!
150+
}
151+
152+
#Federation scalar type used to represent any external entities passed to _entities query.
153+
scalar _Any
154+
155+
#Federation type representing set of fields
156+
scalar _FieldSet
157+
```
158+
159+
Federated Gateway will then combine the schemas from the individual services to generate single schema.
31160

32161
## Documentation
33162

graphql-kotlin-federation/src/main/kotlin/com/expedia/graphql/federation/FederatedSchemaGenerator.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class FederatedSchemaGenerator(generatorConfig: FederatedSchemaGeneratorConfig)
3333
.map { pkg -> Reflections(pkg).getTypesAnnotatedWith(ExtendsDirective::class.java).map { it.kotlin } }
3434
.flatten()
3535
.map {
36-
// TODO we are not applying all hooks on the target objects, e.g. didGenerateGraphQLType is only invoked from base TypeBuilder
3736
val graphQLType = if (it.isAbstract) {
3837
interfaceType(it)
3938
} else {

0 commit comments

Comments
 (0)