Skip to content

Commit 18d3143

Browse files
committed
fix #20
1 parent 1cde7b0 commit 18d3143

11 files changed

+169
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ State: Experimental
2727
- Serves schema for GraphQL gateway based on provided services & their versions
2828
- Validates new schema to be compatible with other running services
2929
- Validates that all client operations are supported by your schema
30-
- Produce a diff between your proposed schema and the current registry state
30+
- Produce a diff between your proposed schema and the current registry state to detect breaking changes and more
3131
- Lightweight authorization concept based on JWT.
3232

3333
[**Read more**](https://principledgraphql.com/integrity#3-track-the-schema-in-a-registry)

docs/api.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Composition stability
22

3-
Whenever a schema is pushed or fetched, Graph-Registry ensures that the schema is valid. You can't fetch or push an invalid schema.
3+
Whenever a schema is pushed or fetched, Graph-Registry ensures that the schema is valid. You can't fetch or produce an invalid registry state.
44

55
# Terminology
66

@@ -35,6 +35,8 @@ POST - `/schema/push` Creates a new graph and schema for a service. If you omit
3535

3636
**Notice:** A schema is normalized before it's stored in the database. Whitespaces are stipped and graphql elements are sorted lexicographically.
3737

38+
**Notice:** The schema isn't validated for breaking-changes. Use [schema diff](#produce-a-diff-from-your-schema) to understand the implications of your update.
39+
3840
<details>
3941
<summary>Example Request</summary>
4042
<p>
@@ -93,7 +95,7 @@ PUT - `/schema/deactivate` Deactivates a schema by id. The schema will no longer
9395

9496
### Produce a diff from your schema
9597

96-
POST - `/schema/diff` Returns the schema report of all services and the provided new schema.
98+
POST - `/schema/diff` Returns the schema report between provided and latest schemas.
9799

98100
<details>
99101
<summary>Example Request</summary>

package-lock.json

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"dependencies": {
6767
"@apollo/federation": "^0.25.0",
6868
"@graphql-inspector/core": "2.5.0",
69+
"@graphql-tools/utils": "^7.10.0",
6970
"ajv": "6.12.6",
7071
"env-schema": "^3.0.1",
7172
"fastify": "^3.15.1",

src/core/federation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function composeAndValidateSchema(servicesSchemaMap: ServiceSchema[]) {
1919

2020
return {
2121
name: schema.name,
22-
url: '',
22+
url: schema.url,
2323
typeDefs,
2424
}
2525
})

src/core/graphql-utils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import {
22
buildSchema,
33
GraphQLSchema,
44
lexicographicSortSchema,
5-
printSchema,
65
stripIgnoredCharacters,
76
} from 'graphql'
7+
import { printSchemaWithDirectives } from '@graphql-tools/utils'
88

99
export function normalize(typeDefs: string): string {
10-
const schema = buildSchema(typeDefs)
10+
const schema = buildSchema(typeDefs, { assumeValidSDL: true })
1111
return stripIgnoredCharacters(printSorted(schema))
1212
}
1313

1414
export function printSorted(schema: GraphQLSchema): string {
15-
return printSchema(lexicographicSortSchema(schema))
15+
const sorted = lexicographicSortSchema(schema)
16+
// without this the default implementation of "printSchema" would remove metadata like "directives"
17+
return printSchemaWithDirectives(sorted)
1618
}

src/migrations/20210504193054_initial_schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function up(knex: Knex): Promise<void> {
5050
.createTable(SchemaDBModel.table, (table) => {
5151
table.increments(SchemaDBModel.field('id')).primary()
5252

53-
table.string(SchemaDBModel.field('typeDefs'))
53+
table.text(SchemaDBModel.field('typeDefs'))
5454
table.boolean(SchemaDBModel.field('isActive')).notNullable().defaultTo(true)
5555
table
5656
.timestamp(SchemaDBModel.field('createdAt'), { useTz: true })

src/registry/federation/compose-schema-versions.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ test('Should return schema of two services', async (t) => {
9090
t.truthy(response.data[0].lastUpdatedAt)
9191
t.like(response.data[0], {
9292
serviceName: `${t.context.testPrefix}_foo`,
93-
typeDefs: 'type Query{hello:String}',
93+
typeDefs: 'schema{query:Query}type Query{hello:String}',
9494
version: '2',
9595
})
9696

9797
t.truthy(response.data[1].lastUpdatedAt)
9898
t.like(response.data[1], {
9999
serviceName: `${t.context.testPrefix}_bar`,
100-
typeDefs: 'type Query{world:String}',
100+
typeDefs: 'schema{query:Query}type Query{world:String}',
101101
version: '2',
102102
})
103103
})
@@ -327,7 +327,7 @@ test('Version "current" should always return the latest (not versioned) register
327327

328328
t.like(response.data[0], {
329329
serviceName: `${t.context.testPrefix}_foo`,
330-
typeDefs: `type Query{world:String}`,
330+
typeDefs: `schema{query:Query}type Query{world:String}`,
331331
version: CURRENT_VERSION,
332332
})
333333
})
@@ -374,7 +374,7 @@ test('Should include "routingUrl" of the service', async (t) => {
374374

375375
t.like(response.data[0], {
376376
serviceName: `${t.context.testPrefix}_foo`,
377-
typeDefs: 'type Query{hello:String}',
377+
typeDefs: 'schema{query:Query}type Query{hello:String}',
378378
routingUrl: 'http://localhost:3000/api/graphql',
379379
version: '1',
380380
})

src/registry/federation/compose-schema.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ test('Should return schema of two services', async (t) => {
5555
t.truthy(response.data[0].lastUpdatedAt)
5656
t.like(response.data[0], {
5757
serviceName: `${t.context.testPrefix}_bar`,
58-
typeDefs: 'type Query{world:String}',
58+
typeDefs: 'schema{query:Query}type Query{world:String}',
5959
version: '2',
6060
})
6161

6262
t.truthy(response.data[1].lastUpdatedAt)
6363
t.like(response.data[1], {
6464
serviceName: `${t.context.testPrefix}_foo`,
65-
typeDefs: `type Query{hello:String}`,
65+
typeDefs: `schema{query:Query}type Query{hello:String}`,
6666
version: '1',
6767
})
6868
})
@@ -136,7 +136,7 @@ test('Version "current" has no precedence over the last updated', async (t) => {
136136

137137
t.like(response.data[0], {
138138
serviceName: `${t.context.testPrefix}_foo`,
139-
typeDefs: 'type Query{world:String}',
139+
typeDefs: 'schema{query:Query}type Query{world:String}',
140140
version: '2',
141141
})
142142
})
@@ -177,7 +177,7 @@ test('Should include "routingUrl" of the service', async (t) => {
177177

178178
t.like(response.data[0], {
179179
serviceName: `${t.context.testPrefix}_foo`,
180-
typeDefs: 'type Query{hello:String}',
180+
typeDefs: 'schema{query:Query}type Query{hello:String}',
181181
routingUrl: 'http://localhost:3000/api/graphql',
182182
version: '1',
183183
})

0 commit comments

Comments
 (0)