Utilities for manipulating GraphQL fragments and converting them into executable query documents.
Converts a fragment document into a query document by wrapping the fragment in a query operation that spreads the fragment.
/**
* Convert a fragment document into an executable query document
* @param document - GraphQL document containing fragment definitions
* @param fragmentName - Name of specific fragment to use (optional if document has only one fragment)
* @returns Query document that spreads the specified fragment
* @throws Error if document contains operations or if fragmentName is needed but not provided
*/
function getFragmentQueryDocument(
document: DocumentNode,
fragmentName?: string
): DocumentNode;Usage Examples:
import { getFragmentQueryDocument } from "apollo-utilities";
import { parse } from "graphql";
// Single fragment document
const fragmentDoc = parse(`
fragment UserInfo on User {
id
name
email
}
`);
const queryDoc = getFragmentQueryDocument(fragmentDoc);
console.log(print(queryDoc));
// query {
// ...UserInfo
// }
//
// fragment UserInfo on User {
// id
// name
// email
// }
// Multiple fragments document - need to specify fragment name
const multiFragmentDoc = parse(`
fragment UserInfo on User {
id
name
email
}
fragment PostInfo on Post {
id
title
content
}
`);
const userQueryDoc = getFragmentQueryDocument(multiFragmentDoc, "UserInfo");
console.log(print(userQueryDoc));
// query {
// ...UserInfo
// }
//
// fragment UserInfo on User {
// id
// name
// email
// }
//
// fragment PostInfo on Post {
// id
// title
// content
// }Error Handling:
The function will throw an InvariantError in these cases:
fragmentName is providedfragmentName doesn't exist in the document// This will throw an error because it contains a query operation
const invalidDoc = parse(`
query GetUser {
user { name }
}
fragment UserInfo on User {
id
name
}
`);
try {
getFragmentQueryDocument(invalidDoc);
} catch (error) {
console.log(error.message);
// "Found a query operation named 'GetUser'. No operations are allowed when using a fragment as a query."
}
// This will throw an error because fragmentName is required for multiple fragments
const multiDoc = parse(`
fragment UserInfo on User { id name }
fragment PostInfo on Post { id title }
`);
try {
getFragmentQueryDocument(multiDoc); // Missing fragmentName
} catch (error) {
console.log(error.message);
// "Found 2 fragments. `fragmentName` must be provided when there is not exactly 1 fragment."
}Convert fragments into executable queries for testing:
import { getFragmentQueryDocument } from "apollo-utilities";
import { execute } from "graphql";
const fragment = parse(`
fragment UserProfile on User {
id
name
email
profile {
bio
avatar
}
}
`);
// Convert to executable query
const query = getFragmentQueryDocument(fragment);
// Now you can execute it against a GraphQL schema
const result = await execute({
schema: mySchema,
document: query,
rootValue: { user: mockUserData }
});Combine fragments with operations for complex queries:
import { getFragmentQueryDocument } from "apollo-utilities";
const baseFragment = parse(`
fragment BaseUser on User {
id
name
}
`);
// Get the query version of the fragment
const fragmentQuery = getFragmentQueryDocument(baseFragment);
// The resulting query can be further composed or analyzed
const operation = getOperationDefinition(fragmentQuery);