Skip to content

Commit 7d2e7cc

Browse files
authored
chore: make graphql schema ready for hive federation project (#6679)
1 parent f2fe6c8 commit 7d2e7cc

File tree

8 files changed

+94
-23
lines changed

8 files changed

+94
-23
lines changed

codegen.mts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ const config: CodegenConfig = {
1212
typeDefsFilePath: false,
1313
mergeSchema: {
1414
path: '../../../../schema.graphql',
15-
config: { includeDirectives: true },
15+
config: {
16+
includeDirectives: true,
17+
append: '\n\n directive @oneOf on INPUT_OBJECT',
18+
},
1619
},
1720
resolverGeneration: 'minimal',
1821
resolverMainFileMode: 'modules',

deployment/utils/publish-graphql-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function publishGraphQLSchema(args: {
1515
}) {
1616
const command = (accessToken: string) =>
1717
`schema:publish` +
18-
` --registry.endpoint ${args.registry.endpoint} --registry.accessToken ${accessToken} --target ${args.registry.target}` +
18+
` --registry.endpoint ${args.registry.endpoint} --registry.accessToken ${accessToken} --target ${args.registry.target} --service graphql` +
1919
` --commit ${args.version.commit} --author "Hive CD" ./schema.graphqls`;
2020

2121
return new local.Command(

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@
137137
138138
"bentocache": "patches/bentocache.patch",
139139
"nextra": "patches/nextra.patch",
140-
"nextra-theme-docs": "patches/nextra-theme-docs.patch"
140+
"nextra-theme-docs": "patches/nextra-theme-docs.patch",
141+
"@graphql-codegen/schema-ast": "patches/@graphql-codegen__schema-ast.patch"
141142
}
142143
}
143144
}

packages/services/api/src/modules/shared/module.graphql.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default gql`
2121
2222
extend schema
2323
@link(url: "https://specs.apollo.dev/link/v1.0")
24-
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@tag", "@composeDirective"])
24+
@link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@tag", "@composeDirective"])
2525
@link(url: "https://github.com/graphql/graphql-spec/pull/825/v0.1", import: ["@oneOf"])
2626
@composeDirective(name: "@oneOf")
2727
@@ -32,7 +32,7 @@ export default gql`
3232
3333
directive @tag(
3434
name: String!
35-
) repeatable on FIELD_DEFINITION | INTERFACE | OBJECT | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION | SCHEMA
35+
) repeatable on FIELD_DEFINITION | INTERFACE | OBJECT | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
3636
3737
type Query {
3838
_: Boolean @tag(name: "public")
@@ -42,6 +42,10 @@ export default gql`
4242
_: Boolean @tag(name: "public")
4343
}
4444
45+
type Subscription {
46+
_: Boolean @tag(name: "public")
47+
}
48+
4549
type PageInfo {
4650
hasNextPage: Boolean! @tag(name: "public")
4751
hasPreviousPage: Boolean! @tag(name: "public")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { SubscriptionResolvers } from './../../../../__generated__/types';
2+
3+
export const _: NonNullable<SubscriptionResolvers['_']> = {
4+
// eslint-disable-next-line require-yield
5+
async *subscribe() {
6+
return;
7+
},
8+
};

packages/services/server/src/public-graphql-schema.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
1-
import { Kind, OperationTypeNode, parse, type DocumentNode } from 'graphql';
1+
import { parse, type DocumentNode } from 'graphql';
22
import { createSchema } from 'graphql-yoga';
33
import { mergeTypeDefs } from '@graphql-tools/merge';
44
import { type Registry } from '@hive/api';
55
import { composeFederationV2 } from '@hive/schema/src/lib/compose';
66
import { applyTagFilterOnSubgraphs } from '@hive/schema/src/lib/federation-tag-extraction';
77

8-
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
9-
108
/**
119
* Creates the public GraphQL schema from the private GraphQL Schema Registry definition.
1210
*/
1311
export function createPublicGraphQLSchema<TContext>(registry: Registry) {
1412
// Merge all modules type definitions into a single document node while excluding the `Subscription` type (for now)
1513
const documentNode: DocumentNode = mergeTypeDefs(registry.typeDefs, {
16-
// For now we exclude the subscription type
17-
exclusions: ['Subscription'],
1814
throwOnConflict: true,
1915
});
2016

21-
// We still have to remove the `Subscription` type from the [`schema`](https://spec.graphql.org/draft/#sec-Schema)
22-
for (const definition of documentNode.definitions) {
23-
if (definition.kind === Kind.SCHEMA_EXTENSION || definition.kind === Kind.SCHEMA_DEFINITION) {
24-
(definition as Writeable<typeof definition>).operationTypes =
25-
definition.operationTypes?.filter(
26-
ttype => ttype.operation !== OperationTypeNode.SUBSCRIPTION,
27-
);
28-
}
29-
}
30-
3117
// Use our tag filter logic for marking everything not tagged with `@tag(name: "public")` as @inaccessible
3218
const [filteredSubgraph] = applyTagFilterOnSubgraphs(
3319
[
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
diff --git a/cjs/index.js b/cjs/index.js
2+
index ed643f4440f514b0d933fea2d41d3fa456f99bd4..bcea9c4a71463bd46d20837a62d8b6d490b66c88 100644
3+
--- a/cjs/index.js
4+
+++ b/cjs/index.js
5+
@@ -4,13 +4,14 @@ exports.transformSchemaAST = exports.validate = exports.plugin = void 0;
6+
const path_1 = require("path");
7+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
8+
const graphql_1 = require("graphql");
9+
-const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation, }) => {
10+
+const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation, append = '' }) => {
11+
const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation, includeIntrospectionTypes });
12+
return [
13+
includeIntrospectionTypes ? (0, graphql_1.printIntrospectionSchema)(transformedSchemaAndAst.schema) : null,
14+
includeDirectives
15+
? (0, graphql_1.print)(transformedSchemaAndAst.ast)
16+
: graphql_1.printSchema(transformedSchemaAndAst.schema, { commentDescriptions }),
17+
+ append,
18+
]
19+
.filter(Boolean)
20+
.join('\n');
21+
diff --git a/esm/index.js b/esm/index.js
22+
index f386116b7c1931f84b7364ac6fcf743ccecafc5a..2d7e12c89722607ac62d64cd8fbbb5f3b332b297 100644
23+
--- a/esm/index.js
24+
+++ b/esm/index.js
25+
@@ -1,13 +1,14 @@
26+
import { extname } from 'path';
27+
import { getCachedDocumentNodeFromSchema, removeFederation, } from '@graphql-codegen/plugin-helpers';
28+
import { buildASTSchema, extendSchema, parse, print, printIntrospectionSchema, printSchema, visit, } from 'graphql';
29+
-export const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation, }) => {
30+
+export const plugin = async (schema, _documents, { commentDescriptions = false, includeDirectives = false, includeIntrospectionTypes = false, sort = false, federation, append = '' }) => {
31+
const transformedSchemaAndAst = transformSchemaAST(schema, { sort, federation, includeIntrospectionTypes });
32+
return [
33+
includeIntrospectionTypes ? printIntrospectionSchema(transformedSchemaAndAst.schema) : null,
34+
includeDirectives
35+
? print(transformedSchemaAndAst.ast)
36+
: printSchema(transformedSchemaAndAst.schema, { commentDescriptions }),
37+
+ append,
38+
]
39+
.filter(Boolean)
40+
.join('\n');
41+
diff --git a/typings/index.d.cts b/typings/index.d.cts
42+
index f2df25dbcbc5c7c0d0764452e06511dff8a1e896..083980b957de8b94ca9f77b801fcee34ef4189af 100644
43+
--- a/typings/index.d.cts
44+
+++ b/typings/index.d.cts
45+
@@ -27,6 +27,8 @@ export interface SchemaASTConfig {
46+
* ```
47+
*/
48+
includeDirectives?: boolean;
49+
+ /** Text to be appended at the end of the generated output. */
50+
+ append?: string;
51+
/**
52+
* @description Include introspection types to Schema output.
53+
* @default false
54+
diff --git a/typings/index.d.ts b/typings/index.d.ts
55+
index f2df25dbcbc5c7c0d0764452e06511dff8a1e896..083980b957de8b94ca9f77b801fcee34ef4189af 100644
56+
--- a/typings/index.d.ts
57+
+++ b/typings/index.d.ts
58+
@@ -27,6 +27,8 @@ export interface SchemaASTConfig {
59+
* ```
60+
*/
61+
includeDirectives?: boolean;
62+
+ /** Text to be appended at the end of the generated output. */
63+
+ append?: string;
64+
/**
65+
* @description Include introspection types to Schema output.
66+
* @default false

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)