在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):alphasights/ember-graphql-adapter开源软件地址(OpenSource Url):https://github.com/alphasights/ember-graphql-adapter开源编程语言(OpenSource Language):JavaScript 97.7%开源软件介绍(OpenSource Introduction):Ember Data GraphQL AdapterA Ember CLI adapter for using GraphQL with Ember Data. Installation
UsageCreate your adapter first // app/adapters/post.js
import GraphQLAdapter from 'ember-graphql-adapter';
export default GraphQLAdapter.extend({
endpoint: 'http://localhost:3000/graph'
}); Now define your serializer // app/serializers/post.js
import { Serializer } from 'ember-graphql-adapter';
export default Serializer.extend({}); And you're done! Features
Rails ExampleBy using the fantastic graphql gem, you can expose your relational database as a GraphQL endpoint. We start by creating a new type # 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 # 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 # 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 # 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 # 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 # config/routes.rb
get 'graph', to: 'graph#execute' And that's it! DevelopingInstallation
Running
Running Tests
Building
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论