graphile / graphile/graphile.github.io
Plugin example: setting the GraphQL type to use for a given PostgreSQL type
- Vorherrschende Sprache
- SCSS
- Sterne
- 27
- Forks
- 126
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
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`)
```ts
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;
});
};
}
```
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Rechercherichtung
Der Issue-Body enthält ein TypeScript-Plugin-Beispiel mit GraphQL und PostgreSQL, nennt aber weder eine Repository-Datei noch einen Test. Finde den passenden Bereich für Plugin- oder Dokumentationsbeispiele und überprüfe anschließend, dass das dokumentierte Beispiel skalarähnliche Typen abdeckt und auf die separate Registrierungsanforderung für Eingabe-/Ausgabetypen bei zusammengesetzten Typen hinweist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- graphql, postgresql, typescript
- Bereich
- api, databases, documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 45/100