npm package: @open-buro-am-draht/apollo-datasource-configurable
Description: Configurable Datasource for existing http-resources
Author: Guitar Hero
Latest version: 0.0.58

# ConfigurableDataSource and Resolve-Directive

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

## Configuration

codegen.yml

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

data-sources.yml

```yaml
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.

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

## Usage example

### Example for Lists and Fields

#### Result from the Datasource call

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

#### Schema

```graphql
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")
}
```
| Attribute           | Description                                                                                       | Example                    |
|---------------------|---------------------------------------------------------------------------------------------------|----------------------------|
| source              | Datasource function                                                                               | DataSource.function(param) |
| path                | path 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 field            | typeId=dataId              |