Skip to content

staff0rd/httpyac-codegen-graphql

Repository files navigation

httpyac-codegen-graphql

A GraphQL code generator preset for generating httpyac requests.

Usage

Add dependencies:

npm i -D @graphql-codegen/cli httpyac-codegen-graphql

Add codegen.yml:

schema: https://api.spacex.land/graphql
generates:
  spacex:
    preset: httpyac-codegen-graphql
    presetConfig:
      host: https://api.spacex.land/graphql
      variables:
        ship_query:
          id: HAWK
        cores_query:
          id: B1034

Generate requests:

npx graphql-codegen

Execute request:

httpyac spacex/ship_query.http --all

Configuration

See Config.ts for all configuration options.

The following configuration example uses a jwt to authenticate to appsync for introspection, and then generates .http files with authentication configured

# codegen.yml
generates:
  output:
    schema:
      # to use introspection - othwerise just specify a graph.schema file
      https://<your-appsync-id>.appsync-api.ap-southeast-2.amazonaws.com/graphql:
        headers:
          Authorization: <your jwt>
    preset: httpyac-codegen-graphql
    presetConfig:
      host: https://<your-appsync-id>.appsync-api.ap-southeast-2.amazonaws.com/graphql
      headers:
        # enables implicit auth
        Authorization: openid implicit oauth
      httpFileVariables:
        # your oauth settings
        oauth_clientId: <your-client-id>
        oauth_authorizationEndpoint: https://login.microsoftonline.com/<your-tenant>/oauth2/authorize?resource=<your-resource-id>
        oauth_tokenEndpoint: https://login.microsoftonline.com/<your-tenant>/oauth2/v2.0/token
      scripts:
        # this script needed to remove 'Bearer' as appsync doesn't use it
        - event: request
          script: request.headers.Authorization = request.headers.Authorization.slice('Bearer '.length);
      variables:
        # set any graphql variables in your generated requests
        some_query:
          someVariable: some-value

Developing

This preset can be cloned locally and developed. During development however, graphql-code-generator needs to be patched locally until this PR is merged.

Build & run

yarn
yarn build && yarn codegen

With logging

yarn build && yarn codegen | more