Skip to content

Commit

Permalink
feat(@whook/example): add GraphQL server
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Feb 24, 2021
1 parent be3b48d commit 3f0609d
Show file tree
Hide file tree
Showing 19 changed files with 14,093 additions and 4,536 deletions.
18,130 changes: 13,616 additions & 4,514 deletions package-lock.json

Large diffs are not rendered by default.

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions packages/whook-create/src/services/createWhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/graphiql": "^7.1.5",
"@whook/graphql": "^7.1.5",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.0",
"graphql": "^15.5.0",
"http-auth-utils": "^3.0.0",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
Expand Down Expand Up @@ -441,11 +444,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/graphiql": "^7.1.5",
"@whook/graphql": "^7.1.5",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.0",
"graphql": "^15.5.0",
"http-auth-utils": "^3.0.0",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
Expand Down Expand Up @@ -681,11 +687,14 @@ Mr Bean
"@whook/authorization": "<current_version>",
"@whook/cli": "<current_version>",
"@whook/cors": "<current_version>",
"@whook/graphiql": "^7.1.5",
"@whook/graphql": "^7.1.5",
"@whook/http-router": "<current_version>",
"@whook/http-transaction": "<current_version>",
"@whook/swagger-ui": "<current_version>",
"@whook/whook": "<current_version>",
"common-services": "^9.0.0",
"graphql": "^15.5.0",
"http-auth-utils": "^3.0.0",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,14 @@
"@whook/authorization": "^7.1.5",
"@whook/cli": "^7.1.5",
"@whook/cors": "^7.1.5",
"@whook/graphql": "^7.1.5",
"@whook/graphiql": "^7.1.5",
"@whook/http-router": "^7.1.5",
"@whook/http-transaction": "^7.1.3",
"@whook/swagger-ui": "^7.1.5",
"@whook/whook": "^7.1.5",
"common-services": "^9.0.0",
"graphql": "^15.5.0",
"http-auth-utils": "^3.0.0",
"jwt-service": "^7.1.0",
"knifecycle": "^11.1.0",
Expand Down
3 changes: 3 additions & 0 deletions packages/whook-example/src/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Object {
- printEnv: A command printing every env values
# Provided by \\"@whook/graphql\\": none
# Provided by \\"@whook/cli\\": 6 commands
- config: A simple program that returns the queryed config value
- create: A command helping to create new Whook files easily
Expand Down
187 changes: 187 additions & 0 deletions packages/whook-example/src/__snapshots__/index.test.ts.snap

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions packages/whook-example/src/config/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { WhookAPIOperationCORSConfig, WhookCORSConfig } from '@whook/cors';
import type { WhookAPIHandlerDefinition, WhookConfigs } from '@whook/whook';
import type { APIConfig } from '../../services/API';
import type { JWTServiceConfig } from 'jwt-service';
import type { WhookGraphQLConfig } from '@whook/graphql';
import type { WhookGraphIQLConfig } from '@whook/graphiql';

// eslint-disable-next-line
const packageConf = require('../../../package');
Expand All @@ -19,8 +21,10 @@ export type AppConfigs = WhookConfigs &
WhookAuthorizationConfig &
WhookSwaggerUIConfig &
WhookCORSConfig &
APIConfig &
JWTServiceConfig;
JWTServiceConfig &
WhookGraphQLConfig &
WhookGraphIQLConfig &
APIConfig;

// Export custom handlers definitions
export type APIHandlerDefinition = WhookAPIHandlerDefinition<
Expand Down Expand Up @@ -66,6 +70,10 @@ const CONFIG: AppConfigs = {
'User-Agent',
].join(','),
},
GRAPHIQL: {
path: '/graphiql',
defaultQuery: '{ hello }',
},
};

export default CONFIG;
1 change: 1 addition & 0 deletions packages/whook-example/src/config/development/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { AppConfigs } from '../common/config';

const CONFIG: AppConfigs = {
...COMMON_CONFIG,
DEV_ACCESS_MECHANISM: 'Fake',
DEV_ACCESS_TOKEN: 'admin|1|1',
DEFAULT_MECHANISM: 'Fake',
// This allows you to map service names depending on
Expand Down
21 changes: 21 additions & 0 deletions packages/whook-example/src/handlers/getGraphQL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
initGetGraphQL,
getGraphQLDefinition as baseDefinition,
} from '@whook/graphql';

// Add authentication to the base getGraphQL endpoints that
// would otherwise be public per default
export const definition = {
...baseDefinition,
operation: {
...baseDefinition.operation,

security: [
{
bearerAuth: ['admin'],
},
],
},
};

export default initGetGraphQL;
21 changes: 21 additions & 0 deletions packages/whook-example/src/handlers/postGraphQL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
initPostGraphQL,
postGraphQLDefinition as baseDefinition,
} from '@whook/graphql';

// Add authentication to the base postGraphQL endpoints that
// would otherwise be public per default
export const definition = {
...baseDefinition,
operation: {
...baseDefinition.operation,

security: [
{
bearerAuth: ['admin'],
},
],
},
};

export default initPostGraphQL;
37 changes: 35 additions & 2 deletions packages/whook-example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
import initHTTPRouter from '@whook/http-router';
import wrapHTTPRouterWithSwaggerUI from '@whook/swagger-ui';
import YError from 'yerror';
import wrapHTTPRouterWithGraphIQL from '@whook/graphiql';
import { initGraphQL } from '@whook/graphql';
import { gql } from 'apollo-server-core';
import type { DependencyDeclaration, Dependencies } from 'knifecycle';
import type { WhookGraphQLFragmentService } from '@whook/graphql';

// Per convention a Whook server main file must export
// the following 3 functions to be composable:
Expand Down Expand Up @@ -43,7 +47,11 @@ export async function prepareServer<
// Add here any logic bound to the server only
// For example, here we add a Swagger UI page for
// development purpose
$.register(wrapHTTPRouterWithSwaggerUI(initHTTPRouter) as any);
$.register(
wrapHTTPRouterWithGraphIQL(
wrapHTTPRouterWithSwaggerUI(initHTTPRouter),
) as any,
);

return await prepareBaseServer(injectedNames, $);
}
Expand Down Expand Up @@ -80,9 +88,34 @@ export async function prepareEnvironment<T extends Knifecycle<Dependencies>>(

// Setup your own whook plugins or avoid whook defaults by leaving it empty
$.register(
constant('WHOOK_PLUGINS', ['@whook/cli', '@whook/whook', '@whook/cors']),
constant('WHOOK_PLUGINS', [
'@whook/graphql',
'@whook/cli',
'@whook/whook',
'@whook/cors',
]),
);

// Declare the GraphQL schema fragments
const helloFragment: WhookGraphQLFragmentService = {
typeDefs: gql`
type Query {
hello: String
}
schema {
query: Query
}
`,
resolvers: {
Query: {
hello: () => 'Hello world!',
},
},
};

$.register(initGraphQL);
$.register(constant('graphQLFragments', [helloFragment]));

return $;
}

Expand Down
1 change: 1 addition & 0 deletions packages/whook-example/src/services/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { noop } from '@whook/whook';
import type { WhookConfig, WhookAPIDefinitions } from '@whook/whook';
import type { LogService } from 'common-services';
import type { OpenAPIV3 } from 'openapi-types';
import { getGraphQLDefinition, postGraphQLDefinition } from '@whook/graphql';

export type APIEnv = {
DEV_MODE?: string;
Expand Down
Loading

0 comments on commit 3f0609d

Please sign in to comment.