Skip to content

Build GraphQL document strings from Elixir primitives

License

Notifications You must be signed in to change notification settings

ucbi/graphql_document

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

81 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQLDocument

Build Status hex.pm hex.pm Documentation

Build GraphQL documents from simple Elixir data structures.

A Power Tool for generating GraphQL Documents in Elixir

The goal of GraphQLDocument is to give the developer superpowers when it comes to writing GraphQL document strings in Elixir.

Using the functions in this library, developers can:

  • Build documents programmatically, enabling higher level tooling and DSLs.
  • Compose separate chunks of GraphQL documents together with ease.
  • Dynamically build GraphQL documents on the fly. (For example, including or excluding sections.)

The complete documentation for GraphQLDocument is located here.

Installation

Add :graphql_document as a dependency in mix.exs:

def deps do
  [
    {:graphql_document, "~> 0.2.2"}
  ]
end

Usage

The following call to GraphQLDocument.query(...) will generate the GraphQL string below.

query(
  [
    customer: {[id: var(:customerId)], [
      :name,
      :email,
      phoneNumbers: field(args: [type: MOBILE]),
      cartItems: [
        :costPerItem,
        ...: :cartDetails
      ]
    ]}
  ],
  variables: [customerId: Int],
  fragments: [cartDetails: {
    on(CartItem),
    [:sku, :description, :count]
  }]
)
query ($customerId: Int) {
  customer(id: $customerId) {
    name
    email
    phoneNumbers(type: MOBILE)
    cartItems {
      costPerItem
      ...cartDetails
    }
  }
}

fragment cartDetails on CartItem {
  sku
  description
  count
}

For more information on syntax and features, read the docs here.

License

Copyright 2022 United Community Bank

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.