|
| 1 | +# Component Creation Patterns |
| 2 | + |
| 3 | +## Class Structure |
| 4 | + |
| 5 | +### Extending GraphQLComponent |
| 6 | +- Always extend `GraphQLComponent` class |
| 7 | +- Implement constructor with options spread pattern |
| 8 | +- Use TypeScript for type safety |
| 9 | + |
| 10 | +```typescript |
| 11 | +import GraphQLComponent from 'graphql-component'; |
| 12 | +import { types } from './types'; |
| 13 | +import { resolvers } from './resolvers'; |
| 14 | +import MyDataSource from './datasource'; |
| 15 | + |
| 16 | +export default class MyComponent extends GraphQLComponent { |
| 17 | + constructor({ dataSources = [new MyDataSource()], ...options } = {}) { |
| 18 | + super({ types, resolvers, dataSources, ...options }); |
| 19 | + } |
| 20 | +} |
| 21 | +``` |
| 22 | + |
| 23 | +### Constructor Pattern |
| 24 | +- Default empty object parameter: `= {}` |
| 25 | +- Default data sources with spread: `dataSources = [new MyDataSource()]` |
| 26 | +- Spread remaining options: `...options` |
| 27 | +- Pass all to super: `super({ types, resolvers, dataSources, ...options })` |
| 28 | + |
| 29 | +### Component References (for Delegation) |
| 30 | +- Store component instances as properties when needed for delegation |
| 31 | +- Initialize imported components in constructor |
| 32 | + |
| 33 | +```typescript |
| 34 | +export default class ListingComponent extends GraphQLComponent { |
| 35 | + propertyComponent: PropertyComponent; |
| 36 | + reviewsComponent: ReviewsComponent; |
| 37 | + |
| 38 | + constructor(options) { |
| 39 | + const propertyComponent = new PropertyComponent(); |
| 40 | + const reviewsComponent = new ReviewsComponent(); |
| 41 | + |
| 42 | + super({ |
| 43 | + types, |
| 44 | + resolvers, |
| 45 | + imports: [propertyComponent, reviewsComponent], |
| 46 | + ...options |
| 47 | + }); |
| 48 | + |
| 49 | + this.propertyComponent = propertyComponent; |
| 50 | + this.reviewsComponent = reviewsComponent; |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## File Organization |
| 56 | + |
| 57 | +### Standard Structure |
| 58 | +``` |
| 59 | +my-component/ |
| 60 | +├── index.ts # Component class (default export) |
| 61 | +├── types.ts # Schema loader |
| 62 | +├── resolvers.ts # Resolver map (named export) |
| 63 | +├── schema.graphql # GraphQL SDL |
| 64 | +└── datasource.ts # Data source class (default export) |
| 65 | +``` |
| 66 | + |
| 67 | +### Schema Loading Pattern |
| 68 | +- Use fs.readFileSync for .graphql files |
| 69 | +- Export as named export `types` |
| 70 | + |
| 71 | +```typescript |
| 72 | +// types.ts |
| 73 | +import fs from 'fs'; |
| 74 | +import path from 'path'; |
| 75 | + |
| 76 | +export const types = fs.readFileSync( |
| 77 | + path.resolve(path.join(__dirname, 'schema.graphql')), |
| 78 | + 'utf-8' |
| 79 | +); |
| 80 | +``` |
| 81 | + |
| 82 | +### Resolver Export Pattern |
| 83 | +- Export as named export `resolvers` |
| 84 | +- Use object literal format |
| 85 | + |
| 86 | +```typescript |
| 87 | +// resolvers.ts |
| 88 | +export const resolvers = { |
| 89 | + Query: { |
| 90 | + myField(_, args, context) { |
| 91 | + return context.dataSources.MyDataSource.getData(args.id); |
| 92 | + } |
| 93 | + } |
| 94 | +}; |
| 95 | +``` |
| 96 | + |
| 97 | +## Federation vs Composition |
| 98 | + |
| 99 | +### Composition Components |
| 100 | +- Use `imports` to include other components |
| 101 | +- Use `delegateToSchema` for cross-component calls |
| 102 | +- No federation flag needed |
| 103 | + |
| 104 | +```typescript |
| 105 | +const component = new GraphQLComponent({ |
| 106 | + types, |
| 107 | + resolvers, |
| 108 | + imports: [childComponent1, childComponent2] |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +### Federation Components |
| 113 | +- Set `federation: true` |
| 114 | +- Include federation directives in schema |
| 115 | +- Implement `__resolveReference` resolvers |
| 116 | + |
| 117 | +```typescript |
| 118 | +const component = new GraphQLComponent({ |
| 119 | + types, |
| 120 | + resolvers, |
| 121 | + dataSources: [new MyDataSource()], |
| 122 | + federation: true // Enable federation |
| 123 | +}); |
| 124 | +``` |
| 125 | + |
| 126 | +## Resolver Delegation |
| 127 | + |
| 128 | +### Cross-Component Calls |
| 129 | +- Use `delegateToSchema` from `@graphql-tools/delegate` |
| 130 | +- Reference component schema via `this.componentName.schema` |
| 131 | +- Pass through context and info |
| 132 | + |
| 133 | +```typescript |
| 134 | +import { delegateToSchema } from '@graphql-tools/delegate'; |
| 135 | + |
| 136 | +export const resolvers = { |
| 137 | + Listing: { |
| 138 | + property(root, args, context, info) { |
| 139 | + return delegateToSchema({ |
| 140 | + schema: this.propertyComponent.schema, |
| 141 | + fieldName: 'propertyById', |
| 142 | + args: { id: root.id }, |
| 143 | + context, |
| 144 | + info |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | +}; |
| 149 | +``` |
| 150 | + |
| 151 | +## Context Usage |
| 152 | + |
| 153 | +### Accessing Data Sources |
| 154 | +- Use destructuring: `{ dataSources }` from context |
| 155 | +- Access by class name: `dataSources.MyDataSource` |
| 156 | + |
| 157 | +```typescript |
| 158 | +const resolvers = { |
| 159 | + Query: { |
| 160 | + user(_, { id }, { dataSources }) { |
| 161 | + return dataSources.UserDataSource.getUser(id); |
| 162 | + } |
| 163 | + } |
| 164 | +}; |
| 165 | +``` |
| 166 | + |
| 167 | +### Federation Resolvers |
| 168 | +- Include `__resolveReference` for entity resolution |
| 169 | +- Use typed parameters for clarity |
| 170 | + |
| 171 | +```typescript |
| 172 | +const resolvers = { |
| 173 | + Property: { |
| 174 | + __resolveReference(ref: { id: string }, { dataSources }: ComponentContext) { |
| 175 | + return dataSources.PropertyDataSource.getPropertyById(ref.id); |
| 176 | + } |
| 177 | + } |
| 178 | +}; |
| 179 | +``` |
0 commit comments