16.1.0 • Published 1 month ago

@whook/graphql v16.1.0

Weekly downloads
7
License
MIT
Repository
github
Last release
1 month 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

16.1.0

1 month ago

16.0.1

2 months ago

16.0.0

2 months ago

15.0.0

2 months ago

13.1.2

8 months ago

13.1.0

8 months ago

13.2.0

7 months ago

14.0.0

6 months ago

13.2.1

7 months ago

13.0.0

9 months ago

12.1.0

12 months ago

10.0.5

2 years ago

10.0.6

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

10.0.2

2 years ago

10.0.3

2 years ago

10.0.4

2 years ago

12.0.0

1 year ago

12.0.1

1 year ago

12.0.2

1 year ago

11.0.0

1 year ago

11.0.1

1 year ago

9.0.1

2 years ago

9.0.0

2 years ago

8.5.1

3 years ago

8.5.0

3 years ago

8.4.2

3 years ago

8.4.1

3 years ago

8.4.0

3 years ago

8.3.0

3 years ago

8.1.0

3 years ago

8.1.1

3 years ago

8.0.4

3 years ago

8.0.3

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

8.0.2

3 years ago

7.1.5

3 years ago

7.1.4

3 years ago

7.1.3

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.0.0

3 years ago

5.1.6

3 years ago

5.1.5

3 years ago

5.1.4

3 years ago

5.1.3

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-alpha.44

4 years ago

4.0.0-alpha.43

4 years ago

4.0.0-alpha.42

4 years ago

4.0.0-alpha.41

4 years ago

4.0.0-alpha.40

4 years ago

4.0.0-alpha.39

4 years ago

4.0.0-alpha.38

4 years ago

4.0.0-alpha.37

4 years ago

4.0.0-alpha.36

4 years ago