0.3.2 • Published 2 months ago

graphql-light-builder v0.3.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

GraphQL light builder

This library is designed to create complex Graphql schemes and scripts with the ability to quickly edit them. It is used as a dependency in apollo-graphql-builder: GitHub, npmjs

Functions

  • remove(fieldName) removes a field from the schema,
  • clear() removes all fields from schema,
  • addComplex(complex) adds nested field,
  • getComplex<TComplex>(field) returns nested field builder; may be typed,
  • useSimpleOnly(complexWithSimple = false) sets only non-nested fields to schema; if complexWithSimple === true then one nested fields with non-nested for each,
  • add(...fields) adds a field to schema; if fields contains nested field then adds nested with non-nested fields,
  • set(...fields) set only the specified fields,
  • setSubfields(name, ...subfields) set complex field with specified subfields,
  • build(addEntryName = true) build schema with provided fields. if addEntryName === true then schema name will be added,
  • createScript(scriptType, name, resultSchema, ...paramsMapping) create script by type (query or mutation) with name and resultSchema as answer (SchemaBuilder or string). If you need you can use paramMapping for args for typing graphql args.

Examples

With this library you can create nested schemes. Details you can find in examples/index.ts For example, let's create 3 constructors: Author, Book, Publisher. The fields will be used as an enumeration:

enum PublisherField {
    NAME = 'name',
    ADDRESS = 'address',
}
enum BookField {
    TITLE = 'title',
    PUBLISHER = 'publisher',
}
enum AuthorField {
    ID = 'id',
    NAME = 'name',
    BOOKS = 'books',
}

Builders with nested builders:

class PublisherSchemaBuilder extends SchemaBuilder<PublisherField> {
    constructor(entryName?: string | null | undefined, ...initFields: PublisherField[]) {
        super(PublisherField, entryName, initFields);
    }
}

class BookSchemaBuilder extends SchemaBuilder<BookField> {
    constructor(entryName?: string | null | undefined, ...initFields: BookField[]) {
        super(
            BookField,
            entryName,
            initFields,
            [BookField.PUBLISHER, (e) => new PublisherSchemaBuilder(e)]);
    }
}

class AuthorSchemaBuilder extends SchemaBuilder<AuthorField> {
    constructor(entryName?: string | null | undefined, ...initFields: AuthorField[]) {
        super(
            AuthorField,
            entryName,
            initFields,
            [AuthorField.BOOKS, (e) => new BookSchemaBuilder(e)]
        );
    }
}

To create a schema for an author without using nested fields, you need to run the command:

new AuthorSchemaBuilder("author").build();

Result:

author {
    id
    name
}

To create a schema for the author using nested schemas, you need to run the following command:

const authorWithPublishers = (builder =>
    builder.addComplex(
        builder.getComplex<BookSchemaBuilder>(AuthorField.BOOKS)?.set(BookField.PUBLISHER)
    )
)(new AuthorSchemaBuilder("author").clear()).build();
  • clear() command clears the list of used fields for the author,
  • addComplex() adds a nested schema,
  • getComplex<BookSchemaBuilder>(AuthorField.BOOKS) returns a typed book schema builder by the name by the nested author field,
  • set(BookField.PUBLISHER) command sets only one field in the book schema, which initializes all non-nested Publisher fields.

Result:

author {
    book {
        publishers {
            name
            address
        }
    }

}

If you want create query with Apollo, you can run code like this:

useQuery(
    gql(
        createScript("query", "getAuthor", new AuthorSchemaBuilder("author").build())
    )
)
0.3.2

2 months ago

0.3.1

2 months ago

0.3.0

2 months ago

0.2.0

2 months ago

0.1.0

2 months ago

0.0.8

3 months ago

0.0.7

3 months ago

0.0.6

3 months ago

0.0.5

3 months ago

0.0.4

3 months ago

0.0.3

3 months ago

0.0.2

3 months ago