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

Commit

Permalink
Add GraphQL operation type (#2789)
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade authored Jun 10, 2024
1 parent 06dfac8 commit bdebd91
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
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

0 comments on commit bdebd91

Please sign in to comment.