Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Federation] Support value types #3063

Merged
merged 42 commits into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
05ab40b
Port UniqueFieldDefinitionNames to TS
trevor-scheer Jul 17, 2019
f2bab61
Update rules
trevor-scheer Jul 17, 2019
224ae45
Import cleanup
trevor-scheer Jul 17, 2019
d513188
Add tests (+ failing tests) for uniqueTypeNamesWithoutEnumsOrScalars
trevor-scheer Jul 17, 2019
6fb0038
Make changes to uniqueTypeNamesWithoutEnumsOrScalars to permit value …
trevor-scheer Jul 17, 2019
4eeb39e
Add tests for uniqueFieldDefinitionNames
trevor-scheer Jul 17, 2019
64f0cdc
Add functionality to uniqueFieldDefinitionNames for supporting value …
trevor-scheer Jul 17, 2019
47c8c12
Fix a bug and add a related test
trevor-scheer Jul 18, 2019
11cf6ca
Clean up: common diffing function
trevor-scheer Jul 18, 2019
1f70648
Add comments
trevor-scheer Jul 18, 2019
459ed87
Break out of visitor early
trevor-scheer Jul 18, 2019
e8b91b8
Cleanup diff function
trevor-scheer Jul 18, 2019
7e6d4d1
Add messaging around kind mismatch.
trevor-scheer Jul 18, 2019
2e49b0c
Report entity-related errors to the user
trevor-scheer Jul 19, 2019
be6749e
Add support and errors around union types
trevor-scheer Jul 19, 2019
a85c0c1
Initial docs update for value types
trevor-scheer Jul 22, 2019
ae3b6ed
Update testing strategy to resemble composition more closely.
trevor-scheer Jul 22, 2019
33c3303
* Update testing strategy to be more closely represent composition
trevor-scheer Jul 22, 2019
48f548c
Update docs with new error codes
trevor-scheer Jul 22, 2019
6b349c1
Remove unused serializer
trevor-scheer Jul 22, 2019
21663f1
Add integration test to composeAndValidate
trevor-scheer Jul 22, 2019
d3a3a86
* Add integration tests
trevor-scheer Jul 22, 2019
2e374de
Cleanup unused
trevor-scheer Jul 23, 2019
13f2e8b
Update docs/source/federation/core-concepts.md
trevor-scheer Jul 23, 2019
4d44308
Update docs/source/federation/core-concepts.md
trevor-scheer Jul 23, 2019
63e335d
Update packages/apollo-federation/src/composition/validate/sdl/unique…
trevor-scheer Jul 23, 2019
9095d2c
Update docs/source/federation/core-concepts.md
trevor-scheer Jul 23, 2019
ce69ca8
Fix logical mistake related to error reporting and update tests accor…
trevor-scheer Jul 23, 2019
c35f15b
Update snapshots related to error message update
trevor-scheer Jul 23, 2019
561470f
Return early, un-nest if statement
trevor-scheer Jul 23, 2019
1f00664
Separate handling of union validations
trevor-scheer Jul 23, 2019
8c1d07b
Types cleanup
trevor-scheer Jul 23, 2019
e01e79a
Add serviceName info to new validation errors
trevor-scheer Jul 23, 2019
41f04d1
Add more info to duplicate field def error message
trevor-scheer Jul 23, 2019
b3a34ad
Add some comments
trevor-scheer Jul 23, 2019
8fc61bd
Update error message and snapshots
trevor-scheer Jul 23, 2019
9e4de8e
Merge branch 'master' into trevor/value-types
trevor-scheer Jul 24, 2019
11337f1
Update union usage to be valid in tests and docs
trevor-scheer Jul 24, 2019
a5829c9
Undo the mess I made of snapshots
trevor-scheer Jul 24, 2019
d15153c
Merge branch 'master' into trevor/value-types
abernix Jul 28, 2019
2d0b1c3
Fix typo in the word "Products".
abernix Jul 28, 2019
3ef8b21
Add CHANGELOG.md for #3063
abernix Jul 28, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions docs/source/federation/core-concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,32 @@ extend type Query {

There is no need to explictly define `Query` or `Mutation` base types anywhere; Apollo automatically handles this for you.

## Sharing Types
## Value Types

Sometimes, Enum or Scalar types are needed in multiple services. Rather than having a single service "own" those types, all services that use them are expected to share ownership of those types.
A natural overlap among identical types between services is not uncommon. Rather than having a single service "own" those types, all services that use them are expected to share ownership. This form of type "duplication" across services is supported for Scalars, Objects, Interfaces, Enums, Unions, and Inputs. The rule of thumb for any of these value types is that the types **must be identical** in name and contents.

Defining the same Enum or Scalar across services is supported **as long as the definitions are all identical**.
### Objects, Interfaces, and Inputs
For types with field definitions, all fields _and their types_ must be identical.

### Scalars
For Scalar values, it's important that services **share the same serialization and parsing logic**, since there is no way to validate that logic from the schema level by federation tooling.

### Enums
For Enum types, all values must match across services. **Even if a service doesn't use all values in an Enum, they still must be defined in the schema**. Failure to include all enum values in all services that use the Enum will result in a validation error when building the federated schema.

For scalar values, it's important that services **share the same serialization and parsing logic**, since there is no way to validate that logic from the schema level by federation tooling.
### Unions
Union types must share the same types in the union, even if not all types are used by a service.

In the following example, the Product and User services both use the same ProductCategory enum and Date scalar.
In the following example, the Product and User services both use the same `ProductCategory` enum, `Date` scalar, and `UPC` union.

```graphql
# Product Service
scalar Date

union UPC = String | ID
trevor-scheer marked this conversation as resolved.
Show resolved Hide resolved

type Product @key(fields: "sku"){
sku: ID!
sku: UPC!
category: ProductCategory
dateCreated: Date
}
Expand All @@ -196,17 +204,24 @@ enum ProductCategory {
# User Service
scalar Date

union UPC = String | ID

type User @key(fields: "id"){
id: ID!
dateCreated: Date
favoriteCategory: ProductCategory
favoriteProducts: [Product!]
}

enum ProductCategory {
FURNITURE
BOOK
DIGITAL_DOWNLOAD
}

extend type Product @key(fields: "sku"){
sku: UPC! @external
}
```


9 changes: 8 additions & 1 deletion docs/source/federation/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ Apollo Federation implements a strict composition model. When building a gateway
- `ROOT_MUTATION_USED`: `Mutation` is disallowed when a schema definition or extension is provided.
- `ROOT_SUBSCRIPTION_USED`: `Subscription` is disallowed when a schema definition or extension is provided.

### Value Types

- `VALUE_TYPE_FIELD_TYPE_MISMATCH`: Two identical types were found, however one or more of their field's types aren't the same. Value types must be identical across services.
- `VALUE_TYPE_NO_ENTITY`: Two identical types were found, however it's marked as an entity in one of the services. Extend the type correctly, or make the identical types non-entities.
- `VALUE_TYPE_UNION_TYPES_MISMATCH`: Two identitical union types were found, however their types are not the same. Shared union types must be identical across services.
- `VALUE_TYPE_KIND_MISMATCH`: A type was found with the same name and fields, however they aren't of the same kind (object, input, interface). Only value types can be duplicated across services.

### Modified SDL validations

- Unique Type Names: type definitions can not be duplicated across services, with the exception of Enums and Scalars. This is a modified version of the `graphql-js` validation with exclusions for Enums and Scalars, since those are required to be duplicated across services.
- Unique Type Names: type definitions can not be duplicated across services, with the exception of Enums, Scalars, and [value types](/core-concepts/#value-types). This is a modified version of the `graphql-js` validation with exclusions for Enums and Scalars, since those are required to be duplicated across services.
17 changes: 16 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"@types/koa-router": "7.0.42",
"@types/lodash": "4.14.136",
"@types/lodash.sortby": "4.7.6",
"@types/lodash.xorby": "4.7.6",
"@types/loglevel": "1.5.4",
"@types/lru-cache": "5.1.0",
"@types/memcached": "2.2.6",
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-federation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"dependencies": {
"apollo-env": "^0.5.1",
"apollo-graphql": "^0.3.3",
"apollo-server-env": "file:../apollo-server-env"
"apollo-server-env": "file:../apollo-server-env",
"lodash.xorby": "^4.7.0"
},
"peerDependencies": {
"graphql": "^14.0.2"
Expand Down
Loading