graphile / graphile/graphile.github.io

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

Abierto
#270 0 comentarios 5 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
SCSS
Estrellas
27
Forks
126
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

El cuerpo del issue proporciona un ejemplo de plugin de TypeScript que usa GraphQL y PostgreSQL, pero no nombra ningún archivo del repositorio ni ninguna prueba. Localiza el área adecuada de ejemplos de plugins o documentación y, a continuación, verifica que el ejemplo documentado cubra tipos similares a escalares y señale el requisito de registro separado de entrada/salida para los tipos compuestos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
graphql, postgresql, typescript
Área
api, databases, documentation
Tipo de issue
Documentación
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.