0.0.58 • Published 2 years ago

@open-buro-am-draht/apollo-datasource-configurable v0.0.58

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

ConfigurableDataSource and Resolve-Directive

Codegen-Plugin to generate Datasources for existing http-Backends.

Configuration

codegen.yml

generates:
  src/generated/data-sources.ts:
    plugins:
      - @open-buro-am-draht/apollo-datasource-configurable:
           services: data-sources.yml

data-sources.yml

RKI:
  target: src/data-sources/rki.ts
  baseURL: https://services7.arcgis.com/mOBPykOjAyBO2ZKk/ArcGIS/rest/services
  functions:
    rkiKeyData:
      path: /rki_key_data_v/FeatureServer/0/query?where=1%3D1&objectIds=&time=&resultType=none&outFields=*&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnDistinctValues=false&cacheHint=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&sqlFormat=none&f=pjson&token=
      method: GET
      arguments:
        arg1: string
    rkiKeyData2:
      path: /rki_key_data_v/FeatureServer/0/query?where=1%3D1&objectIds=&time=&resultType=none&outFields=*&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnDistinctValues=false&cacheHint=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&sqlFormat=none&f=pjson&token=
      method: GET

This configuration will generate a RKI-Datasource in the target-file with methods rkiKeyData and rkiKeyData2 delivering the data of the specified Backend-Resources.

Generated Files:

src/generated/data-sources.ts

This file contains all the configured Resources and functions, will be overwritten by running the Code-Generator.

src/data-sources/rki.ts

An initially empty extended Class of the generated Class. This will only be generated once and can be used to implement custom mapping-functions for the underlying resources.

Arguments and Environment variables

Defined arguments and environment variables can be injected into the url by placeholders.

{env.SOME_ENV_VAR}
{args.argumentName}

Resolve-Directive

A simple directive to make use of generated data sources directly through schema annotations.

Integration

Wrap the schema into the provided directive function and put the directive into the schema source.

const schema = resolveDirectiveTransformer(loadedSchema, "resolveFromDS");

Usage example

Example for Lists and Fields

Result from the Datasource call

{
  "data": [
    {
      "someId": 1,
      "someField": "value1"
    },
    {
      "someId": 2,
      "someField": "value2"
    }
  ]
}

Schema

directive @resolveFromDS(source: String! path: String! keyField: String) on FIELD_DEFINITION

type Query {
    list: [Thing] @resolveFromDS(source: "DataSource.function()" path: "$.data" keyField: "id=someId")
}

type Thing @key(fields: "id") {
    id: Int
    dataField: String @resolveFromDS(source: "DataSource.function()" path: "$.data[?(@.someId=={parent.id})].someField")
}
AttributeDescriptionExample
sourceDatasource functionDataSource.function(param)
pathpath to the actual value of the field (uses jsonpath, see https://www.npmjs.com/package/jsonpath)@.data.field
keyField (optional)mapping for list result types, maps data fields to objects with the given member fieldtypeId=dataId