Skip to content

Commit a62c65c

Browse files
author
amarflybot18
committed
change for es + fed
0 parents  commit a62c65c

File tree

10 files changed

+3943
-0
lines changed

10 files changed

+3943
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.env*
2+
dist
3+
package-lock.json
4+
node_modules
5+
.idea
6+
.vscode
7+
*.log

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<h1 align="center"><strong>Boilerplate for a Minimal GraphQL Server w/ TypeScript</strong></h1>
2+
3+
<br />
4+
5+
<div align="center"><img src="https://imgur.com/1MfnLVl.png" /></div>
6+
7+
<div align="center"><strong>🚀 Bootstrap your GraphQL server within seconds</strong></div>
8+
<div align="center">Minimal starter kit for a flexible GraphQL server for TypeScript - based on best practices from the GraphQL community.</div>
9+
10+
## Features
11+
12+
- **Scalable GraphQL server:** The server uses [`graphql-yoga`](https://github.com/prisma/graphql-yoga) which is based on Apollo Server & Express
13+
- **Tooling**: Out-of-the-box support for [GraphQL Playground](https://github.com/prisma/graphql-playground) & [query performance tracing](https://github.com/apollographql/apollo-tracing)
14+
- **Simple Hello World example:** Where it either returns `Hello <name>!` or `Hello World!` if no name argument is provided.
15+
- **No configuration overhead**: Preconfigured [`graphql-config`](https://github.com/prisma/graphql-config) setup
16+
17+
Read more about the idea behind GraphQL boilerplates [here](https://blog.graph.cool/graphql-boilerplates-graphql-create-how-to-setup-a-graphql-project-6428be2f3a5).
18+
19+
## Requirements
20+
21+
You need to have the [GraphQL CLI](https://github.com/graphql-cli/graphql-cli) installed to bootstrap your GraphQL server using `graphql create`:
22+
23+
```sh
24+
npm install -g graphql-cli
25+
```
26+
27+
## Getting started
28+
29+
```sh
30+
# 1. Bootstrap GraphQL server in directory `my-app`, based on `typescript-basic` boilerplate
31+
graphql create my-app --boilerplate typescript-minimal
32+
33+
# 2. Run yarn install or npm install
34+
35+
# 3. Navigate to the new project
36+
cd my-app
37+
38+
# 4. Start server (runs on http://localhost:4000) and open GraphQL Playground
39+
yarn dev
40+
```
41+
42+
![](https://imgur.com/hElq68i.png)
43+
44+
## Documentation
45+
46+
### Commands
47+
48+
* `yarn start` or `npm run start` starts GraphQL server on `http://localhost:4000`
49+
* `yarn dev` or `npm run dev` starts GraphQL server on `http://localhost:4000` _and_ opens GraphQL Playground
50+
51+
### Project structure
52+
53+
| File name               | Description         <br><br>|
54+
| :-- | :-- |
55+
| `└── src ` (_directory_) | _Contains the source files for your GraphQL server_ |
56+
| `  ├── index.ts` | The entry point for your GraphQL server |
57+
58+
59+
## Contributing
60+
61+
The GraphQL boilerplates are maintained by the GraphQL community, with official support from the [Apollo](https://dev-blog.apollodata.com) & [Graphcool](https://blog.graph.cool/) teams.
62+
63+
Your feedback is **very helpful**, please share your opinion and thoughts! If you have any questions or want to contribute yourself, join the [`#graphql-boilerplate`](https://graphcool.slack.com/messages/graphql-boilerplate) channel on our [Slack](https://graphcool.slack.com/).

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "minimal",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.ts",
6+
"scripts": {
7+
"start": "ts-node src/index.ts",
8+
"dev": "npm-run-all --parallel start playground",
9+
"playground": "graphql playground"
10+
},
11+
"keywords": [],
12+
"author": "Amarendra Kumar",
13+
"license": "VIU",
14+
"dependencies": {
15+
"@apollo/federation": "^0.11.3",
16+
"@types/express": "^4.17.2",
17+
"@types/node": "^13.7.0",
18+
"altair-express-middleware": "^2.4.3",
19+
"apollo-graphql": "^0.4.0",
20+
"apollo-server-express": "^2.9.16",
21+
"elasticsearch": "^16.6.0",
22+
"express": "^4.17.1",
23+
"express-graphql": "^0.9.0",
24+
"graphql": "^14.6.0",
25+
"graphql-compose": "^7.9.0",
26+
"graphql-compose-elasticsearch": "^4.0.6",
27+
"graphql-playground-middleware-express": "^1.7.12",
28+
"graphql-voyager": "^1.0.0-rc.28",
29+
"pug": "^2.0.4"
30+
},
31+
"devDependencies": {
32+
"graphql-cli": "2.17.0",
33+
"npm-run-all": "4.1.5",
34+
"ts-node": "^6.2.0",
35+
"typescript": "^2.9.2"
36+
}
37+
}

src/buildFederatedSchema.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { DocumentNode, GraphQLSchema, specifiedDirectives } from 'graphql';
2+
import {
3+
buildSchemaFromSDL,
4+
GraphQLSchemaModule,
5+
modulesFromSDL,
6+
GraphQLResolverMap,
7+
} from 'apollo-graphql';
8+
9+
import 'apollo-server-env';
10+
import { transformFederatedSchema } from './transformFederatedSchema';
11+
import { extractFederationResolvers } from './extractFederationResolvers';
12+
import federationDirectives from "@apollo/federation/dist/directives";
13+
14+
export function buildFederatedSchema(
15+
modulesOrSDLOrSchema:
16+
| (GraphQLSchemaModule | DocumentNode)[]
17+
| DocumentNode
18+
| GraphQLSchema,
19+
): GraphQLSchema {
20+
// Extract federation specific resolvers from already constructed
21+
// GraphQLSchema and transform it to a federated schema.
22+
if (modulesOrSDLOrSchema instanceof GraphQLSchema) {
23+
return transformFederatedSchema(modulesOrSDLOrSchema, [
24+
extractFederationResolvers(modulesOrSDLOrSchema),
25+
]);
26+
}
27+
28+
// Transform *modules* or *sdl* into a federated schema.
29+
const modules = modulesFromSDL(modulesOrSDLOrSchema);
30+
31+
const resolvers = modules
32+
.filter(module => !!module.resolvers)
33+
.map(module => module.resolvers as GraphQLResolverMap<any>);
34+
35+
return transformFederatedSchema(
36+
buildSchemaFromSDL(
37+
modules,
38+
new GraphQLSchema({
39+
query: undefined,
40+
directives: [...specifiedDirectives, ...federationDirectives],
41+
}),
42+
),
43+
resolvers,
44+
);
45+
}

src/extractFederationResolvers.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { GraphQLSchema } from 'graphql';
2+
import { GraphQLResolverMap } from 'apollo-graphql';
3+
import {
4+
GraphQLReferenceResolver,
5+
ResolvableGraphQLObjectType,
6+
} from './types';
7+
8+
function extractFederationResolverForType(
9+
type: ResolvableGraphQLObjectType,
10+
): { __resolveReference: GraphQLReferenceResolver } | void {
11+
if (type.resolveReference) {
12+
return { __resolveReference: type.resolveReference };
13+
}
14+
}
15+
16+
export function extractFederationResolvers(
17+
schema: GraphQLSchema,
18+
): GraphQLResolverMap<any> {
19+
const map: GraphQLResolverMap<any> = {};
20+
21+
for (const [typeName, type] of Object.entries(schema.getTypeMap())) {
22+
const resolvers = extractFederationResolverForType(
23+
type as ResolvableGraphQLObjectType,
24+
);
25+
26+
if (resolvers) {
27+
map[typeName] = resolvers;
28+
}
29+
}
30+
31+
return map;
32+
}

0 commit comments

Comments
 (0)