This guide provides practical examples of using the GraphQL Federation Schema Parser in various scenarios.
- Basic Usage
- Advanced Schema Transformations
- Federation Patterns
- CLI Examples
- Integration Examples
- Troubleshooting Examples
Given a basic schema file users.graphql:
type User {
id: ID!
name: String!
email: String!
}
type Query {
user(id: ID!): User
users: [User!]!
}Command:
gql-federation-schema-parser parse -d ./schemas -s users -n platformOutput:
type PlatformUsers__User {
id: ID!
name: String!
email: String!
}
type PlatformUsersQueries {
user(id: ID!): PlatformUsers__User
users: [PlatformUsers__User!]!
}
type PlatformQueries {
users: PlatformUsersQueries!
}
type Query {
platform: PlatformQueries!
}Directory structure:
schemas/
├── users.graphql
├── posts.graphql
└── comments.graphql
users.graphql:
type User {
id: ID!
name: String!
}
type Query {
user(id: ID!): User
}posts.graphql:
type Post {
id: ID!
title: String!
authorId: ID!
}
type Query {
post(id: ID!): Post
}Command:
gql-federation-schema-parser parse -d ./schemas -s content -n platformOutput:
type PlatformContent__User {
id: ID!
name: String!
}
type PlatformContent__Post {
id: ID!
title: String!
authorId: ID!
}
type PlatformContentQueries {
user(id: ID!): PlatformContent__User
post(id: ID!): PlatformContent__Post
}
type PlatformQueries {
content: PlatformContentQueries!
}
type Query {
platform: PlatformQueries!
}Input Schema:
scalar DateTime
scalar JSON
enum UserRole {
ADMIN
USER
GUEST
}
type User {
id: ID!
role: UserRole!
createdAt: DateTime!
metadata: JSON
}Command:
gql-federation-schema-parser parse -d ./schema.graphql -s users -n platformOutput:
scalar DateTime
scalar JSON
enum PlatformUsers__UserRole {
ADMIN
USER
GUEST
}
type PlatformUsers__User {
id: ID!
role: PlatformUsers__UserRole!
createdAt: DateTime!
metadata: JSON
}Input Schema:
interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
}
type Post implements Node {
id: ID!
title: String!
}
union SearchResult = User | Post
type Query {
node(id: ID!): Node
search(query: String!): [SearchResult!]!
}Output:
interface Node {
id: ID!
}
type PlatformSearch__User implements Node {
id: ID!
name: String!
}
type PlatformSearch__Post implements Node {
id: ID!
title: String!
}
union PlatformSearch__SearchResult = PlatformSearch__User | PlatformSearch__Post
type PlatformSearchQueries {
node(id: ID!): Node
search(query: String!): [PlatformSearch__SearchResult!]!
}Input Schema:
input CreateUserInput {
name: String!
email: String!
role: UserRole = USER
}
input UpdateUserInput {
name: String
email: String
role: UserRole
}
type User {
id: ID!
name: String!
email: String!
role: UserRole!
}
type Mutation {
createUser(input: CreateUserInput!): User!
updateUser(id: ID!, input: UpdateUserInput!): User!
deleteUser(id: ID!): Boolean!
}Output:
input PlatformUsers__CreateUserInput {
name: String!
email: String!
role: PlatformUsers__UserRole = USER
}
input PlatformUsers__UpdateUserInput {
name: String
email: String
role: PlatformUsers__UserRole
}
type PlatformUsers__User {
id: ID!
name: String!
email: String!
role: PlatformUsers__UserRole!
}
type PlatformUsersMutations {
createUser(input: PlatformUsers__CreateUserInput!): PlatformUsers__User!
updateUser(id: ID!, input: PlatformUsers__UpdateUserInput!): PlatformUsers__User!
deleteUser(id: ID!): Boolean!
}
type PlatformMutations {
users: PlatformUsersMutations!
}
type Mutation {
platform: PlatformMutations!
}Input Schema:
directive @auth(requires: String!) on FIELD_DEFINITION
type User {
id: ID!
name: String!
email: String! @auth(requires: "USER")
adminNotes: String @auth(requires: "ADMIN")
}
type Query {
user(id: ID!): User @auth(requires: "USER")
users: [User!]! @auth(requires: "ADMIN")
}Output:
directive @auth(requires: String!) on FIELD_DEFINITION
type PlatformUsers__User {
id: ID!
name: String!
email: String! @auth(requires: "USER")
adminNotes: String @auth(requires: "ADMIN")
}
type PlatformUsersQueries {
user(id: ID!): PlatformUsers__User @auth(requires: "USER")
users: [PlatformUsers__User!]! @auth(requires: "ADMIN")
}Service: User Service
gql-federation-schema-parser parse -d ./user-schemas -s users -n platformService: Post Service
gql-federation-schema-parser parse -d ./post-schemas -s posts -n platformService: Comment Service
gql-federation-schema-parser parse -d ./comment-schemas -s comments -n platformGateway Query:
query {
platform {
users {
user(id: "1") {
id
name
}
}
posts {
post(id: "1") {
id
title
}
}
comments {
comments(postId: "1") {
id
content
}
}
}
}E-commerce Platform:
# User management
gql-federation-schema-parser parse -d ./user-schemas -s users -n auth
# Product catalog
gql-federation-schema-parser parse -d ./product-schemas -s products -n catalog
# Order processing
gql-federation-schema-parser parse -d ./order-schemas -s orders -n commerceCombined Query:
query {
auth {
users {
currentUser {
id
email
}
}
}
catalog {
products {
products(limit: 10) {
id
name
price
}
}
}
commerce {
orders {
userOrders {
id
status
total
}
}
}
}# Parse a single file
gql-federation-schema-parser parse -d ./schema.graphql -s users -n platform
# Parse a directory
gql-federation-schema-parser parse -d ./schemas -s users -n platform
# Parse with output to file
gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file
# Parse with custom output file
gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file -o custom-schema.graphql# Enable debug output
gql-federation-schema-parser parse -d ./schemas -s users -n platform -D
# Disable colors (useful for CI)
gql-federation-schema-parser parse -d ./schemas -s users -n platform -S# General help
gql-federation-schema-parser --help
# Parse command help
gql-federation-schema-parser parse --help
# Version information
gql-federation-schema-parser --versionpackage.json:
{
"scripts": {
"build:schema": "gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file",
"build:schema:debug": "gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file -D"
}
}GitHub Actions:
name: Build Schema
on:
push:
paths:
- 'schemas/**'
jobs:
build-schema:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install CLI
run: npm install -g @tiagoboeing/gql-federation-schema-parser
- name: Build Schema
run: gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file -S
- name: Upload Schema
uses: actions/upload-artifact@v2
with:
name: schema
path: users-schema.graphqlDockerfile:
FROM node:18-alpine
# Install CLI
RUN npm install -g @tiagoboeing/gql-federation-schema-parser
# Copy schemas
COPY schemas/ /app/schemas/
# Build schema
WORKDIR /app
RUN gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file
# Copy to output
COPY users-schema.graphql /output/Makefile:
.PHONY: build-schema
build-schema:
gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file
.PHONY: build-schema-debug
build-schema-debug:
gql-federation-schema-parser parse -d ./schemas -s users -n platform --write-to-file -D
.PHONY: validate-schema
validate-schema: build-schema
# Add schema validation commands hereIssue: "Path does not exist"
# Problem
gql-federation-schema-parser parse -d ./nonexistent -s users -n platform
# Solution: Check directory exists
ls -la ./schemas
gql-federation-schema-parser parse -d ./schemas -s users -n platformIssue: "No valid schema definitions found"
# Problem: No .graphql files found
gql-federation-schema-parser parse -d ./src -s users -n platform
# Solution: Check file extensions
find ./src -name "*.graphql"
# or
gql-federation-schema-parser parse -d ./src -s users -n platform -DIssue: Invalid GraphQL syntax
# Enable debug mode to see parsing errors
gql-federation-schema-parser parse -d ./schemas -s users -n platform -DTest with minimal schema:
# test.graphql
type User {
id: ID!
}
type Query {
user: User
}gql-federation-schema-parser parse -d ./test.graphql -s test -n testTest with complex nested types:
type User {
id: ID!
posts: [Post!]!
}
type Post {
id: ID!
author: User!
comments: [Comment!]!
}
type Comment {
id: ID!
author: User!
post: Post!
}Large schema directory:
# Time the operation
time gql-federation-schema-parser parse -d ./large-schemas -s content -n platform
# With debug to see processing details
gql-federation-schema-parser parse -d ./large-schemas -s content -n platform -DMemory usage monitoring:
# Monitor memory usage during parsing
/usr/bin/time -v gql-federation-schema-parser parse -d ./schemas -s users -n platformschemas/
├── types/ # Type definitions
│ ├── user.graphql
│ ├── post.graphql
│ └── comment.graphql
├── inputs/ # Input objects
│ ├── user-inputs.graphql
│ └── post-inputs.graphql
├── enums/ # Enum definitions
│ └── common-enums.graphql
└── root/ # Root types
├── queries.graphql
└── mutations.graphql
- Use descriptive service names:
users,posts,orders - Use consistent namespace names:
platform,ecommerce,auth - Follow GraphQL naming conventions in your schemas
- Keep related types in the same file
- Separate inputs from outputs
- Use descriptive filenames
- Maintain consistent directory structure
- Always test with
-Dflag first - Validate GraphQL syntax before processing
- Check file permissions and paths
- Use version control for schema files