18.1.0 • Published 6 months ago

@whook/graphql v18.1.0

Weekly downloads
7
License
MIT
Repository
github
Last release
6 months ago

@whook/graphql

GraphQL implementation for Whook servers

GitHub license

Brings GraphQL to your Whook server!

This module uses Apollo under the hood. Most of its concepts (modules, plugins...) applies to it.

Quick setup

Install the module and its dependencies in your project:

npm i @whook/graphql graphql-tag

Update the types (usually in src/whook.d.ts):

+import type {
+   WhookGraphQLEnv,
+   WhookGraphQLConfig,
+} from '@whook/graphql';

// ...

declare module 'application-services' {

  export interface AppEnvVars
    extends BaseAppEnvVars,
      WhookBaseEnv,
      // (...)
+      WhookGraphQLEnv,
      WhookSwaggerUIEnv {}

  // (...)

  export interface AppConfig
    extends WhookBaseConfigs,
      // (...)
+      WhookGraphQLConfig,
      JWTServiceConfig {}

  // ...

}

Declare the plugin into your src/index.ts file:

+  import { gql } from 'graphql-tag';
+  import type { WhookGraphQLFragmentService } from '@whook/graphql';
  // (...)

+  // Add the Apollo Server configuration
+  $.register(constant('GRAPHQL_SERVER_OPTIONS', {
+    csrfPrevention: true,
+  }));

  // Setup your own whook plugins or avoid whook defaults by leaving it empty
  $.register(
    constant('WHOOK_PLUGINS', [
      ...WHOOK_DEFAULT_PLUGINS,
+      '@whook/graphql',
      '@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(
+    constant('graphQLFragments', [
+      helloFragment,
+    ]),
+  );

  // (...)

The GraphQL fragments can be declared into separated services for more readability, just create a service to gather them all (usually in src/services/graphQLFragments.ts):

import { initializer } from 'knifecycle';

export default initializer(
  {
    name: 'graphQLFragments',
    type: 'service',
    inject: ['graphQLUserFragment', 'graphQLMessageFragment'],
    singleton: true,
  },
  async (services) => Object.keys(services).map((key) => services[key]),
);

See this repository tests for more examples.

API

initGraphQL(services, ENV, graphQLFragments) ⇒ Promise

Initialize the GraphQL service

Kind: global function
Returns: Promise - A promise of a GraphQL service

ParamTypeDefaultDescription
servicesObjectThe services the server depends on
services.ENVObjectThe injected ENV value
services.GRAPHQL_SERVER_OPTIONSObject | functionThe GraphQL options to pass to the server
ENVStringThe process environment
graphQLFragmentsStringFragments of GraphQL schemas/resolvers declaration
services.logfunctionnoopA logging function

Authors

License

MIT

18.1.0

6 months ago

18.0.3

6 months ago

18.0.0

6 months ago

17.0.2

8 months ago

17.0.1

11 months ago

17.0.0

11 months ago

16.1.1

1 year ago

16.1.0

1 year ago

16.0.1

1 year ago

16.0.0

1 year ago

15.0.0

1 year ago

13.1.2

2 years ago

13.1.0

2 years ago

13.2.0

2 years ago

14.0.0

2 years ago

13.2.1

2 years ago

13.0.0

2 years ago

12.1.0

2 years ago

10.0.5

3 years ago

10.0.6

3 years ago

10.0.0

3 years ago

10.0.1

3 years ago

10.0.2

3 years ago

10.0.3

3 years ago

10.0.4

3 years ago

12.0.0

2 years ago

12.0.1

2 years ago

12.0.2

2 years ago

11.0.0

3 years ago

11.0.1

3 years ago

9.0.1

3 years ago

9.0.0

3 years ago

8.5.1

4 years ago

8.5.0

4 years ago

8.4.2

4 years ago

8.4.1

4 years ago

8.4.0

4 years ago

8.3.0

4 years ago

8.1.0

4 years ago

8.1.1

4 years ago

8.0.4

4 years ago

8.0.3

4 years ago

8.0.1

4 years ago

8.0.0

4 years ago

8.0.2

4 years ago

7.1.5

4 years ago

7.1.4

4 years ago

7.1.3

4 years ago

7.1.1

4 years ago

7.1.0

4 years ago

7.0.0

4 years ago

6.0.0

5 years ago

5.1.6

5 years ago

5.1.5

5 years ago

5.1.4

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.0

5 years ago

4.1.2

5 years ago

4.1.1

5 years ago

4.1.0

5 years ago

4.0.4

5 years ago

4.0.3

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

4.0.0-alpha.44

5 years ago

4.0.0-alpha.43

5 years ago

4.0.0-alpha.42

5 years ago

4.0.0-alpha.41

5 years ago

4.0.0-alpha.40

5 years ago

4.0.0-alpha.39

5 years ago

4.0.0-alpha.38

5 years ago

4.0.0-alpha.37

5 years ago

4.0.0-alpha.36

5 years ago