-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f926b8
commit a169969
Showing
10 changed files
with
203 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { isOperationDefinitionNode } from '../lib/graphql.js' | ||
import { tryCatch } from '../lib/prelude.js' | ||
import type { RequestDocument } from './types.js' | ||
/** | ||
* Refactored imports from `graphql` to be more specific, this helps import only the required files (100KiB) | ||
* instead of the entire package (greater than 500KiB) where tree-shaking is not supported. | ||
* @see https://github.com/jasonkuhrt/graphql-request/pull/543 | ||
*/ | ||
import { type DocumentNode, OperationTypeNode } from 'graphql/language/ast.js' | ||
import { parse } from 'graphql/language/parser.js' | ||
import { print } from 'graphql/language/printer.js' | ||
|
||
/** | ||
* helpers | ||
*/ | ||
|
||
const extractOperationName = (document: DocumentNode): string | undefined => { | ||
let operationName = undefined | ||
|
||
const defs = document.definitions.filter(isOperationDefinitionNode) | ||
|
||
if (defs.length === 1) { | ||
operationName = defs[0]!.name?.value | ||
} | ||
|
||
return operationName | ||
} | ||
|
||
const extractIsMutation = (document: DocumentNode): boolean => { | ||
let isMutation = false | ||
|
||
const defs = document.definitions.filter(isOperationDefinitionNode) | ||
|
||
if (defs.length === 1) { | ||
isMutation = defs[0]!.operation === OperationTypeNode.MUTATION | ||
} | ||
|
||
return isMutation | ||
} | ||
|
||
export const analyzeDocument = ( | ||
document: RequestDocument, | ||
excludeOperationName?: boolean, | ||
): { expression: string; operationName: string | undefined; isMutation: boolean } => { | ||
const expression = typeof document === `string` ? document : print(document) | ||
|
||
let isMutation = false | ||
let operationName = undefined | ||
|
||
if (excludeOperationName) { | ||
return { expression, isMutation, operationName } | ||
} | ||
|
||
const docNode = tryCatch(() => (typeof document === `string` ? parse(document) : document)) | ||
if (docNode instanceof Error) { | ||
return { expression, isMutation, operationName } | ||
} | ||
|
||
operationName = extractOperationName(docNode) | ||
isMutation = extractIsMutation(docNode) | ||
|
||
return { expression, operationName, isMutation } | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.