https://github.com/alphasights/ember-graphql-adapter
GraphQL adapter for Ember Data
https://github.com/alphasights/ember-graphql-adapter
ember ember-addon graphql graphql-adapter
Last synced: about 1 month ago
JSON representation
GraphQL adapter for Ember Data
- Host: GitHub
- URL: https://github.com/alphasights/ember-graphql-adapter
- Owner: alphasights
- License: mit
- Created: 2015-11-03T15:25:19.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2022-02-04T19:08:23.000Z (about 3 years ago)
- Last Synced: 2024-04-14T07:25:15.285Z (about 1 year ago)
- Topics: ember, ember-addon, graphql, graphql-adapter
- Language: JavaScript
- Homepage:
- Size: 2.95 MB
- Stars: 245
- Watchers: 45
- Forks: 26
- Open Issues: 26
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-ember - ember-graphql-adapter - GraphQL adapter for Ember Data. (Packages / Adapters)
README
# Ember Data GraphQL Adapter
[](http://badge.fury.io/js/ember-graphql-adapter)
[](https://codeclimate.com/repos/56718e7b080d2e007b000e1d/feed)
[](https://circleci.com/gh/alphasights/ember-graphql-adapter/tree/master)
[](http://emberobserver.com/addons/ember-graphql-adapter)
[](https://greenkeeper.io/)A Ember CLI adapter for using GraphQL with Ember Data.
## Installation
`ember install ember-graphql-adapter`
## Usage
Create your adapter first
```js
// app/adapters/post.js
import GraphQLAdapter from 'ember-graphql-adapter';export default GraphQLAdapter.extend({
endpoint: 'http://localhost:3000/graph'
});
```Now define your serializer
```js
// app/serializers/post.js
import { Serializer } from 'ember-graphql-adapter';export default Serializer.extend({});
```And you're done!
## Features
* Queries and mutations are automatically generated for you
* Field aliases are supported
* Belongs to relationships are fully supported
* Has many relationships are fully supported
* Async relationships and request coalescing is supported with `coalesceFindRequests: true`## Rails Example
By using the fantastic [graphql](https://github.com/rmosolgo/graphql-ruby) gem,
you can expose your relational database as a GraphQL endpoint.We start by creating a new type
```ruby
# app/models/graph/post_type.rb
module Graph
PostType = GraphQL::ObjectType.define do
name "Post"
description "A post"field :id, types.ID
field :name, types.String
end
end
```Then we create the query type
```ruby
# app/models/graph/query_type.rb
module Graph
QueryType = GraphQL::ObjectType.define do
name "Query"
description "The query root of this schema"field :post, PostType do
argument :id, !types.ID, "The ID of the post"
resolve -> (_object, arguments, _context) do
Post.find(arguments[:id])
end
end
end
end
```After that, it's time for the mutation type
```ruby
# app/models/graph/mutation_type.rb
module Graph
MutationType = GraphQL::ObjectType.define do
name "Mutation"
description "Mutations"field :postCreate, PostType do
argument :name, !types.String, "The post name"
resolve -> (_object, arguments, _context) do
Post.create(name: arguments[:name])
end
end
end
end
```Now, we can build the whole schema
```ruby
# app/models/graph/schema.rb
module Graph
Schema = GraphQL::Schema.define do
query Graph::QueryType
mutation Graph::MutationType
end
end
```In the controller we just delegate to the GraphQL schema
```ruby
# app/controllers/graph_controller.rb
class GraphController < ApplicationController
def execute
render json: ::Graph::Schema.execute(
params.fetch("query"),
context: {} # you can pass the current_user here
)
end
end
```Finally, we just expose the GraphQL endpoint in the route
```ruby
# config/routes.rb
get 'graph', to: 'graph#execute'
```And that's it!
## Developing
### Installation
* `git clone https://github.com/alphasights/ember-graphql-adapter.git`
* `yarn install`### Running
* `yarn start`
### Running Tests
* `yarn run ember test -- --server`
### Building
* `yarn build`