Skip to content

Commit

Permalink
Adds CompositionSuccess.publicSdl - SDL with only the queryable fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Feb 26, 2024
1 parent 7d797fe commit be912f4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/young-dingos-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@theguild/federation-composition': minor
---

Adds CompositionSuccess.publicSdl - SDL with only the queryable fields
78 changes: 78 additions & 0 deletions __tests__/supergraph-composition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,58 @@ testImplementations(api => {
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).toContainGraphQL(/* GraphQL */ `
type Product {
id: ID!
name: String!
reviews: [Review!]!
}
`);
}

expect(result.supergraphSdl).toContainGraphQL(/* GraphQL */ `
type Review @join__type(graph: REVIEWS, key: "id") {
id: ID!
body: String!
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).toContainGraphQL(/* GraphQL */ `
type Review {
id: ID!
body: String!
}
`);
}

expect(result.supergraphSdl).toContainGraphQL(/* GraphQL */ `
type Query @join__type(graph: PRODUCTS) @join__type(graph: REVIEWS) {
products: [Product!]! @join__field(graph: PRODUCTS)
reviews: [Review!]! @join__field(graph: REVIEWS)
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).toContainGraphQL(/* GraphQL */ `
type Query {
products: [Product!]!
reviews: [Review!]!
}
`);
}

expect(result.supergraphSdl).toContainGraphQL(/* GraphQL */ `
enum join__Graph {
PRODUCTS @join__graph(name: "products", url: "")
REVIEWS @join__graph(name: "reviews", url: "")
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).not.toEqual(expect.stringContaining('join__Graph'));
}
});

test('composition of basic object types with @requires, @provides, @key', () => {
Expand Down Expand Up @@ -159,6 +191,20 @@ testImplementations(api => {
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).toContainGraphQL(/* GraphQL */ `
type Product {
upc: String!
weight: Int
price: Int
inStock: Boolean
shippingEstimate: Int
name: String
reviews: [Review]
}
`);
}

expect(result.supergraphSdl).toContainGraphQL(/* GraphQL */ `
type User @join__type(graph: ACCOUNTS, key: "id") @join__type(graph: REVIEWS, key: "id") {
id: ID!
Expand All @@ -168,6 +214,17 @@ testImplementations(api => {
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).toContainGraphQL(/* GraphQL */ `
type User {
id: ID!
name: String
username: String
reviews: [Review]
}
`);
}

expect(result.supergraphSdl).toContainGraphQL(/* GraphQL */ `
type Review @join__type(graph: REVIEWS, key: "id") {
id: ID!
Expand All @@ -176,6 +233,17 @@ testImplementations(api => {
product: Product
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).toContainGraphQL(/* GraphQL */ `
type Review {
id: ID!
body: String
author: User
product: Product
}
`);
}
});

test('[Fed v1] set @join__type(extension: true) to types with @extends', () => {
Expand Down Expand Up @@ -355,6 +423,16 @@ testImplementations(api => {
price: Int @join__field(graph: FOO)
}
`);

if (api.library === 'guild') {
expect(result.publicSdl).toContainGraphQL(/* GraphQL */ `
type Product {
id: ID!
variants: [Variant!]!
price: Int
}
`);
}
});

test('[Fed v1] @override', () => {
Expand Down
21 changes: 21 additions & 0 deletions src/compose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GraphQLError, Kind } from 'graphql';
import { print } from './graphql/printer.js';
import { transformSupergraphToPublicSchema } from './graphql/transform-supergraph-to-public-schema.js';
import { sdl as authenticatedSDL } from './specifications/authenticated.js';
import { sdl as inaccessibleSDL } from './specifications/inaccessible.js';
import { sdl as joinSDL } from './specifications/join.js';
Expand Down Expand Up @@ -59,6 +60,8 @@ export function composeServices(
const usedRequiresScopesSpec = validationResult.specs.requiresScopes;
const usedAuthenticatedSpec = validationResult.specs.authenticated;

let _publicSdl: string;

return {
supergraphSdl: `
schema
Expand Down Expand Up @@ -101,6 +104,23 @@ ${print({
definitions: validationResult.supergraph,
})}
`,
/**
*
*/
get publicSdl() {
if (_publicSdl) {
return _publicSdl;
}

_publicSdl = print(
transformSupergraphToPublicSchema({
kind: Kind.DOCUMENT,
definitions: validationResult.supergraph,
}),
);

return _publicSdl;
},
};
}

Expand All @@ -113,6 +133,7 @@ export interface CompositionFailure {

export interface CompositionSuccess {
supergraphSdl: string;
publicSdl: string;
errors?: undefined;
}

Expand Down

0 comments on commit be912f4

Please sign in to comment.