You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In `schema` folder create a file `index.ts` with the following content which traverses `query`, `mutation`, `subscription` folders and create `GraphQLSchema` instance for you:
131
+
132
+
```ts
133
+
import { buildSchema } from 'graphql-compose-modules';
134
+
135
+
exportconstschema = buildSchema(module);
136
+
```
137
+
138
+
After that you may create a GraphQL server:
139
+
140
+
```ts
141
+
import { ApolloServer } from 'apollo-server';
142
+
import { schema } from './schema';
143
+
144
+
const server = new ApolloServer({ schema });
145
+
146
+
server.listen().then(({ url }) => {
147
+
console.log(`🚀 Server ready at ${url}`);
148
+
});
149
+
```
150
+
151
+
## Advanced GraphQLSchema construction
152
+
153
+
If you want transform AST of entrypoints (e.g. for adding authorization, logging, tracing) and for merging with another schemas distributed via npm packages – you may use the following advanced way:
// traverse `query`, `mutation`, `subscription` folders placed near this module
161
+
let ast =directoryToAst(module);
162
+
163
+
// apply transformer which uses astVisitor() method under the hood
164
+
addQueryToMutations(ast);
165
+
166
+
// merge with other ASTs distributed via npm packages
167
+
ast=astMerge(ast, remoteServiceAST);
168
+
169
+
// construct SchemaComposer
170
+
const sc =astToSchema(ast);
171
+
172
+
// construct GraphQLSchema instance and export it
173
+
exportconst schema =sc.buildSchema();
174
+
```
175
+
176
+
## Writing own transformer for entrypoints
177
+
178
+
For writing your own transformers you need to use `astVisitor()` method. As an example let's implement `addQueryToMutations` transformer which adds `query: Query` field to all your mutations:
-`directoryToAst(module: NodeModule, options: DirectoryToAstOptions): AstRootNode` – traverses directories and construct AST for your graphql entrypoints
0 commit comments