0.6.3 • Published 12 days ago

@ttoss/graphql-api v0.6.3

Weekly downloads
-
License
-
Repository
github
Last release
12 days ago

@ttoss/graphql-api

This package provides an opinionated way to create an GraphQL API using ttoss ecosystem modules. The main goal of this package is to provide a resilient way to create a complex GraphQL API to meet the following goals:

  1. Modular: you can create your GraphQL API using modules, so you can reduce the complexity of a big GraphQL API.
  2. Relay: ttoss uses Relay as the main GraphQL client, so this package implements the Relay Server Specification.
  3. Build Schema: as Relay needs an introspection query to work, this package provides a way to build the GraphQL schema by running ttoss-graphl-api build-schema.
  4. Build TypeScript Types: this package provides a way to build the TypeScript types for your GraphQL schema by running ttoss-graphl-api build-schema.
  5. AppSync Support: this package provides a way to create a GraphQL API that works with AWS AppSync, besides you can also create a local GraphQL API server.

Installation

pnpm add @ttoss/graphql-api graphql

Quickstart

This library uses graphql-compose to create the GraphQL schema. It re-export all the graphql-compose types and methods, so you can use it directly from this package.

Type Creation

For more examples about how to create types, check the graphql-compose documentation.

import { schemaComposer } from '@ttoss/graphql-api';

const UserTC = schemaComposer.createObjectTC({
  name: 'User',
  fields: {
    id: 'ID!',
    name: 'String!',
  },
});

This library uses the tsconfig.json file from the target package it is being applied on. If you are using relative imports in your package you can skip this section, but, if you use path aliases in your typescript code by leveraging the paths property, the baseUrl must be filled accordingly.This is needed because in order to interpret the path aliases, ts-node uses tsconfig-paths to resolve the modules that uses this config, and tsconfig-paths needs both baseUrl and paths values to be non-null. A tsconfig.json example that follows such recommendations is given below:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "src/*": ["src/*"]
    }
  }
}

Resolvers

Integrate All Modules

Once you've created all your types and resolvers, you can integrate all the modules to create the GraphQL schema.

// scr/schemaComposer.ts
import { schemaComposer } from '@ttoss/graphql-api';
import './modules/Module1/composer';
import './modules/Module3/composer';
import './modules/User/composer';

export { schemaComposer };

Relay Server Specification

As ttoss uses Relay as the main GraphQL client, this library implements the Relay Server Specification.

Object Identification

Method composeWithRelay will handle the object identification for your ObjectTypeComposer, it will return a globally unique ID among all types in the following format base64(TypeName + ':' + recordId).

Method composeWithRelay only works if ObjectTypeComposer meets the following requirements:

  1. Has defined recordIdFn: returns the id for the globalId construction. For example, if you use DynamoDB, you could create id from hash and range keys:

    UserTC.setRecordIdFn((source) => {
      return `${source.hashKey}:${source.rangeKey}`;
    });
  2. Have findById resolver: this resolver will be used by RootQuery.node to resolve the object by globalId. For example:

    UserTC.addResolver({
      name: 'findById',
      type: UserTC,
      args: {
        id: 'String!',
      },
      resolve: ({ args }) => {
        const { type, recordId } = fromGlobalId(args.id);
        // find object
      },
    });

Example

import {
  composeWithRelay,
  schemaComposer,
  fromGlobalId,
} from '@ttoss/graphql-api';

const UserTC = schemaComposer.createObjectTC({
  name: 'User',
  fields: {
    id: 'ID!',
    name: 'String!',
  },
});

/**
 * 1. Returns you id for the globalId construction.
 */
UserTC.setRecordIdFn((source) => {
  /**
   * If you use DynamoDB, you could create id from hash and range keys:
   * return `${source.hashKey}:${source.rangeKey}`;
   */
  return source.id;
});

/**
 * 2. Define `findById` resolver (that will be used by `RootQuery.node`).
 */
UserTC.addResolver({
  name: 'findById',
  type: UserTC,
  args: {
    id: 'String!',
  },
  resolve: ({ args }) => {
    const { type, recordId } = fromGlobalId(args.id);
    // find object
  },
});

/**
 * 3. This will add the `id` field and the `node` query.
 */
composeWithRelay(UserTC);

We inspired ourselves on graphql-compose-relay to create composeWithRelay.

Connections

This packages provides the method composeWithConnection to create a connection type and queries for a given type, based on graphql-compose-connection plugin and following the Relay Connection Specification.

import { composeWithConnection } from '@ttoss/graphql-api';

AuthorTC.addResolver({
  name: 'findMany',
  type: AuthorTC,
  resolve: async ({ args }) => {
    // find many
  },
});

composeWithConnection(AuthorTC, {
  findManyResolver: AuthorTC.getResolver('findMany'),
  countResolver: AuthorTC.getResolver('count'),
  sort: {
    ASC: {
      value: {
        scanIndexForward: true,
      },
      cursorFields: ['id'],
      beforeCursorQuery: (rawQuery, cursorData, resolveParams) => {
        if (!rawQuery.id) rawQuery.id = {};
        rawQuery.id.$lt = cursorData.id;
      },
      afterCursorQuery: (rawQuery, cursorData, resolveParams) => {
        if (!rawQuery.id) rawQuery.id = {};
        rawQuery.id.$gt = cursorData.id;
      },
    },
    DESC: {
      value: {
        scanIndexForward: false,
      },
      cursorFields: ['id'],
      beforeCursorQuery: (rawQuery, cursorData, resolveParams) => {
        if (!rawQuery.id) rawQuery.id = {};
        rawQuery.id.$gt = cursorData.id;
      },
      afterCursorQuery: (rawQuery, cursorData, resolveParams) => {
        if (!rawQuery.id) rawQuery.id = {};
        rawQuery.id.$lt = cursorData.id;
      },
    },
  },
});

schemaComposer.Query.addFields({
  authors: Authors.getResolver('connection'),
});

When you composeWithConnection a type composer, it will add the resolver connection to the type composer, so you can add to Query or any other type composer. For example:

schemaComposer.Query.addFields({
  authors: Authors.getResolver('connection'),
});

The resolver connection has the following arguments based on the Relay Connection Specification:

  • first: the number of nodes to return.
  • after: the cursor to start the query.
  • last: the number of nodes to return.
  • before: the cursor to start the query.
  • limit: the limit of nodes to return. It's the first or last argument plus one. It's used to know if there are more nodes to return to set hasNextPage or hasPreviousPage PageInfo fields. For example, if first is 10, limit will be 11. If the resolver returns 11 nodes, the resolver will return 10 but it knows there are more nodes to return, so hasNextPage will be true.
  • skip: it's the count minus last. It only works on backward pagination.
  • sort: the sort option to use. It's the value of the sort object. In our example, it's { scanIndexForward: true } for ASC and { scanIndexForward: false }, for DESC.
  • filter: the filter to use. It'll exist if you add the filter to findManyResolver for example, the implementation below will add the filter argument with the name and book fields:

    AuthorTC.addResolver({
      name: 'findMany',
      type: AuthorTC,
      args: {
        filter: {
          name: 'String',
          book: 'String',
        },
      },
      resolve: async ({ args }) => {
        // find many
      },
    });

To configure composeWithConnection, you need to provide the following options:

findManyResolver

The resolver that will be used to find the nodes. It receives the following arguments:

  • args: the args object from the resolver. Example:

    AuthorTC.addResolver({
      name: 'findMany',
      type: AuthorTC,
      args: {
        filter: {
          name: 'String',
          book: 'String',
        },
      },
      resolve: async ({
        args,
      }: {
        args: {
          first?: number;
          after?: string;
          last?: number;
          before?: string;
          /**
           * It's the `first` or `last` argument plus one.
           */
          limit: number;
          /**
           * The `filter` argument, if provided on the query.
           */
          filter?: {
            name: string;
            book: string;
          };
          /**
           * The `sort` argument, if provided on the query as
           * they keys of the `sort` object. In our example
           * above, it's `ASC` and `DESC`. `scanIndexForward`
           * is the value of the `value` property on the sort
           * object. In our example above, it's `true` for
           * `ASC` and `false` for `DESC`.
           */
          sort: {
            scanIndexForward: boolean;
          };
        };
      }) => {
        //
      },
    });
  • rawQuery: an object created by beforeCursorQuery or afterCursorQuery methods from sort option.

countResolver

The resolver that will be used to count the nodes.

sort

It's an object that defines the sort options. Each key is the sort name and the value is an object with the following properties:

  • value: and object that the args resolver will receive as the sort argument. It'll also be the values of the sort enum composer created (check the implementation details here.)
  • cursorFields: an array of fields that will be used to create the cursor.
  • beforeCursorQuery and afterCursorQuery: methods that will be used to create the rawQuery object for the findManyResolver. They receive the following arguments:

    • rawQuery: the rawQuery object that will be used to find the nodes.
    • cursorData: the data from the cursor define on cursorFields. For example, if you define cursorFields as ['id', 'name'], the cursorData will an object with the id and name properties.
    • resolveParams: the resolveParams object from the resolver. You can access args, context and info and other GraphQL properties from this object.

    Example:

    composeWithConnection(AuthorTC, {
      // ...
      sort: {
        ASC: {
          // ...
          cursorFields: ['id', 'name'],
          // Called when `before` cursor is provided.
          beforeCursorQuery: (rawQuery, cursorData, resolveParams) => {
            if (!rawQuery.id) rawQuery.id = {};
            rawQuery.id.$lt = cursorData.id;
            rawQuery.name.$lt = cursorData.name;
          },
          // Called when `after` cursor is provided.
          afterCursorQuery: (rawQuery, cursorData, resolveParams) => {
            if (!rawQuery.id) rawQuery.id = {};
            rawQuery.id.$gt = cursorData.id;
            rawQuery.name.$gt = cursorData.name;
          },
        },
      },
    });

    In the example above, the findManyResolver will receive the following rawQuery object when before cursor is provided:

    {
      "id": {
        "$lt": "id-from-cursor"
      },
      "name": {
        "$lt": "name-from-cursor"
      }
    }

Middlewares

This package provides a way to add middlewares to your final schema. You can add middlewares compatible with graphql-middleware by passing them to the middlewares option on buildSchema method. For example, you can use GraphQL Shield to add authorization to your API:

import { buildSchema } from '@ttoss/graphql-api';
import { allow, deny, shield } from '@ttoss/graphql-api/shield';
import { schemaComposer } from './schemaComposer';

const NotAuthorizedError = new Error('Not authorized!');
/**
 * The error name is the same value `errorType` on GraphQL errors response.
 */
NotAuthorizedError.name = 'NotAuthorizedError';

const permissions = shield(
  {
    Query: {
      '*': deny,
      author: allow,
    },
    Author: {
      id: allow,
      name: allow,
    },
  },
  {
    fallbackRule: deny,
    fallbackError: NotAuthorizedError,
  }
);

/**
 * Apply middlewares to all resolvers.
 */
const logInput = async (resolve, source, args, context, info) => {
  console.log(`1. logInput: ${JSON.stringify(args)}`)
  const result = await resolve(source, args, context, info)
  console.log(`5. logInput`)
  return result
}

/**
 * Apply middlewares only to a specific resolver.
 */
const logOnQueryMe = {
  Query: {
    me: logInput
  }
}

const schema = buildSchema({
  schemaComposer,
  middlewares; [permissions, logInput, logOnQueryMe],
})

Shield

This package re-exports the all methods from GraphQL Shield.

import { allow, deny, shield } from '@ttoss/graphql-api/shield';

Building Schema and Types

As Relay needs an introspection query to work, this package provides a way to build the GraphQL schema by running ttoss-graphl-api build-schema. It build the schema using the schemaComposer from src/schemaComposer.ts file and save the schema in schema/schema.graphql file and TypeScript types in schema/types.ts file.

ttoss-graphl-api build-schema

You can add the build-schema script to your package.json:

{
  "scripts": {
    "build-schema": "ttoss-graphl-api build-schema"
  }
}

If your schemaComposer is in a different directory, you can pass the --directory/-d option to ttoss-graphl-api build-schema command:

ttoss-graphl-api build-schema -d tests

How to Create Tests

We recommend testing the whole GraphQL API using the graphql object and the schema composer to provide the schema. For example:

import { graphql } from 'graphql';
import { schemaComposer } from './schemaComposer';

test('testing my query', () => {
  const author = {
    id: '1',
    name: 'John Doe',
  };

  const response = await graphql({
    schema: schemaComposer.buildSchema(),
    source: /* GraphQL */ `
      query ($id: ID!) {
        node(id: $id) {
          id
          ... on Author {
            name
          }
        }
      }
    `,
    variableValues: {
      id: author.id,
    },
  });

  expect(response).toEqual({
    data: {
      node: {
        id: author.id,
        name: author.name,
      },
    },
  });
});
0.6.3

12 days ago

0.6.2

20 days ago

0.6.1

30 days ago

0.6.0

1 month ago

0.5.3

2 months ago

0.5.2

2 months ago

0.5.1

3 months ago

0.5.0

5 months ago

0.4.7

5 months ago

0.4.5

6 months ago

0.4.6

6 months ago

0.3.6

9 months ago

0.4.4

6 months ago

0.3.5

10 months ago

0.4.1

8 months ago

0.4.0

8 months ago

0.4.3

7 months ago

0.4.2

8 months ago

0.3.4

11 months ago

0.3.3

11 months ago

0.3.1

12 months ago

0.3.0

12 months ago

0.2.0

12 months ago

0.1.0

1 year ago