我不是唯一一个不知道如何使用DatetimeGraphQL-Ruby类型的人:https://github.com/rmosolgo/graphql-ruby-demo/issues/27,你可以看到有18人喜欢我.
如何在这样的某些字段中使用Datetime?
Types::PlayerType = GraphQL::ObjectType.define do
name 'Player'
field :id, !types.ID
field :birth_date, types.Datetime #or what?
field :death_date, !types.Datetime #or what?
field :note, types.String
end
Run Code Online (Sandbox Code Playgroud)
也许我必须使用它(https://github.com/howtographql/graphql-ruby/blob/master/app/graphql/types/date_time_type.rb):
date_time_type.rb:
Types::DateTimeType = GraphQL::ScalarType.define do
name 'DateTime'
coerce_input ->(value, _ctx) { Time.zone.parse(value) }
coerce_result ->(value, _ctx) { value.utc.iso8601 }
end
Run Code Online (Sandbox Code Playgroud)
有人能更好地解释一下吗?
通常查询是在您获取数据时,而变异是在您操作数据时。但是我将如何在没有任何参数的情况下实现突变?
在我的特殊情况下,我删除并创建了 2fa 令牌端点。delete 和 create 令牌都没有参数,因为它们依赖于登录的用户 ID。它们都在数据库中销毁或创建记录。所以我宁愿它们是突变。但这不可能吗?
我正在使用 Graphql-Ruby。但这更像是一个通用的 Graphql 问题。
编辑:
结果我错了。我找不到任何关于它的信息,所以我只是假设这是不可能的。我希望这对其他人有帮助。在 Graphql-Ruby 中,您可以执行以下操作:
mutation {
createFoo(input: {})
}
Run Code Online (Sandbox Code Playgroud) Graphql中删除突变的结果应该是什么?我正在使用graphql-ruby gem.这是我的变异的一个例子,但我只是不确定我应该作为回应返回什么.
Mutations::Brands::Delete = GraphQL::Relay::Mutation.define do
name "DeleteBrand"
description "Delete a brand"
input_field :id, types.ID
# return_field ??
resolve ->(object, inputs, ctx) {
brand = Brand.find(inputs[:id])
brand.destroy
}
end
Run Code Online (Sandbox Code Playgroud) 我正在使用[graphql][1]gem构建一个GraphQL API.
在定义输入时,例如 -
Inputs::BranchInput = GraphQL::InputObjectType.define do
name 'BranchInput'
argument :scope, types.String
end
Run Code Online (Sandbox Code Playgroud)
该参数scope实际上是一个enum字段,它将接受small或者big.如何在输入中定义此参数只接受这两个值?是否有可能在生成文档时在文档中显示此内容,以便UI开发人员也清楚这一点?
当使用Grape生成REST API时,我通常会使用values参数来定义这样的东西.
我想让另一个领域的领域得到解决.
我有一个根据一些参数生成的列表,并希望更新总字段
我的方法可能不正确.
显然,我试图避免重新运行相同的数据库查询并在查询字符串中传递过滤器.
因此,为我的查询假设以下ruby类型:
Types::PostListType = GraphQL::ObjectType.define do
name 'PostList'
field :total, !types.Int, default_value: 0 # <-- this is what I'd like to update in :posts resolution
field :page, !types.Int, default_value: 0 # <-- this is what I'd like to update in :posts resolution
field :per_page, !types.Int, default_value: 0 # <-- this is what I'd like to update in :posts resolution
field :posts, types[Types::PostType] do
argument :page, types.Int, default_value: 1
argument :per_page, types.Int, default_value: 10 # <-- also need …Run Code Online (Sandbox Code Playgroud) 我正在尝试用变量更新我的突变并收到这样的错误:
在“输入字段 SignInInput.signInInput”中找到名称“SignInInput”的重复类型定义
我的突变代码:
module Mutations
class SignIn < BaseMutation
description 'User sign in'
argument :signInInput, Inputs::SignInInput, as: :sign_in_input, required: true
field :token, String, null: false
def resolve(sign_in_input:)
...
end
end
end
Run Code Online (Sandbox Code Playgroud)
我的自定义输入代码:
module Inputs
class SignInInput < Types::BaseInputObject
description "Attributes for user sign in"
argument :email, String, "User email", required: true
argument :password, String, "User password", required: true
end
end
Run Code Online (Sandbox Code Playgroud)
我的查询:
这个奇怪错误的原因是什么?我只定义过一次这样的类。
我是 Graphql 的新手。对于一个简单的用例,假设我有一个简单的模型,
class Post < ActiveRecord::Base
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
下面是我的 graphql query_type.rb 中的代码
PostType = GraphQL::ObjectType.define do
#field :comments, types[CommentType]
#connection :comments, CommentType.connection_type
end
Run Code Online (Sandbox Code Playgroud)
两者都field and connection对我有用。但哪一种是正确的方法。
我的目标是在ruby中测试我的GraphQL模式的类型,我正在使用graphql-ruby gem。
我找不到任何最佳实践,所以我想知道什么是测试架构的字段和类型的最佳方法。
该gem建议不要直接测试该模式http://graphql-ruby.org/schema/testing.html,但是我仍然觉得知道模式何时意外更改很有价值。
具有这样的类型:
module Types
class DeskType < GraphQL::Schema::Object
field :id, ID, 'Id of this Desk', null: false
field :location, String, 'Location of the Desk', null: false
field :custom_id, String, 'Human-readable unique identifier for this desk', null: false
end
end
Run Code Online (Sandbox Code Playgroud)
我的第一种方法是fields在GraphQL :: Schema :: Object类型中使用哈希,例如:
Types::DeskType.fields['location'].type.to_s => 'String!'
Run Code Online (Sandbox Code Playgroud)
创建一个RSpec匹配器,我可以提出如下测试:
RSpec.describe Types::DeskType do
it 'has the expected schema fields' do
fields = {
'id': 'ID!',
'location': 'String!',
'customId': 'String!'
}
expect(described_class).to match_schema_fields(fields)
end
end
Run Code Online (Sandbox Code Playgroud)
但是,这种方法有一些缺点: …
从本指南中,我不明白我们如何在 graphql-ruby 中的字段上添加授权。
我了解我们如何拒绝访问整个对象,但我不明白我们如何阻止仅访问对象的一个字段。在我的用例中,我想阻止一些查询访问String field我的UserType.
是否可以?跟field帮手?
我正在尝试使用 graphql-ruby 和 rails 为过滤器实现输入类型。
基本输入类型如下所示:
module Types
class BaseInputObject < GraphQL::Schema::InputObject
end
end
Run Code Online (Sandbox Code Playgroud)
我自己的输入类型看起来像:
module Types
class PhotoFilterType < Types::BaseInputObject
argument :attribution, String, "Filters by submitter", required: false
argument :country, String, "Filters by country", required: false
argument :year, Int, "Filters by year", required: false
end
end
Run Code Online (Sandbox Code Playgroud)
query_type 的方法标题如下所示:
field :filtered_photos, [Types::PhotoType], null: true do
argument :filters, Types::PhotoFilterType, 'filters for photo', required: true
end
Run Code Online (Sandbox Code Playgroud)
查询如下:
const FILTER_QUERY = gql`
query getFilteredPhotos($filters: PhotoFilterType!) {
filteredPhotos(filters: $filters) {
id
title
width
height …Run Code Online (Sandbox Code Playgroud)