@milkandcartoons/service-communication
TypeScript icon, indicating that this package has built-in type declarations

0.2.20 • Public • Published

Initializing

Client:

// file endpoints.js
import { SCClient } from '@milkandcartoons/service-communication';

const endpoints = [{
  name:  'core',
  url:  process.env.CORE_ENDPOINT_URL,
}];

const scClient =  new SCClient(endpoints);  

export default scClient;

Import scClient to context creation

// file index.js
import scClient from  './endpoints';

// Context creation method
...
// Init client
context.endpoints = scClient.init(context);
// Or add optional contextMapper for mapping context for each request
context.endpoints = scClient.init(context, ctx  => ({ user: ctx.user }));
...

Server:

import { SCServer } from  '@milkandcartoons/service-communication';

const scServer = new SCServer({});

export default scServer;

// sc query schema
export const scSchema =  `
  type Query {
    ${scServer.query}
  }
  ${scServer.type}
`;

// sc query resolvers
export const scResolvers = {
  Query: scServer.resolvers,
};

For graphql-import

//schema.graphql

#import ${pathToNodeModules}/@milkandcartoons/service-communication/src/SCServer/scSchema.graphql

For nexus-prisma

  scServer.prismaTypes - is a necessarry prisma types, you should pass it to the schema creation function.

  scServer.prismaQuery - is the sc query for 'query' and 'function', call it in query definitions function with 't' like this 'scServer.prismaQuery(t)'

For GraphQL requests - SCServer is not required, but you should parse received context from headers and merged with your graphql context. In each request, field context is received in headers in JSON format. Context mapper on client allows you transform context, which will be received on the server request headers

const additionalContext = JSON.stringify(req.headers.context)

GraphQL request example:

context.endpoints.core.sc.graphql({
  query: 'orders',
  $args: { _id: '1234' },
  $fields: {
    _id: 1,
    name: 1,
    $fragments: {
      DeliveryOrder: {
        items: {
          _id
        }
      }
    }
  }
})

GraphQL request syntax

Request with all syntax features

  sc.graphql({
    mutation: 'mutationName',
    $args: {
      argOne: 'Hello',
      argTwo: ['Hello', 'World'],
      nestingArg: {
        arg: 'World'
      }
      enumsArrayArg: {
        $enum: ['ONE', 'TWO']
      },
      enumArg: {
        $enum: 'HELLO'
      }
    },
    $fields: {
      _id: 1,
      $fragments: {
        ImplementingType: {
          name: 1,
          uniqueFieldForThisType: {
            awesomeField: 1
          }
        }
      },
      fieldWithArgs: {
        $args: {
          _id: '123213'
        },
        $fields: {
          fieldValue: 1
        }
      }
    }
  })

Arguments

Arguments is $args key in definition option in .graphql method.

Standart using

  {
  ...
  $args: {
    _id: 'value',
    nested: {
      key: 'value'
    },
    array: ['One', 'Two'],
    numberArray: [1, 2, 3],
    boolean: true
  }
  ...
  }

Using with enums

{
  ...
  $args: {
    singleEnum: {
      $enum: 'HELLO'
    },
    arrayOfEnums: {
      $enum: ['CHO', 'BAR']
    }
  }
  ...
}

Fields

Standart using

  {
    ...
    $fields: {
      _id: 1,
      nested: {
        key: 'value'
      }
    }
    ...
  }

Using with fragments

{
  ...
  $fields: {
    _id: 1,
    $fragments: {
      TargetTypeName: {
        field1: 'value'
      }
    }
  }
  ...
}

Readme

Keywords

none

Package Sidebar

Install

npm i @milkandcartoons/service-communication

Weekly Downloads

34

Version

0.2.20

License

ISC

Unpacked Size

92.4 kB

Total Files

95

Last publish

Collaborators

  • sergk