import GraphQLBuilder from '@src/hylo-components/utils/graphqlbuilder/graphqlbuilder';

For the functions "createRequest" and "createCall" which return a gql formatted request, at least 'npm install graphql-tag --save-dev' is required which is already included in 'npm install graphql --save-dev'.

Class for stitching together a GraphQL query.

You can define the field structure of models, add nested models to a model and build queries out of the defined models. You don't need to actually write a query but build a query by structuring your defined models into a tree. At any point of your tree structure you can add additional fields and models while you're building your query.

You only need to define your models once and can reuse them whenever and wherever you need to build a query.

Build your models in one file, at one place and use them everywhere in your app to create your requests. "createRequest" and "createCall" returned requests are already wrapped in gql... and ready to use with an ApolloClient.

All methods are static.

Example:

GraphQLBuilder.addModel('festival', ["uuid", "title", "subtitle"]);
GraphQLBuilder.addModel('occurrence', ["uuid", "title", "subtitle"]);
GraphQLBuilder.addModel('allEventReferences', ["uuid", "eventType"], {
occurrence: GraphQLBuilder.getModel('occurrence(limit: 10)', ["description", "startDate"])
});

let query = GraphQLBuilder.createQuery('allEventReferences(limit: $limit, offset: 0)',
GraphQLBuilder.getModel('allEventReferences', {
festival: GraphQLBuilder.getModel('festival')
})
);
let request = GraphQLBuilder.createRequest('query AllEventReferences($limit: Int)', query);

https://graphiql-online.com is a good tool for testing.

Methods

  • Adds a fields array, GraphQLBuilderModel or a query to the general GraphQLBuilder library. The general library is separated from the GraphQLBuilder models library.

    Use it to store field arrays, queries and models including their arguments, variables, directive, alias. Also possible is to store a request as string.

    Parameters

    • name: string

      the name under which the object gets stored

    • fields_or_models_or_query: string | string[] | GraphQLBuilderModel

      the object to be stored

    Returns void

  • Adds a GraphQLBuilderModel to the GraphQLBuilder models library.

    More precise, it creates and adds a copy of the provided model. Clears possible attached arguments, variables, directive and/or alias.

    If you want to store a model including arguments, variables, directive and/or alias, retrieve the model with getModel() and store it in the general library for later use:

    GraphQLBuilder.add('myExtendedModel', GraphQLBuilder.getModel('someModel(limit: $limit)', ['additionalField']));
    const query = GraphQLBuilder.createQuery('someQuery', GraphQLBuilder.getModel(rootModel, {
    extendedModel: GraphQLBuilder.get('myExtendedModel')
    }));

    Parameters

    • name: string

      the name of the model

    • model: GraphQLBuilderModel

      the model to add

    Returns void

  • Creates a "gql"-formatted request for the hylo-apollo-client component "call" function. The "call" function optimizes the requests done by the ApolloClient but has limitations. Only queries (requests of type "query") which do not use variables nor fragments can be converted into call requests.

    The id of your call should always be unique to that specific call since it is part of the optimization process.

    Parameters

    • id: string

      the request name

    • queries: string | string[]

      one or more queries sent with your request

    Returns any

    a GraphQL document to be used with a hylo-apollo-client call

  • Creates a GraphQLBuilderModel and adds it to the GraphQLBuilder models library.

    You can add additional fields and models when you are building a query or extend the model with getModel(). Define simple building blocks with which you can create more complex queries and models later on.

    Parameters

    • name: string

      the name of the model

    • Optional fields: string[]

      an array of field names ("primitive" types, not nested models)

    • Optional models: DictionaryOfGraphQLBuilderModel<GraphQLBuilderModel>

      a dictionary of nested models, the "keys" are the field names of the models

    Returns void

  • Creates a query, mutation or fragment.

    If it is a query or mutation, define the header with the root query field name while also including arguments, variables and/or aliases:

    • someQuery(id: 100)
    • someAlias: someQuery(id: $id) ... or any other setup you like.

    If it is a fragment you are creating, the header should look somehow like this:

    • fragment comparisonFields on SomeType You can reference the fragemnt by providing "...comparisonFields" as field to a model.

    The header is placed as provided right before your root model definition.

    Since you are providing a header, possibly defined arguments, directive and alias of the given root model are ignored.

    Parameters

    • header: string

      the header of your query

    • Optional model: GraphQLBuilderModel

      the root model of your query (optional)

    Returns string

  • Creates a request, already "gql"-formatted and ready to be sent to the GraphQL server.

    In the header you can set your request name, define variables and the type of request you are creating:

    • query AllEventReferences($offset: Int = 0)
    • mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!)

    The header is placed as provided in your request.

    Parameters

    • header: string

      the header of your request

    • queries: string | string[]

      one or more queries sent with your request

    • Optional fragments: string | string[]

      one or more fragments used in your queries

    Returns any

    a GraphQL document to be used with a ApolloClient

  • Returns a fields array, query or a GraphQLBuilderModel stored in the general library.

    Parameters

    • name: string

      the name of the fields array, query or GraphQLBuilderModel as defined with add()

    Returns any

    string|string[]|GraphQLBuilderModel

  • Returns a GraphQLBuilderModel from the GraphQLBuilder models library.

    You can add additional fields and/or models to the returned model. The returned model is not the original model stored in the GraphQLBuilder models library but a copy extended by the additionally provided fields and models.

    With the name you can provide arguments, variables, a directive and/or an alias:

    • name
    • name(limit: 10)
    • name @include(if: $onlyTrue)
    • alias: name(limit: $limit) The name is parsed for these additional information and is not passed/used as provided.

    You can create a tunnel for chained models where the additionally provided fields and models are not added to the model of the function call but tunnel through the chain to the last child model of the tunnel. To create a tunnel set the tunnel flag for each member of the chain. A tunnel can not be used directly but needs to be saved first to the GraphQLBuilder models library with addModel(). A tunnel needs to be created in one call. You can not use saved tunnels to create another tunnel:

    GraphQLBuilder.addModel('tunnel', GraphQLBuilder.getModel('firstLevelModel', {
    child: GraphQLBuilder.getModel('secondLevelModel', {}, {}, true)
    }, {}, true));

    GraphQLBuilder.getModel('tunnel', ['field'], {model: GraphQLBuilder.getModel('someModel')});

    The additional "field" and "model" are passed to the "child" secondLevelModel and not added to the firstLevelModel. You can deactivate the tunnel by setting the "tunnel" flag to false when calling/getting the tunnel.

    Parameters

    • name: string

      the name of the model as defined with addModel() including arguments, variables, directive, alias

    • Optional fields_or_models: string[] | DictionaryOfGraphQLBuilderModel<GraphQLBuilderModel>

      additional fields or models

    • Optional models: DictionaryOfGraphQLBuilderModel<GraphQLBuilderModel>

      additional models when also additional fields where provided

    • Optional tunnel: boolean

      pass provided additional fields and models down to the last child model in a row which is also set "tunnel"

    Returns GraphQLBuilderModel

  • Same as createRequest() but returns a string. Needs to be formatted with "gql" in order to be used with the ApolloClient:

    gql(string_request)
    

    Parameters

    • header: string

      the header of your request

    • queries: string | string[]

      one or more queries sent with your request

    • Optional fragments: string | string[]

      one or more fragments used in your queries

    Returns string

Generated using TypeDoc