0.2.20 • Published 5 years ago

@milkandcartoons/service-communication v0.2.20

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

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'
      }
    }
  }
  ...
}
0.2.20

5 years ago

0.2.19

5 years ago

0.2.18

5 years ago

0.2.17

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.1.0-1.alpha.5

5 years ago

0.1.0-1.alpha.4

5 years ago

0.1.0-1.alpha.3

5 years ago

0.1.0-1.alpha.2

5 years ago

0.1.0-1.alpha.1

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago