Plugin example: setting the GraphQL type to use for a given PostgreSQL type

Aperta
#270 0 commenti 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
45/100
Tipo di issue
Documentazione
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
graphql, postgresql, typescript

Direzione di ricerca

Il corpo dell’issue fornisce un esempio di plugin TypeScript che usa GraphQL e PostgreSQL, ma non indica alcun file del repository né alcun test. Individua l’area appropriata degli esempi di plugin o della documentazione, quindi verifica che l’esempio documentato copra i tipi simili agli scalari e segnali il requisito di registrazione separata per input/output per i tipi compositi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Thanks to @Gomes on Discord. This only works for scalar-like types, for composite types you'll need to register separate input and output types (i.e. in addition to calling pgRegisterGqlTypeByTypeId you also need to call pgRegisterGqlInputTypeByTypeId)

import type { GraphQLType } from "graphql";
import type { Plugin } from "graphile-build";
import type { PgType } from "graphile-build-pg";

interface PgDomainToGqlTypePluginArgs {
  /** PostgreSQL type identifier, e.g. "my_schema.my_type" */
  typeIdentifier: string;
  /** What GraphQL type should we use for this PG type */
  gqlType: GraphQLType | ((build: Build) => GraphQLType);
}

/**
 * Sets the GraphQL type `gqlType` to be used for the PostgreSQL type identified
 * by `typeIdentifier`
 */
export function makePgDomainToGqlTypePlugin({
  typeIdentifier,
  gqlType,
}: PgDomainToGqlTypePluginArgs): Plugin {
  return (builder) => {
    builder.hook("build", (build) => {
      const { namespaceName, entityName: typeName } = build.pgParseIdentifier(
        typeIdentifier
      );

      const pgType = build.pgIntrospectionResultsByKind.type.find(
        (type: PgType) =>
          type.namespaceName === namespaceName && type.name === typeName
      );

      if (!pgType) {
        throw new Error(`Failed to find the type '${typeIdentifier}'`);
      }

      build.pgRegisterGqlTypeByTypeId(pgType.id, () =>
        typeof gqlType === "function" ? gqlType(build) : gqlType
      );

      return build;
    });
  };
}
Lingua principale
SCSS
Stelle
27
Fork
124
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

  • Nessun Dockerfile né file Docker Compose
  • Ha un modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di graphile/graphile.github.io

Tutte le issue di graphile/graphile.github.io

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.