当前位置: 首页 > 软件库 > 大数据 > 数据查询 >

elm-graphql

Autogenerate type-safe GraphQL queries in Elm.
授权协议 View license
开发语言 Java
所属分类 大数据、 数据查询
软件类型 开源软件
地区 不详
投 递 者 申宜
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

dillonkearns/elm-graphql

Build Status

elm-graphql logo

Why use this package over the other available Elm GraphQL packages? This is the only one thatgenerates type-safe code for your entire schema. Check out this blog post, Type-Safe & Composable GraphQL in Elm, to learn more about the motivation for this library. (It's also the only type-safelibrary with Elm 0.18 or 0.19 support, seethis discourse thread).

I built this package because I wanted to have something that:

  1. Gives you type-safe GraphQL queries (if it compiles, it's valid according to the schema),
  2. Creates decoders for you in a seamless and failsafe way, and
  3. Eliminates GraphQL features in favor of Elm language constructs where possible for a simpler UX (for example, GraphQL variables & fragments should just be Elm functions, constants, lets).

See an example in action on Ellie. See more end-to-end example code in theexamples/folder.

Overview

dillonkearns/elm-graphql is an Elm package and accompanying command-line code generator that creates type-safe Elm code for your GraphQL endpoint. You don't write any decoders for your API with dillonkearns/elm-graphql, instead you simply select which fields you would like, similar to a standard GraphQL query but in Elm. For example, this GraphQL query

query {
  human(id: "1001") {
    name
    homePlanet
  }
}

would look like thisin dillonkearns/elm-graphql (the code in this example that is prefixed with StarWars is auto-generated)

import Graphql.Operation exposing (RootQuery)
import Graphql.SelectionSet as SelectionSet exposing (SelectionSet)
import StarWars.Object
import StarWars.Object.Human as Human
import StarWars.Query as Query
import StarWars.Scalar exposing (Id(..))


query : SelectionSet (Maybe HumanData) RootQuery
query =
    Query.human { id = Id "1001" } humanSelection


type alias HumanData =
    { name : String
    , homePlanet : Maybe String
    }


humanSelection : SelectionSet HumanData StarWars.Object.Human
humanSelection =
    SelectionSet.map2 HumanData
        Human.name
        Human.homePlanet

GraphQL and Elm are a perfect match because GraphQL is used to enforce the types that your API takes as inputs and outputs, much like Elm's type system does within Elm. elm-graphql simply bridges this gap by making your Elm code aware of your GraphQL server's schema. If you are new to GraphQL, graphql.org/learn/ is an excellent way to learn the basics.

After following the installation instructions to install the @dillonkearns/elm-graphqlNPM package and the proper Elm packages (see the Setup section for details).Once you've installed everything, running the elm-graphql code generation toolis as simple as this:

npx elm-graphql https://elm-graphql.herokuapp.com --base StarWars --output examples/src

If headers are required, such as a Bearer Token, the --header flag can be supplied.

npx elm-graphql https://elm-graphql.herokuapp.com --base StarWars --output examples/src --header 'headerKey: header value'

Learning Resources

  • There is a thorough tutorial in the SelectionSet docs. SelectionSets are the core concept in this library, so I recommend reading through the whole page (it's not very long!).

  • The examples/ folder is another great place to start.

  • If you want to learn more GraphQL basics, this is a great tutorial, and a short read: graphql.org/learn/

  • My Elm Conf 2018 talk goes into the philosophy behind dillonkearns/elm-graphql

(Skip to 13:06 to go straight to the dillonkearns/elm-graphql demo).

  • My 10-minute video tutorial on how to leverage Custom Scalars in elm-graphql using the Scalar Codecs feature.

If you're wondering why code is generated a certain way, you're likely to find an answer in the Frequently Asked Questions (FAQ).

There's a very helpful group of people in the #graphql channel in the Elm Slack. Don't hesitate to ask any questions about getting started, best practices, or just general GraphQL in there!

Setup

dillonkearns/elm-graphql generates Elm code that allows you to build up type-safe GraphQL requests. Here are the steps to setup dillonkearns/elm-graphql.

  1. Add the dillonkearns/elm-graphql elm packageas a dependency in your elm.json. You will also need to make sure that elm/json is a dependency of your projectsince the generated code has lots of JSON decoders in it.

    elm install dillonkearns/elm-graphql
    elm install elm/json
  2. Install the @dillonkearns/elm-graphql command line tool through npm. This is what you will use to generate Elm code for your API.It is recommended that you save the @dillonkearns/elm-graphql command line tool as a devdependency so that everyone on your project is using the same version.

    npm install --save-dev @dillonkearns/elm-graphql
    # you can now run it locally using `npx elm-graphql`,
    # or by calling it through an npm script as in this project's package.json
  3. Run the @dillonkearns/elm-graphql command line tool installed above to generate your code. If you used the --save-dev method above, you can simply create a script in your package.json like the following:

    {
      "name": "star-wars-elm-graphql-project",
      "version": "1.0.0",
      "scripts": {
        "api": "elm-graphql https://elm-graphql.herokuapp.com/api --base StarWars"
      }
    
  4. With the above in your package.json, running npm run api will generate dillonkearns/elm-graphql code for you to call in ./src/StarWars/. You can now use the generated code as in this Ellie example or in the examples folder.

Subscriptions Support

You can do real-time APIs using GraphQL Subscriptions and dillonkearns/elm-graphql.Just wire in the framework-specific JavaScript code for opening the WebSocket connectionthrough a port. Here's a live demo and itssource code. The demo server is running Elixir/Absinthe.

Contributors

Thank you Mario Martinez (martimatix) forall your feedback, the elm-format PR, and for the incredible logo design!

Thank you Mike Stock (mikeastock) forsetting up Travis CI!

Thanks for the reserved words pull request @madsflensted!

A huge thanks to @xtian for doing the vast majorityof the 0.19 upgrade work! ��

Thank you Josh Adams (@knewter) for the code example forSubscriptions with Elixir/Absinthe wired up through Elm ports!

Thank you Romario for adding OptionalArgument.map!

Thank you Aaron White for your pull request to improve the performance andstability of the elm-format step! ��

Roadmap

All core features are supported. That is, you can build any query or mutationwith your dillonkearns/elm-graphql-generated code, and it is guaranteed to be valid accordingto your server's schema.

dillonkearns/elm-graphql will generate code for you to generate subscriptionsand decode the responses, but it doesn't deal with the low-level details forhow to send them over web sockets. To do that, you will need to usecustom code or a package that knows how to communicate over websockets (or whicheverprotocol) to setup a subscription with your particular framework. Seethis discussion for whythose details are not handled by this library directly.

I would love to hear feedback if you are using GraphQL Subscriptions. In particular,I'd love to see live code examples to drive any improvements to the Subscriptionsdesign. Please ping me on Slack, drop a message in the#graphql channel, or open up aGithub issue to discuss!

I would like to investigate generating helpers to make pagination simplerfor Connections (based on theRelay Cursor Connections Specification).If you have ideas on this chime in on this thread.

See the full roadmap on Trello.

  • 背景 REST作为一种现代网络应用非常流行的软件架构风格,自从Roy Fielding博士在2000年他的博士论文中提出来到现在已经有了20年的历史。它的简单易用性,可扩展性,伸缩性受到广大Web开发者的喜爱。 REST 的 API 配合JSON格式的数据交换,使得前后端分离、数据交互变得非常容易,而且也已经成为了目前Web领域最受欢迎的软件架构设计模式。 但随着REST API的流行和发展,它的

 相关资料
  • Elm

    Elm是一种函数式语言,可编译为HTML、CSS和JavaScript。 Elm为函数式反应编程而设计,便于创建高可交互应用。

  • vue2-elm 基于 vue2 + vuex 构建一个具有 45 个页面的大型单页面应用。 前言 初学vue时曾在网上搜索vue的实战项目源码,无奈大部分都是简单的demo,对于深究vue没有太大的帮助,剩下的一些大部分都是像音乐播放器之类的展示型项目,交互没有预期那么复杂。但我们实际在工作中,经常会遇到有购物车的项目,这类项目因为涉及到money,所以对逻辑严谨度要求高,页面之间交互复杂,又会

  • About 因为前端项目是根据饿了么官网接口写的,所以后台系统也保持了和官网一致的API接口。 整个项目分为两部分:前台项目接口、后台管理接口,共60多个。涉及登陆、注册、添加商品、商品展示、筛选排序、购物车、下单、用户中心等,构成一个完整的流程。 注1:此项目纯属个人瞎搞,不用于任何商业用途。 注2:项目预览地址和接口需要使用https访问哦! 说明 node-elm 接口文档: 接口文档地址

  • Create Elm App Create Elm apps with no build configuration. Getting Started – How to create a new app. User Guide – How to develop apps bootstrapped with Create Elm App. Elm SPA example - Using Create

  • Elm Millennium Edition 是基于文本的邮件客户端,包括两个分支:Elm 2.4ME+,是基于 Elm 2.4;Elm ME+ 2.5, 从 Elm 2.5 中合并了一些代码。 主要特性: Elm Millennium Edition 包括增强版的 MIME 和字符集 可以处理UTF-8(Unicode)的子集 Elm Millennium Edition 可以从本地 mbox

  • 本文向大家介绍Elm筛选清单,包括了Elm筛选清单的使用技巧和注意事项,需要的朋友参考一下 例子 List.filter : (a -> Bool) -> List a -> List a是一个高阶函数,它将一个参数函数从任何值转换为布尔值,并将该函数应用于给定列表的每个元素,仅保留该函数为其返回的那些元素True。List.filter以第一个参数为函数的函数通常称为谓词。 在中对此进行评估el