Skip to content

Commit

Permalink
merge duplicate schema definitions if we've skipped compilation for fed1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashi Krishnan committed Apr 11, 2022
1 parent 418609d commit c68e501
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 146 deletions.
16 changes: 0 additions & 16 deletions subgraph-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,6 @@

This CHANGELOG pertains only to Apollo Federation packages in the 2.x range. The Federation v0.x equivalent for this package can be found [here](https://github.com/apollographql/federation/blob/version-0.x/subgraph-js/CHANGELOG.md) on the `version-0.x` branch of this repo.

## v2.0.0

- Previous preview release promoted to general availability! Please see previous changelog entries for full info.

## v2.0.0-preview.14

- Implement `buildSubgraphSchema` using federation internals [PR #1697](https://github.com/apollographql/federation/pull/1697)

## v2.0.0-preview.13

- Revert previous `@apollo/core-schema` update due to incopatibilities with some existing schemas [PR #1704](https://github.com/apollographql/federation/pull/1704)

## v2.0.0-preview.12

- Generate a core schema in `buildSubgraphSchema`, incorporating the latest changes from `@apollo/core-schema` [PR #1554](https://github.com/apollographql/federation/pull/1554)

## v2.0.0-preview.11

- Released in sync with other federation packages but no changes to this package.
Expand Down
6 changes: 6 additions & 0 deletions subgraph-js/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ module.exports = {
name: "@apollo/subgraph",
color: "blue",
},
snapshotSerializers: [
'@apollo/core-schema/dist/snapshot-serializers/ast',
'@apollo/core-schema/dist/snapshot-serializers/raw',
'@apollo/core-schema/dist/snapshot-serializers/gref',
'@apollo/core-schema/dist/snapshot-serializers/redirect',
]
};
8 changes: 4 additions & 4 deletions subgraph-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apollo/subgraph",
"version": "2.0.0",
"version": "2.0.0-preview.11",
"description": "Apollo Subgraph Utilities",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -20,12 +20,12 @@
"engines": {
"node": ">=12.13.0 <18.0"
},
"dependencies": {
"@apollo/core-schema": "^0.3"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@apollo/federation-internals": "file:../internals-js"
},
"peerDependencies": {
"graphql": "^16.0.0"
}
Expand Down
47 changes: 47 additions & 0 deletions subgraph-js/src/__tests__/buildSubgraphSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DocumentNode,
execute,
type DefinitionNode,
OperationTypeNode,
} from 'graphql';
import { buildSubgraphSchema } from '../buildSubgraphSchema';
import { typeSerializer } from 'apollo-federation-integration-testsuite';
Expand Down Expand Up @@ -741,4 +742,50 @@ describe('legacy interface', () => {
`union _Entity = Product`,
);
});
it('does not break if the schema definition AST uses undefined for directives', () => {
// This AST is equilvalent to:
// schema {
// query: Query
// }
//
// type Query {
// test: String
// }
//
// but the AST used `undefined` for fields not present, where `gql` applied
// to the SDL above would use empty arrays instead. Of course, both are
// valid and this shouldn't make a different, but this has tripped code before,
// hence this test.
const doc: DocumentNode = {
kind: Kind.DOCUMENT,
definitions: [
{
kind: Kind.SCHEMA_DEFINITION,
operationTypes: [
{
kind: Kind.OPERATION_TYPE_DEFINITION,
operation: OperationTypeNode.QUERY,
type: { kind: Kind.NAMED_TYPE, name: { kind: Kind.NAME, value: 'Query' } }
},
]
},
{
kind: Kind.OBJECT_TYPE_DEFINITION,
name: { kind: Kind.NAME, value: 'Query' },
fields: [
{
kind: Kind.FIELD_DEFINITION,
name: { kind: Kind.NAME, value: 'test' },
type: {
kind: Kind.NAMED_TYPE,
name: { kind: Kind.NAME, value: 'String' }
},
},
]
},
],
};

expect(() => buildSubgraphSchema(doc)).not.toThrow();
});
});
32 changes: 3 additions & 29 deletions subgraph-js/src/__tests__/printSubgraphSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ describe('printSubgraphSchema', () => {
scalar JSON @specifiedBy(url: \\"https://json-spec.dev\\") @tag(name: \\"from-reviews\\")
type RootQuery {
user(id: ID!): User
me: User
_entities(representations: [_Any!]!): [_Entity]!
_service: _Service!
user(id: ID!): User
me: User
}
type PasswordAccount @key(fields: \\"email\\") {
Expand Down Expand Up @@ -76,18 +76,6 @@ describe('printSubgraphSchema', () => {
description: String @override(from: \\"books\\")
}
enum link__Purpose {
\\"\\"\\"
\`SECURITY\` features provide metadata necessary to securely resolve fields.
\\"\\"\\"
SECURITY
\\"\\"\\"
\`EXECUTION\` features provide metadata necessary for operation execution.
\\"\\"\\"
EXECUTION
}
scalar federation__FieldSet
"
`);
Expand All @@ -102,8 +90,6 @@ describe('printSubgraphSchema', () => {
expect(printSubgraphSchema(subgraphSchema)).toMatchInlineSnapshot(`
"scalar JSON
scalar _FieldSet
type Query {
_service: _Service!
}
Expand All @@ -121,9 +107,9 @@ describe('printSubgraphSchema', () => {
directive @transform(from: String!) on FIELD
type Query {
topReviews(first: Int = 5): [Review]
_entities(representations: [_Any!]!): [_Entity]!
_service: _Service!
topReviews(first: Int = 5): [Review]
}
type Review @key(fields: \\"id\\") {
Expand Down Expand Up @@ -208,18 +194,6 @@ describe('printSubgraphSchema', () => {
union MetadataOrError = KeyValue | Error
enum link__Purpose {
\\"\\"\\"
\`SECURITY\` features provide metadata necessary to securely resolve fields.
\\"\\"\\"
SECURITY
\\"\\"\\"
\`EXECUTION\` features provide metadata necessary for operation execution.
\\"\\"\\"
EXECUTION
}
scalar federation__FieldSet
"
`);
Expand Down
40 changes: 23 additions & 17 deletions subgraph-js/src/__tests__/subgraphCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ describe('subgraphCore', () => {
const result = getResult(() => subgraphCore(fixtures[0].typeDefs));
expect([...result.errors()]).toEqual([]);
expect(raw(print(result.unwrap()))).toMatchInlineSnapshot(`
schema {
query: RootQuery
mutation: Mutation
}
extend schema @link(url: "https://specs.apollo.dev/link/v1.0") @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key", "@requires", "@provides", "@external", "@tag", "@extends", "@shareable", "@inaccessible", "@override"])
directive @stream on FIELD
Expand All @@ -28,11 +33,6 @@ describe('subgraphCore', () => {
scalar JSON @tag(name: "from-reviews") @specifiedBy(url: "https://json-spec.dev")
schema {
query: RootQuery
mutation: Mutation
}
type RootQuery {
user(id: ID!): User
me: User @cacheControl(maxAge: 1000, scope: PRIVATE)
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('subgraphCore', () => {
directive @shareable on FIELD_DEFINITION | OBJECT
directive @inaccessible on OBJECT | INTERFACE | FIELD_DEFINITION
directive @inaccessible on OBJECT | INTERFACE | FIELD_DEFINITION | ENUM_VALUE
directive @override(from: String!) on FIELD_DEFINITION
Expand Down Expand Up @@ -204,13 +204,16 @@ describe('subgraphCore', () => {

expect([...Schema.from(document)]).toMatchInlineSnapshot(`
Array [
<>[+] schema {
query: RootQuery
mutation: Mutation
},
<#@stream>[GraphQL request] 👉directive @stream on FIELD,
<#@transform>[GraphQL request] 👉directive @transform(from: String!) on FIELD,
<#@tag>[GraphQL request] 👉directive @tag(,
<#CacheControlScope>[GraphQL request] 👉enum CacheControlScope @tag(name: "from-reviews") {,
<#@cacheControl>[GraphQL request] 👉directive @cacheControl(,
<#JSON>[GraphQL request] 👉scalar JSON,
<>[GraphQL request] 👉schema {,
<#RootQuery>[GraphQL request] 👉type RootQuery {,
<#PasswordAccount>[GraphQL request] 👉type PasswordAccount @key(fields: "email") {,
<#SMSAccount>[GraphQL request] 👉type SMSAccount @key(fields: "number") {,
Expand All @@ -231,6 +234,14 @@ describe('subgraphCore', () => {

expect([...Schema.from(document).refs]).toMatchInlineSnapshot(`
Array [
<>[+] schema {
query: RootQuery
mutation: Mutation
},
<>[+] query: RootQuery,
<#RootQuery>[GraphQL request] query: 👉RootQuery,
<>[+] mutation: Mutation,
<#Mutation>[GraphQL request] mutation: 👉Mutation,
<#@stream>[GraphQL request] 👉directive @stream on FIELD,
<#@transform>[GraphQL request] 👉directive @transform(from: String!) on FIELD,
<https://specs.graphql.org/#String>[GraphQL request] directive @transform(from: 👉String!) on FIELD,
Expand All @@ -246,11 +257,6 @@ describe('subgraphCore', () => {
<#JSON>[GraphQL request] 👉scalar JSON,
<#@tag>[GraphQL request] 👉@tag(name: "from-reviews"),
<https://specs.graphql.org/#@specifiedBy>[GraphQL request] 👉@specifiedBy(url: "https://json-spec.dev"),
<>[GraphQL request] 👉schema {,
<>[GraphQL request] 👉query: RootQuery,
<#RootQuery>[GraphQL request] query: 👉RootQuery,
<>[GraphQL request] 👉mutation: Mutation,
<#Mutation>[GraphQL request] mutation: 👉Mutation,
<#RootQuery>[GraphQL request] 👉type RootQuery {,
<https://specs.graphql.org/#ID>[GraphQL request] user(id: 👉ID!): User,
<#User>[GraphQL request] user(id: ID!): 👉User,
Expand Down Expand Up @@ -322,6 +328,11 @@ describe('subgraphCore', () => {
`);

expect(raw(print(document))).toMatchInlineSnapshot(`
schema {
query: RootQuery
mutation: Mutation
}
directive @stream on FIELD
directive @transform(from: String!) on FIELD
Expand All @@ -337,11 +348,6 @@ describe('subgraphCore', () => {
scalar JSON @tag(name: "from-reviews") @specifiedBy(url: "https://json-spec.dev")
schema {
query: RootQuery
mutation: Mutation
}
type RootQuery {
user(id: ID!): User
me: User @cacheControl(maxAge: 1000, scope: PRIVATE)
Expand Down
Loading

0 comments on commit c68e501

Please sign in to comment.