How to modularize schema #4777
-
How to modularize schema using gql provided by apollo. How to write schema in separate file and stitch it type User { type Mission { enum PatchSize { |
Beta Was this translation helpful? Give feedback.
Answered by
KeithGillette
Dec 9, 2020
Replies: 1 comment
-
I built a simple service class to accomplish this in our Apollo Server project, but the logic of import { gql } from 'apollo-angular';
import { readdirSync, readFileSync, statSync } from 'fs';
import { join } from 'path';
import { DocumentNode } from 'graphql';
import { Service } from '../infrastructure/decorators/service.decorator';
const schemaFolder = 'assets/app/core/application/schema';
/** Injectable() service providing GraphQL schema array */
@Service()
export class SchemaService {
public getSchemas(): DocumentNode[] {
function buildFilePathList(directoryPath: string): string[] {
const directoryEntryList = readdirSync(directoryPath);
const filePathList = [];
for (const directoryEntry of directoryEntryList) {
const pathToEntry = join(directoryPath, directoryEntry);
const isDirectory = statSync(pathToEntry).isDirectory();
if (isDirectory) {
filePathList.push(...buildFilePathList(pathToEntry));
} else {
filePathList.push(pathToEntry);
}
}
return filePathList;
}
return buildFilePathList(schemaFolder)
.map((filePath: string) => {
return gql(readFileSync(filePath).toString());
});
}
} Call new ApolloServer({
typeDefs: this.schemaService.getSchemas(),
// …
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ssmilin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I built a simple service class to accomplish this in our Apollo Server project, but the logic of
getSchemas
should be all you need, since it's actually stateless.