Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Add GraphQL operation type #2789

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions .changeset/clean-pumpkins-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphql-mini-transforms': minor
'graphql-typed': minor
---

Add `type` field to `SimpleDocument` type
8 changes: 8 additions & 0 deletions packages/graphql-mini-transforms/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export function toSimpleDocument<
): SimpleDocument<Data, Variables, DeepPartial> {
return {
id: document.id,
type: operationTypeForDocument(document),
name: operationNameForDocument(document),
source: includeSource ? document.loc?.source?.body! : '',
};
Expand All @@ -120,6 +121,13 @@ export function formatDocument(document: DocumentNode, format: OutputFormat) {
}
}

function operationTypeForDocument(document: DocumentNode) {
return document.definitions.find(
(definition): definition is OperationDefinitionNode =>
definition.kind === 'OperationDefinition',
)?.operation;
}

function operationNameForDocument(document: DocumentNode) {
return document.definitions.find(
(definition): definition is OperationDefinitionNode =>
Expand Down
9 changes: 9 additions & 0 deletions packages/graphql-mini-transforms/src/tests/webpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@ describe('graphql-mini-transforms/webpack', () => {
);
});

it('has a type property that is the operation type of the first operation in the document', async () => {
const result = await extractDocumentExport(
`query Shop { shop { id } }`,
createLoaderContext({query: {format: 'simple'}}),
);

expect(result).toHaveProperty('type', 'query');
});

it('has a name property that is the name of the first operation', async () => {
const result = await extractDocumentExport(
`query Shop { shop { id } }`,
Expand Down
1 change: 1 addition & 0 deletions packages/graphql-typed/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface DocumentNode<Data = {}, Variables = {}, DeepPartial = {}>
export interface SimpleDocument<Data = {}, Variables = {}, DeepPartial = {}>
extends GraphQLOperation<Data, Variables, DeepPartial> {
readonly id: string;
readonly type?: 'query' | 'mutation' | 'subscription';
readonly name?: string;
readonly source: string;
}
Expand Down
Loading