@ailo/graphql-server

Ailo TypeScript starter

Usage no npm install needed!

<script type="module">
  import ailoGraphqlServer from 'https://cdn.skypack.dev/@ailo/graphql-server';
</script>

README

@ailo/graphql-server

Ailo GraphQL server to be used in Ailo node.js services. Requires Koa as a HTTP server.

Usage

Setup

  1. Install dependencies

    yarn add @ailo/graphql-server graphql
    yarn add -D apollo-server-testing
    
  2. Setup GraphQL Server

    // src/api/graphqlServer.ts
    import { Logger } from "local/app/utils/logger";
    import { monitoring } from "local/app/utils/monitoring";
    import { GraphQLServer, BaseCommonSchemaModule, AuthZCommonSchemaModule } from "@ailo/graphql-server";
    
    export const consumerGraphqlServer = new GraphQLServer({
      schema: { modules: [BaseCommonSchemaModule, AuthZCommonSchemaModule, ...] },
      logger: Logger.logAs(`gql.consumer`),
      monitoring,
    });
    
    // src/app/app.ts
    const app = new Koa();
    consumerGraphqlServer.applyMiddleware({ app, path: "/consumer/graphql" });
    
  3. Setup @graphql-codegen/cli:

    • add following schema files to the beginning of your generates[*].schema[]:

      "schema": [
        "node_modules/@ailo/graphql-server/build/module/commonSchemaModules/base.js",
        "node_modules/@ailo/graphql-server/build/module/commonSchemaModules/authz.js",
        ...
      ]
      
    • add following scalars to config.scalars:

      "scalars": {
        "TimeZone": "string",
        "AiloRN": "@ailo/ailorn#AiloRN",
      },
      
  • if you use SortDirection in your schema, add the following to each of your schema under generates[*].config.enumValues:

    "config": {
      "enumValues": {
         "SortDirection": "@ailo/graphql-server#SortDirection",
         ...
      }
    }
    

How to write integration tests for code using @ailo/graphql-server ? (example)

// src/api/some-query.integration.test.ts
import { mockGraphQLServer, gql } from "@ailo/graphql-server";
import { modules } from "./schema";

it("updateName mutation updates the name", async () => {
  const { query } = mockGraphQLServer({ modules });
  const response = await query({
    query: gql`
      mutation($name: String!) {
        updateName(name: $name) {
          name
        }
      }
    `,
    variables: {
      name: "new name",
    },
  });
  expect(response.data.updateName).toMatchObject({
    name: "new name",
  });
});

How to resolve ActionInitiator ? (example)

import { gql, resolveActionInitiatorFromAiloRN } from "@ailo/graphql-server";

const typeDefs = gql`
  type Supplier {
    createdBy: ActionInitiator
    modifiedBy: ActionInitiator
    archivedBy: ActionInitiator
  }
`;

const resolvers: GraphQLResolvers = {
  Supplier: {
    createdBy: (supplier) =>
      resolveActionInitiatorFromAiloRN(supplier.createdBy),
    modifiedBy: (supplier) =>
      resolveActionInitiatorFromAiloRN(supplier.modifiedBy),
    archivedBy: (supplier) =>
      resolveActionInitiatorFromAiloRN(supplier.archivedBy),
  },
};

export const module = { typeDefs, resolvers };

Development

yarn
yarn start

Testing

yarn lint # prettier and eslint
yarn test # unit tests
yarn test:watch # unit tests in watch mode

E2E Test

yarn test:e2e
graphqurl http://localhost:8080/consumer/graphql -i

Releasing

yarn release # will automatically ask you about version bump, run tests and build, and push new version to git & npm