0.1.4 • Published 1 year ago

add-typenames v0.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

add-typenames

addTypenames copies a given input object and recursively adds GraphQL __typenames to the output.

Installation

yarn install add-typenames graphql

Usage

const typedItem = addTypenames({ item, schema });

Example

import addTypenames from 'add-typenames';
import { buildSchema } from 'graphql'

const typeDefs = `
    """ Add your schema definition here
`;
const item = {
    /* Your item to be typed */
};

const schema = buildSchema(typeDefs);

const typedItem = addTypenames({ item, schema });

Hint: Adding __typename to the root of the input item will resolve most issues that may arise by leveraging the schema to resolve all types rather than relying on heuristics

Handling Unions, Interfaces and Scalars

In order to properly resolve Unions, Interfaces and Scalars we need the resolvers for those types to be included as part of the schema. This can be done with makeExecutableSchema

import { makeExecutableSchema } from '@graphql-tools/schema';

const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
});

Hint: Only include the resolvers for Unions, Interfaces and Scalars. The rest are unused and not needed.

0.1.4

1 year ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago