创建/删除/更新/删除(CRUD)突变通常返回相应的数据库模型实例作为突变的输出类型。然而,对于非 CRUD 突变,我想定义业务逻辑特定的突变输出类型。例如,返回列表元素的计数 + 不能在 graphql 类型和数据库模型之间一对一映射的 ID 列表。我怎样才能做到这一点graphene-django?
当我通过 graphql-playground 创建我的所有者时,它工作正常,\n但我的测试失败并回复我“body.data.createOwner 未定义”,没有数据。
\n// owner.e2e.spec.ts\ndescribe('Owner test (e2e)', () => {\n let app: INestApplication;\n\n beforeAll(async () => {\n const moduleRef = await Test.createTestingModule({\n imports: [\n GraphQLModule.forRoot({\n autoSchemaFile: join(process.cwd(), 'src/schema.gql'),\n }),\n OwnerModule,\n DatabaseModule\n ]\n }).compile();\n app = moduleRef.createNestApplication();\n await app.init();\n });\n\n afterAll(async () => {\n await app.close();\n })\n\n const createOwnerQuery = `\n mutation createOwner($OwnerInput: OwnerInput!) {\n createOwner(ownerInput: $OwnerInput) {\n _id\n name\n firstname\n email\n password\n firstsub\n expsub\n createdAt\n updatedAt\n }\n }\n `;\n \n let id: string = '';\n\n it('createOwner', () => …Run Code Online (Sandbox Code Playgroud) 我已从此模型在 AWS AppSync 上(使用 CLI)生成了一个简单的 GraphQL API:
type WalletProperty @model {
id: ID!
title: String!
}
Run Code Online (Sandbox Code Playgroud)
这生成了 CreateWalletProperty、UpdateWalletProperty 和 DeleteWalletProperty 突变,所有这些都与此类似:
mutation CreateWalletProperty(
$input: CreateWalletPropertyInput!
$condition: ModelWalletPropertyConditionInput <<<<<<<<<<<< what is this for?
) {
createWalletProperty(input: $input, condition: $condition) {
id
title
createdAt
updatedAt
}
}
Run Code Online (Sandbox Code Playgroud)
条件的模式是:
input ModelWalletPropertyConditionInput {
title: ModelStringInput
and: [ModelWalletPropertyConditionInput]
or: [ModelWalletPropertyConditionInput]
not: ModelWalletPropertyConditionInput
}
Run Code Online (Sandbox Code Playgroud)
鉴于我总是必须提供强制性的 $input,$condition 参数有什么用?
我碰巧向 Graphql API(Python3 + Graphene)发送了 2 个单独的请求,以便:
我意识到这可能不符合 Graphql 的“精神”,所以我搜索并阅读了有关嵌套迁移的内容。不幸的是,我还发现这是不好的做法,因为嵌套迁移不是连续的,并且可能会导致客户端由于竞争条件而难以调试问题。
我正在尝试使用顺序根突变来实现考虑嵌套迁移的用例。请允许我向您展示我想象的一个用例和一个简单的解决方案(但可能不是一个好的实践)。很抱歉发了这么长的帖子。
让我们想象一下,我有用户和组实体,我希望从客户端表单更新组,不仅能够添加用户,而且还能够创建一个要添加到组中的用户(如果该用户不存在)。用户的 id 名为 uid(用户 id)和组 gid(组 id),只是为了突出区别。因此,使用根突变,我想象执行如下查询:
mutation {
createUser(uid: "b53a20f1b81b439", username: "new user", password: "secret"){
uid
username
}
updateGroup(gid: "group id", userIds: ["b53a20f1b81b439", ...]){
gid
name
}
}
Run Code Online (Sandbox Code Playgroud)
您注意到我在突变的输入中提供了用户 ID createUser。我的问题是,要进行updateGroup更改,我需要新创建用户的 ID。我不知道如何在 mutate 方法 resolving 内的石墨烯中获取该信息updateGroup,因此我想象在加载客户端表单数据时从 API 查询 UUID。因此,在发送上面的突变之前,在我的客户端初始加载时,我会执行以下操作:
query {
uuid
group (gid: "group id") {
gid
name
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我将在突变请求中使用此查询响应中的 uuid(该值将为b53a20f1b81b439,如上面的第一个 scriptlet 中所示)。 …
我正在创建一个有效的突变,但我不确定它是否按照我认为的方式工作。不过我想知道执行顺序是什么?
我想确保在执行插入/更新插入之前从表中删除某些项目。使用以下突变查询字符串,这是否总是可以执行我想要的操作,或者是否会时不时地工作,因为我假设它是同步的,但实际上它是异步的?
mutation MyMutation(...) {
update_my_table_1(...) { }
delete_my_table_2(...) { }
insert_my_table_2(...) { }
}
Run Code Online (Sandbox Code Playgroud) 我在前端有一个非常基本的 graphql 突变,我将其发送到后端。我在 by 上使用此代码graphql-request作为指南。
对于原语它可以工作:
const mutation = gql`
mutation EditArticle($id: ID!, $title: String) {
editArticle(id: $id, title: $title) {
id
}
}
`
Run Code Online (Sandbox Code Playgroud)
现在我还希望能够更改有关文章的一些元数据,这些元数据存储在meta文章内的对象中:
...,
title: "Hello World",
meta: {
author: "John",
age: 32,
...
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:当使用graphql-request从前端发出请求时,如何将非原始对象类型作为参数传递给突变?
我已经尝试过这样的事情:
const Meta = new GraphQLObjectType({
name: "Meta",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
age ....
}),
})
const mutation = gql`
mutation EditArticle($id: ID!, $title: …Run Code Online (Sandbox Code Playgroud) javascript graphql graphql-js express-graphql graphql-mutation
mutation insert {
insert_ArticleType(objects: {id: 10, name: "test", spaceId: 10, creationDate: ""}) {
affected_rows
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用 x-hasura-role 等于“reader-space”(此处“reader-space”角色未配置插入表权限)运行此查询时,我收到如下错误,
{
"errors": [
{
"extensions": {
"path": "$",
"code": "validation-failed"
},
"message": "no mutations exist"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否可以配置此错误消息?因为在这里,角色没有变异的权限。所以我想从突变中检索出更有意义的信息。是否可以?
尝试refetchQueries在我的useMutation钩子中定义时遇到以下错误。
Type 'DocumentNode' is not assignable to type 'string | PureQueryOptions'.
Property 'query' is missing in type 'DocumentNode' but required in type 'PureQueryOptions'.
Run Code Online (Sandbox Code Playgroud)
我不确定我做错了什么,因为 Apollo 的官方文档对此有点不清楚,而且我在谷歌搜索时找不到任何优先级。
据我了解该错误,query我的 GraphQL 查询中缺少该属性。我是否将正确的值传递给了refetchQueries?我认为我做得对,但后来我不知道为什么它抱怨query没有参与其中。
代码
import { useMutation, useQuery } from '@apollo/client';
import { GET_CUSTOMERS, MARK_AS_VIP } from './queries';
export default function Customers() {
const [ markAsVIP, { data: vips, loading: vipLoading, error: vipError } ] = useMutation( MARK_AS_VIP );
const { loading, error, data: …Run Code Online (Sandbox Code Playgroud) 我正在实施 graphql 登录突变来验证用户登录凭据。Mutation 使用 bcrypt 验证密码,然后将 cookie 发送到客户端,客户端将根据 cookie 是买家还是所有者用户来呈现用户配置文件。
GraphQL 登录突变代码:
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
loginUser: {
type: UserType,
args: {
email: { type: GraphQLString },
password: { type: GraphQLString }
},
resolve: function (parent, args, { req, res }) {
User.findOne({ email: args.email }, (err, user) => {
if (user) {
bcrypt.compare(args.password, user.password).then(isMatch => {
if (isMatch) {
if (!user.owner) {
res.cookie('cookie', "buyer", { maxAge: 900000, httpOnly: false, path: '/' });
} else …Run Code Online (Sandbox Code Playgroud) 我是 graphQL 石墨烯(python)的新手。我想知道是否可以使用石墨烯创建订阅根类型。谢谢
graphql graphene-python graphql-subscriptions graphql-mutation
graphql-mutation ×10
graphql ×9
aws-appsync ×1
django ×1
e2e-testing ×1
graphql-js ×1
hasura ×1
javascript ×1
nestjs ×1
python ×1
python-3.x ×1
react-apollo ×1
reactjs ×1
testing ×1