graphile / graphile/graphile.github.io

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

Aperta
#270 0 commenti 5 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
SCSS
Stelle
27
Fork
126
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

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`)

```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;
});
};
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

The issue body provides a TypeScript plugin example using GraphQL and PostgreSQL, but names no repository file or test. Locate the appropriate plugin or documentation example area, then verify the documented example covers scalar-like types and notes the separate input/output registration requirement for composite types.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.